mirror of
https://github.com/Drop-OSS/dropbreak.git
synced 2026-07-19 20:43:35 -04:00
Add in-memory and fix docs
This commit is contained in:
+1
-6
@@ -24,20 +24,15 @@ version = "0.7.0"
|
||||
optional = true
|
||||
version = "0.8.0"
|
||||
|
||||
[dependencies.error-chain]
|
||||
optional = true
|
||||
version = "0.11.0"
|
||||
|
||||
[dependencies.serde_yaml]
|
||||
optional = true
|
||||
version = "0.7"
|
||||
|
||||
[dev-dependencies]
|
||||
lazy_static = "1.0.0"
|
||||
serde_derive = "1"
|
||||
tempfile = "2.1"
|
||||
|
||||
[features]
|
||||
bin = ["bincode", "base64", "error-chain"]
|
||||
bin = ["bincode", "base64"]
|
||||
yaml = ["serde_yaml"]
|
||||
ron_enc = ["ron"]
|
||||
|
||||
+4
-5
@@ -13,18 +13,17 @@ extern crate rustbreak;
|
||||
#[macro_use] extern crate serde_derive;
|
||||
#[macro_use] extern crate lazy_static;
|
||||
|
||||
use std::fs::File;
|
||||
use std::path::PathBuf;
|
||||
use std::default::Default;
|
||||
use rustbreak::Database;
|
||||
use rustbreak::FileDatabase;
|
||||
use rustbreak::deser::Yaml;
|
||||
|
||||
type DB = Database<Config, File, Yaml>;
|
||||
type DB = FileDatabase<Config, Yaml>;
|
||||
|
||||
lazy_static! {
|
||||
static ref CONFIG: DB = {
|
||||
let db = Database::from_path(Config::default(), "/tmp/config.yml").expect("Create database from path");
|
||||
let db = db.with_deser(Yaml);
|
||||
let db = FileDatabase::from_path(Config::default(), Yaml, "/tmp/config.yml")
|
||||
.expect("Create database from path");
|
||||
db.reload().expect("Config to load");
|
||||
db
|
||||
};
|
||||
|
||||
+39
-6
@@ -47,9 +47,9 @@
|
||||
//!
|
||||
//! ```rust
|
||||
//! # use std::collections::HashMap;
|
||||
//! use rustbreak::Database;
|
||||
//! use rustbreak::{MemoryDatabase, deser::Ron};
|
||||
//!
|
||||
//! let db = Database::<HashMap<String, String>>::memory(HashMap::new());
|
||||
//! let db = MemoryDatabase::<HashMap<String, String>, Ron>::memory(HashMap::new(), Ron);
|
||||
//!
|
||||
//! println!("Writing to Database");
|
||||
//! db.write(|db| {
|
||||
@@ -191,12 +191,11 @@ impl<Data, Back, DeSer> Database<Data, Back, DeSer>
|
||||
}
|
||||
|
||||
/// Read lock the database and get write access to the `Data` container
|
||||
pub fn read<T>(&self, task: T) -> error::Result<()>
|
||||
where T: FnOnce(&Data)
|
||||
pub fn read<T, R>(&self, task: T) -> error::Result<R>
|
||||
where T: FnOnce(&Data) -> R
|
||||
{
|
||||
let mut lock = self.data.read().map_err(|_| error::RustbreakErrorKind::PoisonError)?;
|
||||
task(&mut lock);
|
||||
Ok(())
|
||||
Ok(task(&mut lock))
|
||||
}
|
||||
|
||||
/// Reload the Data from the backend
|
||||
@@ -253,3 +252,37 @@ impl<Data, DeSer> Database<Data, FileBackend, DeSer>
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// An in memory backend
|
||||
///
|
||||
/// It is backed by a `Vec<u8>`
|
||||
pub struct MemoryBackend(Vec<u8>);
|
||||
|
||||
impl Backend for MemoryBackend {
|
||||
fn get_data(&mut self) -> error::Result<Vec<u8>> {
|
||||
Ok(self.0.clone())
|
||||
}
|
||||
|
||||
fn put_data(&mut self, data: &[u8]) -> error::Result<()> {
|
||||
self.0 = data.to_owned();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// A database backed by a file
|
||||
pub type MemoryDatabase<D, DS> = Database<D, MemoryBackend, DS>;
|
||||
|
||||
impl<Data, DeSer> Database<Data, MemoryBackend, DeSer>
|
||||
where
|
||||
Data: Serialize + DeserializeOwned + Debug + Clone + Send,
|
||||
DeSer: DeSerializer<Data> + Send + Sync
|
||||
{
|
||||
/// Create new FileDatabase from Path
|
||||
pub fn memory(data: Data, deser: DeSer) -> MemoryDatabase<Data, DeSer> {
|
||||
Database {
|
||||
data: RwLock::new(data),
|
||||
backend: Mutex::new(MemoryBackend(vec![])),
|
||||
deser: deser,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user