#![allow(async_fn_in_trait)]
cfg_if::cfg_if! {
if #[cfg(feature = "fs")] {
pub mod file;
pub use file::File;
}
}
pub mod memory;
use std::ops::Deref;
pub use memory::Memory;
#[cfg_attr(not(web), trait_variant::make(Send))]
pub trait Persist: Deref {
type Error: std::error::Error + Send + Sync + 'static;
fn as_mut(&mut self) -> &mut Self::Target;
async fn persist(&mut self) -> Result<(), Self::Error>;
fn into_value(self) -> Self::Target
where
Self::Target: Sized;
}
pub trait PersistExt: Persist {
async fn mutate<R>(
&mut self,
mutation: impl FnOnce(&mut Self::Target) -> R,
) -> Result<R, Self::Error>;
}
impl<T: Persist> PersistExt for T {
async fn mutate<R>(
&mut self,
mutation: impl FnOnce(&mut Self::Target) -> R,
) -> Result<R, Self::Error> {
let output = mutation(self.as_mut());
self.persist().await?;
Ok(output)
}
}