anystore/store.rs
1use crate::{
2 address::{primitive::UniqueRootAddress, *},
3 location::Location,
4};
5
6// pub mod list;
7// pub mod tree;
8
9/// Main store driver
10///
11/// This and the related traits are what you need to implement
12/// if you want to add a store.
13pub trait Store: Clone {
14 type Error: std::fmt::Debug + std::fmt::Display + Send + Sync + 'static;
15 type RootAddress: Address + From<UniqueRootAddress> = UniqueRootAddress;
16}
17
18pub trait StoreEx<Root: Address + From<UniqueRootAddress>>: Store {
19 fn sub<Addr: Address>(&self, addr: Addr) -> Location<Addr, Self>
20 where
21 Self: Addressable<Addr>,
22 {
23 Location::new(addr, self.clone())
24 }
25
26 fn root(&self) -> Location<Root, Self>
27 where
28 Self: Addressable<Root>,
29 {
30 Location::new(UniqueRootAddress.into(), self.clone())
31 }
32
33 fn path<Addr: Address>(&self, p: &str) -> StoreResult<Location<Addr, Self>, Self>
34 where
35 Self: Addressable<Root> + Addressable<Addr>,
36 Root: PathAddress<Output = Addr>,
37 <Self as Store>::Error: From<<Root as PathAddress>::Error>, // Root::Error: From<Self::Error> + Into<Self::Error>,
38 {
39 self.root().path(p)
40 }
41}
42
43impl<S: Store> StoreEx<S::RootAddress> for S {}
44
45pub type StoreResult<V, S> = Result<V, <S as Store>::Error>;
46
47// pub struct SharedStore<S: Store> {
48// store: Arc<Mutex<S>>,
49// }
50
51// impl<S: Store> S {}
52// impl<S: Store> SharedStore<S> {
53// pub fn new(store: S) -> Self {
54// SharedStore {
55// store: Arc::new(Mutex::new(store)),
56// }
57// }
58
59// pub fn root(&self) -> Location<OpaqueValue, UniqueRootAddress, S> {
60// Location::new(UniqueRootAddress, self.store.clone())
61// }
62// pub fn sub<A: AddressFor<V, S>, V>(&self, addr: A) -> Location<V, A, S> {
63// self.root().sub(addr)
64// }
65// }
66
67// pub trait ReadStore<A: AddressFor<V, Self>, V>: Store {
68// async fn _read(&self, addr: A) -> StoreResult<Option<V>, Self>;
69// }
70// pub trait WriteStore<A: AddressFor<V, Self>, V>: Store {
71// async fn _write(&self, value: Option<V>, addr: A) -> StoreResult<(), Self>;
72// }