Skip to main content

luct_core/store/
async_adapter.rs

1use crate::store::{
2    AsyncOrderedStoreRead, AsyncSearchableStoreRead, AsyncStoreRead, AsyncStoreWrite,
3    OrderedStoreRead, SearchableStoreRead, StoreBase, StoreRead, StoreWrite,
4};
5
6pub struct AsyncAdapter<S>(S);
7
8impl<S> AsyncAdapter<S> {
9    pub fn new(store: S) -> Self {
10        Self(store)
11    }
12}
13
14impl<S: StoreBase> StoreBase for AsyncAdapter<S> {
15    type Key = S::Key;
16    type Value = S::Value;
17}
18
19impl<S: StoreRead> AsyncStoreRead for AsyncAdapter<S> {
20    async fn get(&self, key: Self::Key) -> Option<Self::Value> {
21        self.0.get(&key)
22    }
23
24    async fn len(&self) -> usize {
25        self.0.len()
26    }
27}
28
29impl<S: StoreWrite> AsyncStoreWrite for AsyncAdapter<S> {
30    async fn insert(&self, key: Self::Key, value: Self::Value) {
31        self.0.insert(key, value);
32    }
33
34    async fn delete(&self, key: Self::Key) -> bool {
35        self.0.delete(&key)
36    }
37}
38
39impl<S: OrderedStoreRead> AsyncOrderedStoreRead for AsyncAdapter<S> {
40    async fn last(&self) -> Option<(Self::Key, Self::Value)> {
41        self.0.last()
42    }
43}
44
45impl<S: SearchableStoreRead> AsyncSearchableStoreRead for AsyncAdapter<S> {
46    async fn filter(
47        &self,
48        pred: impl FnMut(&Self::Key, &Self::Value) -> bool,
49    ) -> Vec<(Self::Key, Self::Value)> {
50        self.0.filter(pred)
51    }
52
53    async fn find(
54        &self,
55        pred: impl FnMut(&Self::Key, &Self::Value) -> bool,
56    ) -> Option<(Self::Key, Self::Value)> {
57        self.0.find(pred)
58    }
59}