use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use tokio::sync::RwLock;
use super::{GeneratorState, Storage, StorageLoadResult, StorageSaveResult};
#[derive(Debug, Default)]
pub struct MemoryStorage {
state: Arc<RwLock<GeneratorState>>,
}
impl MemoryStorage {
pub fn new() -> Self {
Self::default()
}
pub fn with_state(state: GeneratorState) -> Self {
Self {
state: Arc::new(RwLock::new(state)),
}
}
}
impl Storage for MemoryStorage {
fn load(&self) -> Pin<Box<dyn Future<Output = StorageLoadResult> + Send + '_>> {
Box::pin(async {
let state = self.state.read().await;
Ok(*state)
})
}
fn save(
&self,
state: GeneratorState,
) -> Pin<Box<dyn Future<Output = StorageSaveResult> + Send + '_>> {
Box::pin(async move {
let mut current = self.state.write().await;
*current = state;
Ok(())
})
}
}