custom_serde/
custom_serde.rs

1use serde::{Deserialize, Serialize};
2use typed_sled::custom_serde::{serialize::BincodeSerDeLazy, Tree};
3
4fn main() -> Result<(), Box<dyn std::error::Error>> {
5    // Creating a temporary sled database.
6    // If you want to persist the data use sled::open instead.
7    let db = sled::Config::new().temporary(true).open().unwrap();
8
9    // Notice that we are using &str, and SomeValue<'a> here which do not implement
10    // serde::DeserializeOwned and thus could not be used with typed_sled::Tree.
11    // However our custom lazy Deserializer contained in BincodeSerDeLazy allows us
12    // to perform the deserialization lazily and only requires serde::Deserialize<'a>
13    // for the lazy deserialization.
14    let tree = Tree::<&str, SomeValue, BincodeSerDeLazy>::open(&db, "unique_id");
15
16    tree.insert(&"some_key", &SomeValue("some_value"))?;
17
18    assert_eq!(
19        tree.get(&"some_key")?.unwrap().deserialize(),
20        SomeValue("some_value")
21    );
22    Ok(())
23}
24
25#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
26struct SomeValue<'a>(&'a str);