Skip to main content

luct_store/
switch.rs

1use luct_core::store::{
2    AppendableStore, OrderedStoreRead, SearchableStoreRead, StoreBase, StoreRead, StoreWrite,
3};
4
5/// [`Store`](luct_core::store::Store) implementation that switches between two different
6/// inner [`Stores`](luct_core::store::Store).
7///
8/// This can be used, if you want to switch at runtime between the [`Stores`](luct_core::store::Store)
9/// [`A`](Self::A) and [`B`](Self::B),
10pub enum StoreSwitch<A, B> {
11    A(A),
12    B(B),
13}
14
15impl<A, B, K, V> StoreBase for StoreSwitch<A, B>
16where
17    A: StoreBase<Key = K, Value = V>,
18    B: StoreBase<Key = K, Value = V>,
19{
20    type Key = K;
21    type Value = V;
22}
23
24impl<A, B, K, V> StoreRead for StoreSwitch<A, B>
25where
26    A: StoreRead<Key = K, Value = V>,
27    B: StoreRead<Key = K, Value = V>,
28{
29    fn get(&self, key: &K) -> Option<V> {
30        match self {
31            StoreSwitch::A(a) => a.get(key),
32            StoreSwitch::B(b) => b.get(key),
33        }
34    }
35
36    fn len(&self) -> usize {
37        match self {
38            StoreSwitch::A(a) => a.len(),
39            StoreSwitch::B(b) => b.len(),
40        }
41    }
42}
43
44impl<A, B, K, V> StoreWrite for StoreSwitch<A, B>
45where
46    A: StoreWrite<Key = K, Value = V>,
47    B: StoreWrite<Key = K, Value = V>,
48{
49    fn insert(&self, key: K, value: V) {
50        match self {
51            StoreSwitch::A(a) => a.insert(key, value),
52            StoreSwitch::B(b) => b.insert(key, value),
53        }
54    }
55
56    fn delete(&self, key: &K) -> bool {
57        match self {
58            StoreSwitch::A(a) => a.delete(key),
59            StoreSwitch::B(b) => b.delete(key),
60        }
61    }
62}
63
64impl<A, B, K, V> OrderedStoreRead for StoreSwitch<A, B>
65where
66    K: Ord,
67    A: OrderedStoreRead<Key = K, Value = V>,
68    B: OrderedStoreRead<Key = K, Value = V>,
69{
70    fn last(&self) -> Option<(K, V)> {
71        match self {
72            StoreSwitch::A(a) => a.last(),
73            StoreSwitch::B(b) => b.last(),
74        }
75    }
76}
77
78impl<A, B, K, V> AppendableStore for StoreSwitch<A, B>
79where
80    K: Ord,
81    A: AppendableStore<Key = K, Value = V>,
82    B: AppendableStore<Key = K, Value = V>,
83{
84    fn append(&self, value: V) -> K {
85        match self {
86            StoreSwitch::A(a) => a.append(value),
87            StoreSwitch::B(b) => b.append(value),
88        }
89    }
90}
91
92impl<A, B, K, V> SearchableStoreRead for StoreSwitch<A, B>
93where
94    K: Ord,
95    A: SearchableStoreRead<Key = K, Value = V>,
96    B: SearchableStoreRead<Key = K, Value = V>,
97{
98    fn filter(&self, pred: impl FnMut(&K, &V) -> bool) -> Vec<(K, V)> {
99        match self {
100            StoreSwitch::A(a) => a.filter(pred),
101            StoreSwitch::B(b) => b.filter(pred),
102        }
103    }
104
105    fn find(&self, pred: impl FnMut(&K, &V) -> bool) -> Option<(K, V)> {
106        match self {
107            StoreSwitch::A(a) => a.find(pred),
108            StoreSwitch::B(b) => b.find(pred),
109        }
110    }
111}