use core::{borrow::Borrow, future::Future, hash::Hash, ops::RangeBounds};
use super::{
sync::*,
types::{Entry, EntryValue},
};
pub struct AsyncMarker<'a, C> {
marker: &'a mut C,
}
impl<'a, C> AsyncMarker<'a, C> {
#[inline]
pub fn new(marker: &'a mut C) -> Self {
Self { marker }
}
}
impl<'a, C: AsyncCm> AsyncMarker<'a, C> {
pub async fn mark(&mut self, k: &C::Key) {
self.marker.mark_read(k).await;
}
pub async fn mark_conflict(&mut self, k: &C::Key) {
self.marker.mark_conflict(k).await;
}
}
impl<'a, C: AsyncCmRange> AsyncMarker<'a, C> {
pub async fn mark_range(&mut self, range: impl RangeBounds<C::Key>) {
self.marker.mark_range(range).await;
}
}
impl<'a, C: CmRange> AsyncMarker<'a, C> {
pub fn mark_range_blocking(&mut self, range: impl RangeBounds<C::Key>) {
self.marker.mark_range(range);
}
}
impl<'a, C: AsyncCmEquivalentRange> AsyncMarker<'a, C> {
pub async fn mark_range_equivalent<Q>(&mut self, range: impl RangeBounds<Q>)
where
C::Key: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self.marker.mark_range_equivalent(range).await;
}
}
impl<'a, C: CmEquivalentRange> AsyncMarker<'a, C> {
pub fn mark_range_equivalent_blocking<Q>(&mut self, range: impl RangeBounds<Q>)
where
C::Key: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self.marker.mark_range_equivalent(range);
}
}
impl<'a, C: AsyncCmComparableRange> AsyncMarker<'a, C> {
pub async fn mark_range_comparable<Q>(&mut self, range: impl RangeBounds<Q>)
where
C::Key: Borrow<Q>,
Q: Ord + ?Sized,
{
self.marker.mark_range_comparable(range).await;
}
}
impl<'a, C: CmComparableRange> AsyncMarker<'a, C> {
pub fn mark_range_comparable_blocking<Q>(&mut self, range: impl RangeBounds<Q>)
where
C::Key: Borrow<Q>,
Q: Ord + ?Sized,
{
self.marker.mark_range_comparable(range);
}
}
impl<'a, C: AsyncCmComparable> AsyncMarker<'a, C> {
pub async fn mark_comparable<Q>(&mut self, k: &Q)
where
C::Key: Borrow<Q>,
Q: Ord + ?Sized,
{
self.marker.mark_read_comparable(k).await;
}
pub async fn mark_conflict_comparable<Q>(&mut self, k: &Q)
where
C::Key: Borrow<Q>,
Q: Ord + ?Sized,
{
self.marker.mark_conflict_comparable(k).await;
}
}
impl<'a, C: AsyncCmEquivalent> AsyncMarker<'a, C> {
pub async fn mark_equivalent<Q>(&mut self, k: &Q)
where
C::Key: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self.marker.mark_read_equivalent(k).await;
}
pub async fn mark_conflict_equivalent<Q>(&mut self, k: &Q)
where
C::Key: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self.marker.mark_conflict_equivalent(k).await;
}
}
impl<'a, C: Cm> AsyncMarker<'a, C> {
pub fn mark_blocking(&mut self, k: &C::Key) {
self.marker.mark_read(k);
}
pub fn mark_conflict_blocking(&mut self, k: &C::Key) {
self.marker.mark_conflict(k);
}
}
impl<'a, C: CmComparable> AsyncMarker<'a, C> {
pub fn mark_comparable_blocking<Q>(&mut self, k: &Q)
where
C::Key: Borrow<Q>,
Q: Ord + ?Sized,
{
self.marker.mark_read_comparable(k);
}
pub fn mark_conflict_comparable_blocking<Q>(&mut self, k: &Q)
where
C::Key: Borrow<Q>,
Q: Ord + ?Sized,
{
self.marker.mark_conflict_comparable(k);
}
}
impl<'a, C: CmEquivalent> AsyncMarker<'a, C> {
pub fn mark_equivalent_blocking<Q>(&mut self, k: &Q)
where
C::Key: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self.marker.mark_read_equivalent(k);
}
pub fn mark_conflict_equivalent_blocking<Q>(&mut self, k: &Q)
where
C::Key: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self.marker.mark_conflict_equivalent(k);
}
}
pub trait AsyncCm: Sized {
type Error: crate::error::Error;
type Key;
type Options;
fn new(options: Self::Options) -> impl Future<Output = Result<Self, Self::Error>>;
fn mark_read(&mut self, key: &Self::Key) -> impl Future<Output = ()>;
fn mark_conflict(&mut self, key: &Self::Key) -> impl Future<Output = ()>;
fn has_conflict(&self, other: &Self) -> impl Future<Output = bool>;
fn rollback(&mut self) -> impl Future<Output = Result<(), Self::Error>>;
}
pub trait AsyncCmRange: Cm + Sized {
fn mark_range(&mut self, range: impl RangeBounds<<Self as Cm>::Key>) -> impl Future<Output = ()>;
}
pub trait AsyncCmEquivalentRange: AsyncCmRange + Sized {
fn mark_range_equivalent<Q>(&mut self, range: impl RangeBounds<Q>) -> impl Future<Output = ()>
where
Self::Key: Borrow<Q>,
Q: Hash + Eq + ?Sized;
}
pub trait AsyncCmComparableRange: AsyncCmRange + CmComparable {
fn mark_range_comparable<Q>(&mut self, range: impl RangeBounds<Q>) -> impl Future<Output = ()>
where
Self::Key: Borrow<Q>,
Q: Ord + ?Sized;
}
pub trait AsyncCmEquivalent: AsyncCm {
fn mark_read_equivalent<Q>(&mut self, key: &Q) -> impl Future<Output = ()>
where
Self::Key: Borrow<Q>,
Q: Hash + Eq + ?Sized;
fn mark_conflict_equivalent<Q>(&mut self, key: &Q) -> impl Future<Output = ()>
where
Self::Key: Borrow<Q>,
Q: Hash + Eq + ?Sized;
}
pub trait AsyncCmComparable: AsyncCm {
fn mark_read_comparable<Q>(&mut self, key: &Q) -> impl Future<Output = ()>
where
Self::Key: Borrow<Q>,
Q: Ord + ?Sized;
fn mark_conflict_comparable<Q>(&mut self, key: &Q) -> impl Future<Output = ()>
where
Self::Key: Borrow<Q>,
Q: Ord + ?Sized;
}
pub trait AsyncPwm: Sized {
type Error: crate::error::Error;
type Key;
type Value;
type Options;
type Iter<'a>: Iterator<Item = (&'a Self::Key, &'a EntryValue<Self::Value>)>
where
Self: 'a;
type IntoIter: Iterator<Item = (Self::Key, EntryValue<Self::Value>)>;
fn new(options: Self::Options) -> impl Future<Output = Result<Self, Self::Error>>;
fn is_empty(&self) -> impl Future<Output = bool>;
fn len(&self) -> impl Future<Output = usize>;
fn validate_entry(
&self,
entry: &Entry<Self::Key, Self::Value>,
) -> impl Future<Output = Result<(), Self::Error>>;
fn max_batch_size(&self) -> u64;
fn max_batch_entries(&self) -> u64;
fn estimate_size(&self, entry: &Entry<Self::Key, Self::Value>) -> u64;
fn get(
&self,
key: &Self::Key,
) -> impl Future<Output = Result<Option<&EntryValue<Self::Value>>, Self::Error>>;
fn get_entry(
&self,
key: &Self::Key,
) -> impl Future<Output = Result<Option<(&Self::Key, &EntryValue<Self::Value>)>, Self::Error>>;
fn contains_key(&self, key: &Self::Key) -> impl Future<Output = Result<bool, Self::Error>>;
fn insert(
&mut self,
key: Self::Key,
value: EntryValue<Self::Value>,
) -> impl Future<Output = Result<(), Self::Error>>;
fn remove_entry(
&mut self,
key: &Self::Key,
) -> impl Future<Output = Result<Option<(Self::Key, EntryValue<Self::Value>)>, Self::Error>>;
fn rollback(&mut self) -> impl Future<Output = Result<(), Self::Error>>;
fn iter(&self) -> impl Future<Output = Self::Iter<'_>>;
fn into_iter(self) -> impl Future<Output = Self::IntoIter>;
}
pub trait AsyncPwmRange: AsyncPwm {
type Range<'a>: IntoIterator<Item = (&'a Self::Key, &'a EntryValue<Self::Value>)>
where
Self: 'a;
fn range<R: RangeBounds<Self::Key>>(&self, range: R) -> impl Future<Output = Self::Range<'_>>;
}
pub trait AsyncPwmComparableRange: AsyncPwmRange + AsyncPwmComparable {
fn range_comparable<T, R>(&self, range: R) -> impl Future<Output = Self::Range<'_>>
where
T: ?Sized + Ord,
Self::Key: Borrow<T> + Ord,
R: RangeBounds<T>;
}
pub trait AsyncPwmEquivalentRange: AsyncPwmRange + AsyncPwmEquivalent {
fn range_equivalent<T, R>(&self, range: R) -> impl Future<Output = Self::Range<'_>>
where
T: ?Sized + Eq + Hash,
Self::Key: Borrow<T> + Eq + Hash,
R: RangeBounds<T>;
}
pub trait AsyncPwmEquivalent: AsyncPwm {
fn get_equivalent<Q>(
&self,
key: &Q,
) -> impl Future<Output = Result<Option<&EntryValue<Self::Value>>, Self::Error>>
where
Self::Key: Borrow<Q>,
Q: Hash + Eq + ?Sized;
fn get_entry_equivalent<Q>(
&self,
key: &Q,
) -> impl Future<Output = Result<Option<(&Self::Key, &EntryValue<Self::Value>)>, Self::Error>>
where
Self::Key: Borrow<Q>,
Q: Hash + Eq + ?Sized;
fn contains_key_equivalent<Q>(&self, key: &Q) -> impl Future<Output = Result<bool, Self::Error>>
where
Self::Key: Borrow<Q>,
Q: Hash + Eq + ?Sized;
fn remove_entry_equivalent<Q>(
&mut self,
key: &Q,
) -> impl Future<Output = Result<Option<(Self::Key, EntryValue<Self::Value>)>, Self::Error>>
where
Self::Key: Borrow<Q>,
Q: Hash + Eq + ?Sized;
}
pub trait AsyncPwmComparable: AsyncPwm {
fn get_comparable<Q>(
&self,
key: &Q,
) -> impl Future<Output = Result<Option<&EntryValue<Self::Value>>, Self::Error>>
where
Self::Key: Borrow<Q>,
Q: Ord + ?Sized;
fn get_entry_comparable<Q>(
&self,
key: &Q,
) -> impl Future<Output = Result<Option<(&Self::Key, &EntryValue<Self::Value>)>, Self::Error>>
where
Self::Key: Borrow<Q>,
Q: Ord + ?Sized;
fn contains_key_comparable<Q>(&self, key: &Q) -> impl Future<Output = Result<bool, Self::Error>>
where
Self::Key: Borrow<Q>,
Q: Ord + ?Sized;
fn remove_entry_comparable<Q>(
&mut self,
key: &Q,
) -> impl Future<Output = Result<Option<(Self::Key, EntryValue<Self::Value>)>, Self::Error>>
where
Self::Key: Borrow<Q>,
Q: Ord + ?Sized;
}
impl<T> AsyncCm for T
where
T: Cm,
{
type Error = <T as Cm>::Error;
type Key = <T as Cm>::Key;
type Options = <T as Cm>::Options;
async fn new(options: Self::Options) -> Result<Self, Self::Error> {
<T as Cm>::new(options)
}
async fn mark_read(&mut self, key: &Self::Key) {
<T as Cm>::mark_read(self, key)
}
async fn mark_conflict(&mut self, key: &Self::Key) {
<T as Cm>::mark_conflict(self, key)
}
async fn has_conflict(&self, other: &Self) -> bool {
<T as Cm>::has_conflict(self, other)
}
async fn rollback(&mut self) -> Result<(), Self::Error> {
<T as Cm>::rollback(self)
}
}
impl<T> AsyncCmComparable for T
where
T: CmComparable,
{
async fn mark_read_comparable<Q>(&mut self, key: &Q)
where
Self::Key: Borrow<Q>,
Q: Ord + ?Sized,
{
<T as CmComparable>::mark_read_comparable(self, key)
}
async fn mark_conflict_comparable<Q>(&mut self, key: &Q)
where
Self::Key: Borrow<Q>,
Q: Ord + ?Sized,
{
<T as CmComparable>::mark_conflict_comparable(self, key)
}
}
impl<T> AsyncCmEquivalent for T
where
T: CmEquivalent,
{
async fn mark_read_equivalent<Q>(&mut self, key: &Q)
where
Self::Key: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
<T as CmEquivalent>::mark_read_equivalent(self, key)
}
async fn mark_conflict_equivalent<Q>(&mut self, key: &Q)
where
Self::Key: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
<T as CmEquivalent>::mark_conflict_equivalent(self, key)
}
}
impl<T> AsyncPwm for T
where
T: Pwm,
{
type Error = <T as Pwm>::Error;
type Key = <T as Pwm>::Key;
type Value = <T as Pwm>::Value;
type Options = <T as Pwm>::Options;
type Iter<'a> = <T as Pwm>::Iter<'a> where Self: 'a;
type IntoIter = <T as Pwm>::IntoIter;
async fn new(options: Self::Options) -> Result<Self, Self::Error> {
<T as Pwm>::new(options)
}
async fn is_empty(&self) -> bool {
<T as Pwm>::is_empty(self)
}
async fn len(&self) -> usize {
<T as Pwm>::len(self)
}
async fn validate_entry(&self, entry: &Entry<Self::Key, Self::Value>) -> Result<(), Self::Error> {
<T as Pwm>::validate_entry(self, entry)
}
fn max_batch_size(&self) -> u64 {
<T as Pwm>::max_batch_size(self)
}
fn max_batch_entries(&self) -> u64 {
<T as Pwm>::max_batch_entries(self)
}
fn estimate_size(&self, entry: &Entry<Self::Key, Self::Value>) -> u64 {
<T as Pwm>::estimate_size(self, entry)
}
async fn get(&self, key: &Self::Key) -> Result<Option<&EntryValue<Self::Value>>, Self::Error> {
<T as Pwm>::get(self, key)
}
async fn get_entry(
&self,
key: &Self::Key,
) -> Result<Option<(&Self::Key, &EntryValue<Self::Value>)>, Self::Error> {
<T as Pwm>::get_entry(self, key)
}
async fn contains_key(&self, key: &Self::Key) -> Result<bool, Self::Error> {
<T as Pwm>::contains_key(self, key)
}
async fn insert(
&mut self,
key: Self::Key,
value: EntryValue<Self::Value>,
) -> Result<(), Self::Error> {
<T as Pwm>::insert(self, key, value)
}
async fn remove_entry(
&mut self,
key: &Self::Key,
) -> Result<Option<(Self::Key, EntryValue<Self::Value>)>, Self::Error> {
<T as Pwm>::remove_entry(self, key)
}
async fn rollback(&mut self) -> Result<(), Self::Error> {
<T as Pwm>::rollback(self)
}
async fn iter(&self) -> Self::Iter<'_> {
<T as Pwm>::iter(self)
}
async fn into_iter(self) -> Self::IntoIter {
<T as Pwm>::into_iter(self)
}
}
impl<T> AsyncPwmRange for T
where
T: PwmRange,
{
type Range<'a> = <T as PwmRange>::Range<'a> where Self: 'a;
async fn range<R: RangeBounds<Self::Key>>(&self, range: R) -> Self::Range<'_> {
<T as PwmRange>::range(self, range)
}
}
impl<C> AsyncPwmComparableRange for C
where
C: PwmComparableRange,
{
async fn range_comparable<T, R>(&self, range: R) -> Self::Range<'_>
where
T: ?Sized + Ord,
Self::Key: Borrow<T> + Ord,
R: RangeBounds<T>,
{
<C as PwmComparableRange>::range_comparable(self, range)
}
}
impl<C> AsyncPwmEquivalentRange for C
where
C: PwmEquivalentRange,
{
async fn range_equivalent<T, R>(&self, range: R) -> Self::Range<'_>
where
T: ?Sized + Eq + Hash,
Self::Key: Borrow<T> + Eq + Hash,
R: RangeBounds<T>,
{
<C as PwmEquivalentRange>::range_equivalent(self, range)
}
}
impl<T> AsyncPwmComparable for T
where
T: PwmComparable,
{
async fn get_comparable<Q>(
&self,
key: &Q,
) -> Result<Option<&EntryValue<Self::Value>>, Self::Error>
where
Self::Key: Borrow<Q>,
Q: Ord + ?Sized,
{
<T as PwmComparable>::get_comparable(self, key)
}
async fn get_entry_comparable<Q>(
&self,
key: &Q,
) -> Result<Option<(&Self::Key, &EntryValue<Self::Value>)>, Self::Error>
where
Self::Key: Borrow<Q>,
Q: Ord + ?Sized,
{
<T as PwmComparable>::get_entry_comparable(self, key)
}
async fn contains_key_comparable<Q>(&self, key: &Q) -> Result<bool, Self::Error>
where
Self::Key: Borrow<Q>,
Q: Ord + ?Sized,
{
<T as PwmComparable>::contains_key_comparable(self, key)
}
async fn remove_entry_comparable<Q>(
&mut self,
key: &Q,
) -> Result<Option<(Self::Key, EntryValue<Self::Value>)>, Self::Error>
where
Self::Key: Borrow<Q>,
Q: Ord + ?Sized,
{
<T as PwmComparable>::remove_entry_comparable(self, key)
}
}
impl<T> AsyncPwmEquivalent for T
where
T: PwmEquivalent,
{
async fn get_equivalent<Q>(
&self,
key: &Q,
) -> Result<Option<&EntryValue<Self::Value>>, Self::Error>
where
Self::Key: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
<T as PwmEquivalent>::get_equivalent(self, key)
}
async fn get_entry_equivalent<Q>(
&self,
key: &Q,
) -> Result<Option<(&Self::Key, &EntryValue<Self::Value>)>, Self::Error>
where
Self::Key: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
<T as PwmEquivalent>::get_entry_equivalent(self, key)
}
async fn contains_key_equivalent<Q>(&self, key: &Q) -> Result<bool, Self::Error>
where
Self::Key: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
<T as PwmEquivalent>::contains_key_equivalent(self, key)
}
async fn remove_entry_equivalent<Q>(
&mut self,
key: &Q,
) -> Result<Option<(Self::Key, EntryValue<Self::Value>)>, Self::Error>
where
Self::Key: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
<T as PwmEquivalent>::remove_entry_equivalent(self, key)
}
}