Struct mvdb::Mvdb
[−]
[src]
pub struct Mvdb<T> { /* fields omitted */ }
Minimum Viable Psuedo Database
Methods
impl<T> Mvdb<T> where
T: Serialize + DeserializeOwned,
[src]
T: Serialize + DeserializeOwned,
pub fn new(data: T, path: &Path) -> Result<Self>
[src]
Create a new Mvdb
given data to contain and path to store.
File will be created and written to immediately
Examples
let data = DemoData::new(); let file = Path::new("demo.json"); let my_data = Mvdb::new(data, &file) .expect("Could not write to file");
pub fn new_pretty(data: T, path: &Path) -> Result<Self>
[src]
Create a new Mvdb
given data to contain and path to store.
File will be created and written to immediately. Information
will be stored in a "pretty-print" JSON format, at the cost
of additonal storage space and write time
Examples
let data = DemoData::new(); let file = Path::new("demo_pretty.json"); let my_data = Mvdb::new_pretty(data, &file) .expect("Could not write to file");
pub fn from_file(path: &Path) -> Result<Self>
[src]
Create a new Mvdb
given just the path. If the file does
not exist, or the contained data does not match the schema
of T
, this will return an Error
Examples
let file = Path::new("demo.json"); let my_data: Mvdb<DemoData> = Mvdb::from_file(&file); .expect("File does not exist, or schema mismatch");
pub fn from_file_pretty(path: &Path) -> Result<Self>
[src]
Create a new Mvdb
given just the path. If the file does
not exist, or the contained data does not match the schema
of T
, this will return an Error. Subsequent writes will
be stored in a "pretty-print" JSON format, at the cost of
additional storage space and write time. The file does not
need to be "pretty-printed" before using this function
Examples
let file = Path::new("demo_pretty.json"); let my_data: Mvdb<DemoData> = Mvdb::from_file_pretty(&file); .expect("File does not exist, or schema mismatch");
pub fn access<F, R>(&self, action: F) -> Result<R> where
F: Fn(&T) -> R,
[src]
F: Fn(&T) -> R,
Provide atomic read-only access to the database contents via a closure. Contents are accessed in-memory only, and will not re-read from the storage file, or cause any writes
Examples
let foo_from_disk = my_data.access(|db| db.foo.clone()) .expect("Failed to access file");
pub fn access_mut<F, R>(&self, action: F) -> Result<R> where
F: FnOnce(&mut T) -> R,
[src]
F: FnOnce(&mut T) -> R,
Provide atomic writable access to the database contents via a closure. If the hash of the serialized contents after the access has changed, the database will be written to the file.
Examples
my_data.access_mut(|db: &mut DemoData| { db.baz = "New Value".into(); }).expect("Failed to access file");
impl<T> Mvdb<T> where
T: Serialize + DeserializeOwned + Default,
[src]
T: Serialize + DeserializeOwned + Default,
pub fn from_file_or_default(path: &Path) -> Result<Self>
[src]
Attempt to load from a file. If the file does not exist,
or if the schema does not match, a new file will be written
with the default contents of T
.
Examples
let file = Path::new("demo.json"); let my_data: Mvdb<DemoData> = Mvdb::from_file_or_default(&file); .expect("Could not write to file");
pub fn from_file_or_default_pretty(path: &Path) -> Result<Self>
[src]
Attempt to load from a file. If the file does not exist,
or if the schema does not match, a new file will be written
with the default contents of T
. Any writes made will use
pretty-printed JSON
Examples
let file = Path::new("demo_pretty.json"); let my_data: Mvdb<DemoData> = Mvdb::from_file_or_default_pretty(&file); .expect("Could not write to file");
Trait Implementations
impl<T> Clone for Mvdb<T>
[src]
Implement Clone
manually, otherwise Rust expects T
to also impl Clone
,
which is not necessary