basecoin_store/impls/
shared.rs1use core::ops::{Deref, DerefMut};
2use std::sync::{Arc, RwLock};
3
4use ics23::CommitmentProof;
5
6use crate::context::{ProvableStore, Store};
7use crate::types::{Height, Path, RawHeight};
8use crate::utils::{SharedRw, SharedRwExt};
9
10#[derive(Clone, Debug)]
12pub struct SharedStore<S>(SharedRw<S>);
13
14impl<S> SharedStore<S> {
15 pub fn new(store: S) -> Self {
16 Self(Arc::new(RwLock::new(store)))
17 }
18
19 pub fn share(&self) -> Self {
20 Self(self.0.clone())
21 }
22}
23
24impl<S> Default for SharedStore<S>
25where
26 S: Default + Store,
27{
28 fn default() -> Self {
29 Self::new(S::default())
30 }
31}
32
33impl<S> Store for SharedStore<S>
34where
35 S: Store,
36{
37 type Error = S::Error;
38
39 #[inline]
40 fn set(&mut self, path: Path, value: Vec<u8>) -> Result<Option<Vec<u8>>, Self::Error> {
41 self.write_access().set(path, value)
42 }
43
44 #[inline]
45 fn get(&self, height: Height, path: &Path) -> Option<Vec<u8>> {
46 self.read_access().get(height, path)
47 }
48
49 #[inline]
50 fn delete(&mut self, path: &Path) {
51 self.write_access().delete(path)
52 }
53
54 #[inline]
55 fn commit(&mut self) -> Result<Vec<u8>, Self::Error> {
56 self.write_access().commit()
57 }
58
59 #[inline]
60 fn apply(&mut self) -> Result<(), Self::Error> {
61 self.write_access().apply()
62 }
63
64 #[inline]
65 fn reset(&mut self) {
66 self.write_access().reset()
67 }
68
69 #[inline]
70 fn current_height(&self) -> RawHeight {
71 self.read_access().current_height()
72 }
73
74 #[inline]
75 fn get_keys(&self, key_prefix: &Path) -> Vec<Path> {
76 self.read_access().get_keys(key_prefix)
77 }
78}
79
80impl<S> ProvableStore for SharedStore<S>
81where
82 S: ProvableStore,
83{
84 #[inline]
85 fn root_hash(&self) -> Vec<u8> {
86 self.read_access().root_hash()
87 }
88
89 #[inline]
90 fn get_proof(&self, height: Height, key: &Path) -> Option<CommitmentProof> {
91 self.read_access().get_proof(height, key)
92 }
93}
94
95impl<S> Deref for SharedStore<S> {
96 type Target = Arc<RwLock<S>>;
97
98 fn deref(&self) -> &Self::Target {
99 &self.0
100 }
101}
102
103impl<S> DerefMut for SharedStore<S> {
104 fn deref_mut(&mut self) -> &mut Self::Target {
105 &mut self.0
106 }
107}