use crate::{DataResult, DataStore, Equivalent};
use serde_json::Value;
use std::io::Result;
use std::sync::Mutex;
pub struct TransientDB<T> {
#[cfg(not(target_arch = "wasm32"))]
store: Mutex<Box<dyn DataStore<Output = T> + Send>>,
#[cfg(target_arch = "wasm32")]
store: Mutex<Box<dyn DataStore<Output = T>>>,
}
#[cfg(target_arch = "wasm32")]
unsafe impl<T> Send for TransientDB<T> {}
#[cfg(target_arch = "wasm32")]
unsafe impl<T> Sync for TransientDB<T> {}
impl<T> TransientDB<T> {
#[cfg(not(target_arch = "wasm32"))]
pub fn new(store: impl DataStore<Output = T> + Send + 'static) -> Self {
Self {
store: Mutex::new(Box::new(store)),
}
}
#[cfg(target_arch = "wasm32")]
pub fn new(store: impl DataStore<Output = T> + 'static) -> Self {
Self {
store: Mutex::new(Box::new(store)),
}
}
pub fn has_data(&self) -> bool {
self.store.lock().unwrap().has_data()
}
pub fn reset(&self) {
self.store.lock().unwrap().reset();
}
pub fn append(&self, data: Value) -> Result<()> {
self.store.lock().unwrap().append(data)
}
pub fn fetch(
&self,
count: Option<usize>,
max_bytes: Option<usize>,
) -> Result<Option<DataResult<T>>> {
self.store.lock().unwrap().fetch(count, max_bytes)
}
pub fn remove(&self, data: &[Box<dyn Equivalent>]) -> Result<()> {
self.store.lock().unwrap().remove(data)
}
}