diff --git a/async-skipdb/Cargo.toml b/async-skipdb/Cargo.toml index 90d2e01..8460aaf 100644 --- a/async-skipdb/Cargo.toml +++ b/async-skipdb/Cargo.toml @@ -36,3 +36,13 @@ smol = "2" [package.metadata.docs.rs] all-features = true rustdoc-args = ["--cfg", "docsrs"] + +[[example]] +name = "optimistic" +path = "examples/optimistic.rs" +required-features = ["tokio"] + +[[example]] +name = "serializable" +path = "examples/serializable.rs" +required-features = ["tokio"] diff --git a/async-skipdb/README.md b/async-skipdb/README.md index fe5c41d..405d240 100644 --- a/async-skipdb/README.md +++ b/async-skipdb/README.md @@ -88,52 +88,19 @@ This crate contains two kinds of in-memory key-value database: ## Example - ```rust - use async_skipdb::serializable::TokioSerializableDb; - - #[derive(Debug)] - struct Person { - name: String, - hobby: String, - age: u8, - } - - #[tokio::main] - async fn main() { - let db: TokioSerializableDb = TokioSerializableDb::new().await; +See [examples](./examples/). + +- Optimistic - { - let alice = Person { name: "Alice".to_string(), hobby: "swim".to_string(), age: 20 }; - let bob = Person { name: "Bob".to_string(), hobby: "run".to_string(), age: 30 }; - - let mut txn = db.serializable_write().await; - txn.insert(1, alice).unwrap(); - txn.insert(2, bob).unwrap(); - - { - let alice = txn.get(&1).unwrap().unwrap(); - assert_eq!(alice.value().name, "Alice"); - assert_eq!(alice.value().age, 20); - assert_eq!(alice.value().hobby, "swim"); - } - - txn.commit().await.unwrap(); - } - - { - let txn = db.read().await; - let alice = txn.get(&1).unwrap(); - assert_eq!(alice.value().name, "Alice"); - assert_eq!(alice.value().age, 20); - assert_eq!(alice.value().hobby, "swim"); - - let bob = txn.get(&2).unwrap(); - assert_eq!(bob.value().name, "Bob"); - assert_eq!(bob.value().age, 30); - assert_eq!(bob.value().hobby, "run"); - } - } - ``` + ```bash + cargo run --example optimistic --features tokio + ``` + +- Serializable + + ```bash + cargo run --example serializable --features tokio + ``` #### License