use core::cmp::Ordering;
use crate::prelude::*;
pub trait Entrylike<const MCL: usize, const MCC: usize, const MPL: usize, N, S, PD>:
Coordinatelike<MCL, MCC, MPL, S>
{
fn wdm_namespace_id(&self) -> &N;
fn wdm_payload_length(&self) -> u64;
fn wdm_payload_digest(&self) -> &PD;
}
pub trait EntrylikeExt<const MCL: usize, const MCC: usize, const MPL: usize, N, S, PD>:
Entrylike<MCL, MCC, MPL, N, S, PD> + CoordinatelikeExt<MCL, MCC, MPL, S>
{
fn wdm_entry_eq<OtherEntry>(&self, other: &OtherEntry) -> bool
where
OtherEntry: Entrylike<MCL, MCC, MPL, N, S, PD>,
N: PartialEq,
S: PartialEq,
PD: PartialEq,
{
self.wdm_namespace_id() == other.wdm_namespace_id()
&& self.wdm_coordinate_eq(other)
&& self.wdm_payload_digest() == other.wdm_payload_digest()
&& self.wdm_payload_length() == other.wdm_payload_length()
}
fn wdm_entry_ne<OtherEntry>(&self, other: &OtherEntry) -> bool
where
OtherEntry: Entrylike<MCL, MCC, MPL, N, S, PD>,
N: PartialEq,
S: PartialEq,
PD: PartialEq,
{
self.wdm_namespace_id() != other.wdm_namespace_id()
|| self.wdm_coordinate_ne(other)
|| self.wdm_payload_digest() != other.wdm_payload_digest()
|| self.wdm_payload_length() != other.wdm_payload_length()
}
fn wdm_cmp_recency<OtherEntry>(&self, other: &OtherEntry) -> Ordering
where
OtherEntry: Entrylike<MCL, MCC, MPL, N, S, PD>,
PD: Ord,
{
self.wdm_timestamp()
.cmp(&other.wdm_timestamp())
.then_with(|| {
self.wdm_payload_digest()
.cmp(other.wdm_payload_digest())
.then_with(|| self.wdm_payload_length().cmp(&other.wdm_payload_length()))
})
}
fn wdm_is_newer_than<OtherEntry>(&self, other: &OtherEntry) -> bool
where
OtherEntry: Entrylike<MCL, MCC, MPL, N, S, PD>,
PD: Ord,
{
self.wdm_cmp_recency(other) == Ordering::Greater
}
fn wdm_is_older_than<OtherEntry>(&self, other: &OtherEntry) -> bool
where
OtherEntry: Entrylike<MCL, MCC, MPL, N, S, PD>,
PD: Ord,
{
self.wdm_cmp_recency(other) == Ordering::Less
}
fn wdm_prunes<OtherEntry>(&self, other: &OtherEntry) -> bool
where
OtherEntry: Entrylike<MCL, MCC, MPL, N, S, PD>,
N: PartialEq,
S: PartialEq,
PD: Ord,
{
self.wdm_is_newer_than(other)
&& self.wdm_namespace_id() == other.wdm_namespace_id()
&& self.wdm_subspace_id() == other.wdm_subspace_id()
&& self.wdm_path().is_prefix_of(other.wdm_path())
}
fn wdm_is_pruned_by<OtherEntry>(&self, other: &OtherEntry) -> bool
where
OtherEntry: Entrylike<MCL, MCC, MPL, N, S, PD>,
N: PartialEq,
S: PartialEq,
PD: Ord,
Self: Sized,
{
other.wdm_prunes(self)
}
}
impl<const MCL: usize, const MCC: usize, const MPL: usize, N, S, PD, E>
EntrylikeExt<MCL, MCC, MPL, N, S, PD> for E
where
E: Entrylike<MCL, MCC, MPL, N, S, PD>,
{
}