Skip to main content

luct_store/
switch.rs

1use luct_core::store::{
2    AppendableStore, AsyncAppendableStore, AsyncOrderedStoreRead, AsyncSearchableStoreRead,
3    AsyncStoreRead, AsyncStoreWrite, OrderedStoreRead, SearchableStoreRead, StoreBase, StoreRead,
4    StoreWrite,
5};
6
7/// [`Store`](luct_core::store::Store) implementation that switches between two different
8/// inner [`Stores`](luct_core::store::Store).
9///
10/// This can be used, if you want to switch at runtime between the [`Stores`](luct_core::store::Store)
11/// [`A`](Self::A) and [`B`](Self::B),
12pub enum StoreSwitch<A, B> {
13    A(A),
14    B(B),
15}
16
17impl<A, B, K, V> StoreBase for StoreSwitch<A, B>
18where
19    A: StoreBase<Key = K, Value = V>,
20    B: StoreBase<Key = K, Value = V>,
21{
22    type Key = K;
23    type Value = V;
24}
25
26impl<A, B, K, V> StoreRead for StoreSwitch<A, B>
27where
28    A: StoreRead<Key = K, Value = V>,
29    B: StoreRead<Key = K, Value = V>,
30{
31    fn get(&self, key: &K) -> Option<V> {
32        match self {
33            StoreSwitch::A(a) => a.get(key),
34            StoreSwitch::B(b) => b.get(key),
35        }
36    }
37
38    fn len(&self) -> usize {
39        match self {
40            StoreSwitch::A(a) => a.len(),
41            StoreSwitch::B(b) => b.len(),
42        }
43    }
44}
45
46impl<A, B, K, V> StoreWrite for StoreSwitch<A, B>
47where
48    A: StoreWrite<Key = K, Value = V>,
49    B: StoreWrite<Key = K, Value = V>,
50{
51    fn insert(&self, key: K, value: V) {
52        match self {
53            StoreSwitch::A(a) => a.insert(key, value),
54            StoreSwitch::B(b) => b.insert(key, value),
55        }
56    }
57
58    fn delete(&self, key: &K) -> bool {
59        match self {
60            StoreSwitch::A(a) => a.delete(key),
61            StoreSwitch::B(b) => b.delete(key),
62        }
63    }
64}
65
66impl<A, B, K, V> OrderedStoreRead for StoreSwitch<A, B>
67where
68    K: Ord,
69    A: OrderedStoreRead<Key = K, Value = V>,
70    B: OrderedStoreRead<Key = K, Value = V>,
71{
72    fn last(&self) -> Option<(K, V)> {
73        match self {
74            StoreSwitch::A(a) => a.last(),
75            StoreSwitch::B(b) => b.last(),
76        }
77    }
78}
79
80impl<A, B, K, V> AppendableStore for StoreSwitch<A, B>
81where
82    K: Ord,
83    A: AppendableStore<Key = K, Value = V>,
84    B: AppendableStore<Key = K, Value = V>,
85{
86    fn append(&self, value: V) -> K {
87        match self {
88            StoreSwitch::A(a) => a.append(value),
89            StoreSwitch::B(b) => b.append(value),
90        }
91    }
92}
93
94impl<A, B, K, V> SearchableStoreRead for StoreSwitch<A, B>
95where
96    K: Ord,
97    A: SearchableStoreRead<Key = K, Value = V>,
98    B: SearchableStoreRead<Key = K, Value = V>,
99{
100    fn filter(&self, pred: impl FnMut(&K, &V) -> bool) -> Vec<(K, V)> {
101        match self {
102            StoreSwitch::A(a) => a.filter(pred),
103            StoreSwitch::B(b) => b.filter(pred),
104        }
105    }
106
107    fn find(&self, pred: impl FnMut(&K, &V) -> bool) -> Option<(K, V)> {
108        match self {
109            StoreSwitch::A(a) => a.find(pred),
110            StoreSwitch::B(b) => b.find(pred),
111        }
112    }
113}
114
115impl<A, B, K, V> AsyncStoreRead for StoreSwitch<A, B>
116where
117    A: AsyncStoreRead<Key = K, Value = V>,
118    B: AsyncStoreRead<Key = K, Value = V>,
119{
120    async fn get(&self, key: K) -> Option<V> {
121        match self {
122            StoreSwitch::A(a) => a.get(key).await,
123            StoreSwitch::B(b) => b.get(key).await,
124        }
125    }
126
127    async fn len(&self) -> usize {
128        match self {
129            StoreSwitch::A(a) => a.len().await,
130            StoreSwitch::B(b) => b.len().await,
131        }
132    }
133}
134
135impl<A, B, K, V> AsyncStoreWrite for StoreSwitch<A, B>
136where
137    A: AsyncStoreWrite<Key = K, Value = V>,
138    B: AsyncStoreWrite<Key = K, Value = V>,
139{
140    async fn insert(&self, key: K, value: V) {
141        match self {
142            StoreSwitch::A(a) => a.insert(key, value).await,
143            StoreSwitch::B(b) => b.insert(key, value).await,
144        }
145    }
146
147    async fn delete(&self, key: K) -> bool {
148        match self {
149            StoreSwitch::A(a) => a.delete(key).await,
150            StoreSwitch::B(b) => b.delete(key).await,
151        }
152    }
153}
154
155impl<A, B, K, V> AsyncOrderedStoreRead for StoreSwitch<A, B>
156where
157    K: Ord,
158    A: AsyncOrderedStoreRead<Key = K, Value = V>,
159    B: AsyncOrderedStoreRead<Key = K, Value = V>,
160{
161    async fn last(&self) -> Option<(K, V)> {
162        match self {
163            StoreSwitch::A(a) => a.last().await,
164            StoreSwitch::B(b) => b.last().await,
165        }
166    }
167}
168
169impl<A, B, K, V> AsyncAppendableStore for StoreSwitch<A, B>
170where
171    K: Ord,
172    A: AsyncAppendableStore<Key = K, Value = V>,
173    B: AsyncAppendableStore<Key = K, Value = V>,
174{
175    async fn append(&self, value: V) -> K {
176        match self {
177            StoreSwitch::A(a) => a.append(value).await,
178            StoreSwitch::B(b) => b.append(value).await,
179        }
180    }
181}
182
183impl<A, B, K, V> AsyncSearchableStoreRead for StoreSwitch<A, B>
184where
185    K: Ord,
186    A: AsyncSearchableStoreRead<Key = K, Value = V>,
187    B: AsyncSearchableStoreRead<Key = K, Value = V>,
188{
189    async fn filter(&self, pred: impl FnMut(&K, &V) -> bool) -> Vec<(K, V)> {
190        match self {
191            StoreSwitch::A(a) => a.filter(pred).await,
192            StoreSwitch::B(b) => b.filter(pred).await,
193        }
194    }
195
196    async fn find(&self, pred: impl FnMut(&K, &V) -> bool) -> Option<(K, V)> {
197        match self {
198            StoreSwitch::A(a) => a.find(pred).await,
199            StoreSwitch::B(b) => b.find(pred).await,
200        }
201    }
202}