Module custom_serde

Module custom_serde 

Source
Expand description

Support for custom (de)serialization.

This module exports the same types as the root module, however the types take an additional generic parameter called SerDe. The SerDe type must implement the trait SerDe which defines how (de)serialization takes place.

The Tree<K, V> is equivalent to Tree<K, V, BincodeSerDe> from this module.

The following features are supported for the custom (de)serialization Tree:

  • [key_generating][self::key_generating]: Create Trees with automatically generated keys.
  • [convert][self::convert]: Convert any Tree into another Tree with different key and value types.

§Example

use serde::{Deserialize, Serialize};
use typed_sled::custom_serde::{serialize::BincodeSerDeLazy, Tree};

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
struct SomeValue<'a>(&'a str);

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Creating a temporary sled database.
    // If you want to persist the data use sled::open instead.
    let db = sled::Config::new().temporary(true).open().unwrap();

    // Notice that we are using &str, and SomeValue<'a> here which do not implement
    // serde::DeserializeOwned and thus could not be used with typed_sled::Tree.
    // However our custom lazy Deserializer contained in BincodeSerDeLazy allows us
    // to perform the deserialization lazily and only requires serde::Deserialize<'a>
    // for the lazy deserialization.
    let tree = Tree::<&str, SomeValue, BincodeSerDeLazy>::open(&db, "unique_id");

    tree.insert(&"some_key", &SomeValue("some_value"))?;

    assert_eq!(
        tree.get(&"some_key")?.unwrap().deserialize(),
        SomeValue("some_value")
    );
    Ok(())
}

Modules§

serialize
Create custom (de)serializers for key and value (de)serialization.

Structs§

Batch
CompareAndSwapError
Compare and swap error.
Iter
Subscriber
TransactionalTree
Tree
A flash-sympathetic persistent lock-free B+ tree.

Enums§

Event