use core::{borrow::Borrow, hash::Hash, ops::RangeBounds};
use super::types::*;
#[cfg(feature = "alloc")]
mod hash_cm;
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub use hash_cm::*;
#[cfg(feature = "alloc")]
mod btree_cm;
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub use btree_cm::*;
#[cfg(feature = "alloc")]
mod btree_pwm;
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub use btree_pwm::*;
#[cfg(feature = "alloc")]
mod hash_pwm;
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub use hash_pwm::*;
pub struct Marker<'a, C> {
marker: &'a mut C,
}
impl<'a, C> Marker<'a, C> {
#[inline]
pub fn new(marker: &'a mut C) -> Self {
Self { marker }
}
}
impl<'a, C: Cm> Marker<'a, C> {
pub fn mark(&mut self, k: &C::Key) {
self.marker.mark_read(k);
}
pub fn mark_conflict(&mut self, k: &C::Key) {
self.marker.mark_conflict(k);
}
}
impl<'a, C: CmRange> Marker<'a, C> {
pub fn mark_range(&mut self, range: impl RangeBounds<<C as Cm>::Key>) {
self.marker.mark_range(range);
}
}
impl<'a, C: CmIter> Marker<'a, C> {
pub fn mark_iter(&mut self) {
self.marker.mark_iter();
}
}
impl<'a, C: CmComparable> Marker<'a, C> {
pub fn mark_comparable<Q>(&mut self, k: &Q)
where
C::Key: Borrow<Q>,
Q: Ord + ?Sized,
{
self.marker.mark_read_comparable(k);
}
pub fn mark_conflict_comparable<Q>(&mut self, k: &Q)
where
C::Key: Borrow<Q>,
Q: Ord + ?Sized,
{
self.marker.mark_conflict_comparable(k);
}
}
impl<'a, C: CmComparableRange> Marker<'a, C> {
pub 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);
}
}
impl<'a, C: CmEquivalent> Marker<'a, C> {
pub fn mark_equivalent<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<Q>(&mut self, k: &Q)
where
C::Key: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self.marker.mark_conflict_equivalent(k);
}
}
impl<'a, C: CmEquivalentRange> Marker<'a, C> {
pub 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);
}
}
pub trait Cm: Sized {
type Error: crate::error::Error;
type Key;
type Options;
fn new(options: Self::Options) -> Result<Self, Self::Error>;
fn mark_read(&mut self, key: &Self::Key);
fn mark_conflict(&mut self, key: &Self::Key);
fn has_conflict(&self, other: &Self) -> bool;
fn rollback(&mut self) -> Result<(), Self::Error>;
}
pub trait CmRange: Cm + Sized {
fn mark_range(&mut self, range: impl RangeBounds<<Self as Cm>::Key>);
}
pub trait CmIter: Cm + Sized {
fn mark_iter(&mut self);
}
impl<T: CmRange> CmIter for T {
fn mark_iter(&mut self) {
self.mark_range(..);
}
}
pub trait CmEquivalent: Cm {
fn mark_read_equivalent<Q>(&mut self, key: &Q)
where
Self::Key: Borrow<Q>,
Q: Hash + Eq + ?Sized;
fn mark_conflict_equivalent<Q>(&mut self, key: &Q)
where
Self::Key: Borrow<Q>,
Q: Hash + Eq + ?Sized;
}
pub trait CmEquivalentRange: CmRange + Sized {
fn mark_range_equivalent<Q>(&mut self, range: impl RangeBounds<Q>)
where
Self::Key: Borrow<Q>,
Q: Hash + Eq + ?Sized;
}
pub trait CmComparable: Cm {
fn mark_read_comparable<Q>(&mut self, key: &Q)
where
Self::Key: Borrow<Q>,
Q: Ord + ?Sized;
fn mark_conflict_comparable<Q>(&mut self, key: &Q)
where
Self::Key: Borrow<Q>,
Q: Ord + ?Sized;
}
pub trait CmComparableRange: CmRange + CmComparable {
fn mark_range_comparable<Q>(&mut self, range: impl RangeBounds<Q>)
where
Self::Key: Borrow<Q>,
Q: Ord + ?Sized;
}
pub trait Pwm: Sized {
type Error: crate::error::Error;
type Key;
type Value;
type Iter<'a>: Iterator<Item = (&'a Self::Key, &'a EntryValue<Self::Value>)>
where
Self: 'a;
type IntoIter: Iterator<Item = (Self::Key, EntryValue<Self::Value>)>;
type Options;
fn new(options: Self::Options) -> Result<Self, Self::Error>;
fn is_empty(&self) -> bool;
fn len(&self) -> usize;
fn validate_entry(&self, entry: &Entry<Self::Key, Self::Value>) -> 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) -> Result<Option<&EntryValue<Self::Value>>, Self::Error>;
fn get_entry(
&self,
key: &Self::Key,
) -> Result<Option<(&Self::Key, &EntryValue<Self::Value>)>, Self::Error>;
fn contains_key(&self, key: &Self::Key) -> Result<bool, Self::Error>;
fn insert(&mut self, key: Self::Key, value: EntryValue<Self::Value>) -> Result<(), Self::Error>;
fn remove_entry(
&mut self,
key: &Self::Key,
) -> Result<Option<(Self::Key, EntryValue<Self::Value>)>, Self::Error>;
fn iter(&self) -> Self::Iter<'_>;
fn into_iter(self) -> Self::IntoIter;
fn rollback(&mut self) -> Result<(), Self::Error>;
}
pub trait PwmRange: Pwm {
type Range<'a>: IntoIterator<Item = (&'a Self::Key, &'a EntryValue<Self::Value>)>
where
Self: 'a;
fn range<R: RangeBounds<Self::Key>>(&self, range: R) -> Self::Range<'_>;
}
pub trait PwmComparableRange: PwmRange + PwmComparable {
fn range_comparable<T, R>(&self, range: R) -> Self::Range<'_>
where
T: ?Sized + Ord,
Self::Key: Borrow<T> + Ord,
R: RangeBounds<T>;
}
pub trait PwmEquivalentRange: PwmRange + PwmEquivalent {
fn range_equivalent<T, R>(&self, range: R) -> Self::Range<'_>
where
T: ?Sized + Eq + Hash,
Self::Key: Borrow<T> + Eq + Hash,
R: RangeBounds<T>;
}
pub trait PwmEquivalent: Pwm {
fn get_equivalent<Q>(&self, key: &Q) -> Result<Option<&EntryValue<Self::Value>>, Self::Error>
where
Self::Key: Borrow<Q>,
Q: Hash + Eq + ?Sized;
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;
fn contains_key_equivalent<Q>(&self, key: &Q) -> Result<bool, Self::Error>
where
Self::Key: Borrow<Q>,
Q: Hash + Eq + ?Sized;
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;
}
pub trait PwmComparable: Pwm {
fn get_comparable<Q>(&self, key: &Q) -> Result<Option<&EntryValue<Self::Value>>, Self::Error>
where
Self::Key: Borrow<Q>,
Q: Ord + ?Sized;
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;
fn contains_key_comparable<Q>(&self, key: &Q) -> Result<bool, Self::Error>
where
Self::Key: Borrow<Q>,
Q: Ord + ?Sized;
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;
}