Skip to content

Commit

Permalink
Improve example
Browse files Browse the repository at this point in the history
  • Loading branch information
al8n committed Oct 25, 2024
1 parent 019602e commit 4c7fe9b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 45 deletions.
10 changes: 10 additions & 0 deletions async-skipdb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
57 changes: 12 additions & 45 deletions async-skipdb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64, Person> = 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

Expand Down

0 comments on commit 4c7fe9b

Please sign in to comment.