use crate::store::StoreBase;
use std::future::Future;
pub trait AsyncStoreRead: StoreBase {
fn get(&self, key: Self::Key) -> impl Future<Output = Option<Self::Value>>;
fn len(&self) -> impl Future<Output = usize>;
fn is_empty(&self) -> impl Future<Output = bool> {
async { self.len().await == 0 }
}
}
pub trait AsyncStoreWrite: StoreBase {
fn insert(&self, key: Self::Key, value: Self::Value) -> impl Future<Output = ()>;
fn delete(&self, key: Self::Key) -> impl Future<Output = bool>;
}
pub trait AsyncStore: AsyncStoreRead + AsyncStoreWrite {}
impl<T> AsyncStore for T where T: AsyncStoreRead + AsyncStoreWrite {}
pub trait AsyncOrderedStoreRead: AsyncStoreRead<Key: Ord> {
fn last(&self) -> impl Future<Output = Option<(Self::Key, Self::Value)>>;
}
pub trait AsyncOrderedStore: AsyncOrderedStoreRead + AsyncStoreWrite {}
impl<T> AsyncOrderedStore for T where T: AsyncOrderedStoreRead + AsyncStoreWrite {}
pub trait AsyncAppendableStore: AsyncOrderedStoreRead {
fn append(&self, value: Self::Value) -> impl Future<Output = Self::Key>;
}
pub trait AsyncSearchableStoreRead: AsyncOrderedStoreRead {
fn filter(
&self,
pred: impl FnMut(&Self::Key, &Self::Value) -> bool,
) -> impl Future<Output = Vec<(Self::Key, Self::Value)>>;
fn find(
&self,
mut pred: impl FnMut(&Self::Key, &Self::Value) -> bool,
) -> impl Future<Output = Option<(Self::Key, Self::Value)>> {
async move {
let mut found = false;
let vals = self
.filter(|key, value| {
if !found && pred(key, value) {
found = true;
true
} else {
false
}
})
.await;
if found {
assert_eq!(vals.len(), 1);
Some(vals.into_iter().next().unwrap())
} else {
None
}
}
}
}
pub trait AsyncSearchableStore: AsyncSearchableStoreRead + AsyncStoreWrite {}
impl<T> AsyncSearchableStore for T where T: AsyncSearchableStoreRead + AsyncStoreWrite {}