mirror of
https://github.com/Drop-OSS/dropbreak.git
synced 2026-07-18 09:57:53 -04:00
Add into_inner and from_parts for the database
This commit is contained in:
@@ -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
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user