commonware_storage/
store.rs1use std::future::Future;
4
5pub trait Store {
7 type Key;
8 type Value;
9 type Error;
10
11 fn get(
13 &self,
14 key: &Self::Key,
15 ) -> impl Future<Output = Result<Option<Self::Value>, Self::Error>>;
16}
17
18pub trait StoreMut: Store {
20 fn update(
22 &mut self,
23 key: Self::Key,
24 value: Self::Value,
25 ) -> impl Future<Output = Result<(), Self::Error>>;
26
27 fn upsert(
30 &mut self,
31 key: Self::Key,
32 update: impl FnOnce(&mut Self::Value),
33 ) -> impl Future<Output = Result<(), Self::Error>>
34 where
35 Self::Value: Default,
36 {
37 async {
38 let mut value = self.get(&key).await?.unwrap_or_default();
39 update(&mut value);
40
41 self.update(key, value).await
42 }
43 }
44
45 fn create(
48 &mut self,
49 key: Self::Key,
50 value: Self::Value,
51 ) -> impl Future<Output = Result<bool, Self::Error>> {
52 async {
53 if self.get(&key).await?.is_some() {
54 return Ok(false);
55 }
56
57 self.update(key, value).await?;
58 Ok(true)
59 }
60 }
61}
62
63pub trait StoreDeletable: StoreMut {
65 fn delete(&mut self, key: Self::Key) -> impl Future<Output = Result<bool, Self::Error>>;
69}
70
71pub trait StorePersistable: StoreMut {
73 fn commit(&mut self) -> impl Future<Output = Result<(), Self::Error>>;
75
76 fn destroy(self) -> impl Future<Output = Result<(), Self::Error>>;
78}