1#![allow(async_fn_in_trait)]
9
10mod dirty;
11use dirty::Dirty;
12
13cfg_if::cfg_if! {
14 if #[cfg(with_indexed_db)] {
15 pub mod indexed_db;
16 pub use indexed_db::IndexedDb;
17 }
18}
19
20cfg_if::cfg_if! {
21 if #[cfg(feature = "fs")] {
22 pub mod file;
23 pub use file::File;
24 }
25}
26
27pub mod memory;
28use std::ops::Deref;
29
30pub use memory::Memory;
31
32#[cfg_attr(not(web), trait_variant::make(Send))]
37pub trait Persist: Deref {
38 type Error: std::error::Error + Send + Sync + 'static;
39
40 fn as_mut(&mut self) -> &mut Self::Target;
44
45 async fn persist(&mut self) -> Result<(), Self::Error>;
47
48 fn into_value(self) -> Self::Target
50 where
51 Self::Target: Sized;
52}
53
54#[cfg_attr(not(web), trait_variant::make(Send))]
55pub trait PersistExt: Persist {
56 async fn mutate<R: Send>(
58 &mut self,
59 mutation: impl FnOnce(&mut Self::Target) -> R + Send,
60 ) -> Result<R, Self::Error>;
61}
62
63impl<T: Persist> PersistExt for T {
64 async fn mutate<R>(
65 &mut self,
66 mutation: impl FnOnce(&mut Self::Target) -> R + Send,
67 ) -> Result<R, Self::Error> {
68 let output = mutation(self.as_mut());
69 self.persist().await?;
70 Ok(output)
71 }
72}