Add into_inner and from_parts for the database

This commit is contained in:
Marcel Müller
2018-04-14 11:54:34 +02:00
parent b136c1900e
commit e311ee9ac3
2 changed files with 23 additions and 15 deletions
+7 -15
View File
@@ -26,19 +26,19 @@ Features
- Simple To Use, Fast, Secure
- Threadsafe
- Key/Value Storage
- bincode or yaml storage
- ron, bincode, or yaml storage
Usage
-----
Usage is quite simple:
- Create/open a database using `Database::open`
- You can specify the kind of Key you want using this Syntax:
`Database::<Key>::open`
- `Insert`/`Retrieve` data from the Database
- Don't forget to run `flush` periodically
- Create/open a database using one of the Database constructors:
- Create a `FileDatabase` with `FileDatabase::from_path`
- Create a `MemoryDatabase` with `MemoryDatabase::memory`
- Create a `Database` with `Database::from_parts`
- `Write`/`Read` data from the Database
- Don't forget to run `sync` periodically
```rust
# use std::collections::HashMap;
@@ -88,14 +88,6 @@ default-features = false
features = ["ron_enc"]
```
How it works
------------
Internally the Database holds a Hashmap behind a RwLock.
This Hashmap is then written to/read from and safely casted to the requested
type. This works thanks to encoding/decoding traits.
[doc]:http://neikos.me/rustbreak/rustbreak/index.html
[Daybreak]:https://propublica.github.io/daybreak/
+16
View File
@@ -164,6 +164,22 @@ impl<Data, Back, DeSer> Database<Data, Back, DeSer>
backend.put_data(ser.as_bytes())?;
Ok(())
}
/// Create a database from its constituents
pub fn from_parts(data: Data, backend: Back, deser: DeSer) -> Database<Data, Back, DeSer> {
Database {
data: RwLock::new(data),
backend: Mutex::new(backend),
deser: deser,
}
}
/// Break a database into its individual parts
pub fn into_inner(self) -> error::Result<(Data, Back, DeSer)> {
Ok((self.data.into_inner().map_err(|_| error::RustbreakErrorKind::PoisonError)?,
self.backend.into_inner().map_err(|_| error::RustbreakErrorKind::PoisonError)?,
self.deser))
}
}
/// A database backed by a file