Crate crio

source · []
Expand description

This crate provides an easy to use API to store persistent data of the same type.

Any type that is able to be deserialized or serialized using serde can be stored on disk. Data is stored on disk with a CRC32 checksum associated with every document to ensure data integrity. Each document is deserialized into bytes via bincode when writing to file. All data is stored in little-endian order.

This crate is meant for storing small serializable data that stores the state of an application after exit. Since all the data is loaded into memory when calling load on a Client<T>, handling large amounts of data is not advised. However, the data stored on disk has a relatively small footprint and should not take up much space.

Note that this is not an embedded database and there are other crates which are better suited for this task, such as sled: https://github.com/spacejam/sled

Note

This documentation uses terminology derived from document-oriented databases (e.g. MongoDB) such as “document” or “collection”.

Example

use crio::Client;
use serde_derive::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
struct Message {
    id: usize,
    message: String,
}

let msg1 = Message {
    id: 1,
    message: "Hello there, you suck".to_string(),
};
let msg2 = Message {
    id: 2,
    message: "No you".to_string(),
};
let msg3 = Message {
    id: 3,
    message: "You both suck".to_string(),
};
let messages = vec![msg1, msg2, msg3];
let client: Client<Message> = Client::new("messages", false)?; // If no file is found, a new empty file is created.
client.write_many(&messages)?; // If no file is found, a new file is created and then written to. Since append is set to false for its client, this method overwrites any previous value stored on the same file
let returned_messages = client.load()?;
if let Some(data) = returned_messages {
    assert_eq!(messages, data);
} else {
    panic!("File is empty");
}

Structs

Responsible for handling IO operations as well as serialization and deserialization.

Enums

This is the main error type of this crate.