Rustbreak is an [Daybreak] inspired self-contained file database. It is meant to be fast and simple to use. You add it to your application and it should just work for you. The only thing you will have to take care of is saving.
This library started out because of a need to be able to quickly write an application in rust that needed some persistence while still being able to write arbitrary data to it.
In Ruby there is [Daybreak] however for Rust there was no similar crate, until now!
Add this to your Cargo.toml
:
toml
[dependencies.rustbreak]
version = "2"
features = ["ron_enc"] # You can also use "yaml_enc" or "bin_enc"
# Check the documentation to add your own!
```rust
use rustbreak::{MemoryDatabase, deser::Ron};
let db = MemoryDatabase::
println!("Writing to Database"); db.write(|db| { db.insert(0, String::from("world")); db.insert(1, String::from("bar")); });
db.read(|db| { // db.insert("foo".into(), String::from("bar")); // The above line will not compile since we are only reading println!("Hello: {:?}", db.get(&0)); })?;
```
Usage is quite simple:
FileDatabase
with FileDatabase::from_path
MemoryDatabase
with MemoryDatabase::memory
Database
with Database::from_parts
Write
/Read
data from the Databasesave
periodically, or whenever it makes sense.
```rust
use rustbreak::{MemoryDatabase, deser::Ron};
let db = MemoryDatabase::
println!("Writing to Database"); db.write(|db| { db.insert("hello".into(), String::from("world")); db.insert("foo".into(), String::from("bar")); });
db.read(|db| { // db.insert("foo".into(), String::from("bar")); // The above line will not compile since we are only reading println!("Hello: {:?}", db.get("hello")); }); ```
The following parts explain how to enable the respective features. You can also enable several at the same time.
If you would like to use yaml you need to specify yaml_enc
as a feature:
toml
[dependencies.rustbreak]
version = "1"
features = ["yaml_enc"]
You can now use rustbreak::deser::Yaml
as deserialization struct.
If you would like to use ron
you need to
specify ron_enc
as a feature:
toml
[dependencies.rustbreak]
version = "1"
features = ["ron_enc"]
You can now use rustbreak::deser::Ron
as deserialization struct.
If you would like to use bincode you need to specify bin_enc
as a feature:
toml
[dependencies.rustbreak]
version = "1"
features = ["bin_enc"]
You can now use rustbreak::deser::Bincode
as deserialization struct.