use crate::paths::*;
/// A keylike value is one that can be used to uniquely identify an [Entry](https://willowprotocol.org/specs/data-model/index.html#Entry) within a [namespace](https://willowprotocol.org/specs/data-model/index.html#namespace)-specific [store](https://willowprotocol.org/specs/data-model/index.html#store).
///
/// A [subspace_id](https://willowprotocol.org/specs/data-model/index.html#entry_subspace_id) and a [path](https://willowprotocol.org/specs/data-model/index.html#entry_path) together uniquely identify an entry in a namespace, because between any two non-equal entries with of equal [subspace_id](https://willowprotocol.org/specs/data-model/index.html#entry_subspace_id), [path](https://willowprotocol.org/specs/data-model/index.html#entry_path), and [namespace_id](https://willowprotocol.org/specs/data-model/index.html#entry_namespace_id), one would overwrite the other.
pub trait Keylike<const MCL: usize, const MCC: usize, const MPL: usize, S> {
/// Returns the [subspace_id](https://willowprotocol.org/specs/data-model/index.html#entry_subspace_id) of `self`.
fn wdm_subspace_id(&self) -> &S;
/// Returns the [path](https://willowprotocol.org/specs/data-model/index.html#entry_path) of `self`.
fn wdm_path(&self) -> &Path<MCL, MCC, MPL>;
}
impl<const MCL: usize, const MCC: usize, const MPL: usize, S> Keylike<MCL, MCC, MPL, S>
for (S, Path<MCL, MCC, MPL>)
{
fn wdm_subspace_id(&self) -> &S {
&self.0
}
fn wdm_path(&self) -> &Path<MCL, MCC, MPL> {
&self.1
}
}
/// Methods for working with [`Keylikes`](Keylike).
///
/// This trait is automatically implemented by all types implementing [`Keylike`].
pub trait KeylikeExt<const MCL: usize, const MCC: usize, const MPL: usize, S>:
Keylike<MCL, MCC, MPL, S>
{
/// Returns whether `self` and `other` describe equal keys, i.e., whether their [subspace ids](Keylike::wdm_subspace_id) and [paths](Keylike::wdm_path) are both equal.
///
/// # Examples
///
/// ```
/// use willow_data_model::prelude::*;
///
/// assert!(("alfie", Path::<4, 4, 4>::new()).wdm_key_eq(&("alfie", Path::<4, 4, 4>::new())));
/// assert!(!("alfie", Path::<4, 4, 4>::new()).wdm_key_eq(&("betty", Path::<4, 4, 4>::new())));
/// ```
fn wdm_key_eq<OtherKey>(&self, other: &OtherKey) -> bool
where
OtherKey: Keylike<MCL, MCC, MPL, S>,
S: PartialEq,
{
self.wdm_subspace_id() == other.wdm_subspace_id() && self.wdm_path() == other.wdm_path()
}
/// Returns whether `self` and `other` describe non-equal keys, i.e., whether their [subspace ids](Keylike::wdm_subspace_id) and [paths](Keylike::wdm_path) are not both equal.
///
/// # Examples
///
/// ```
/// use willow_data_model::prelude::*;
///
/// assert!(!("alfie", Path::<4, 4, 4>::new()).wdm_key_ne(&("alfie", Path::<4, 4, 4>::new())));
/// assert!(("alfie", Path::<4, 4, 4>::new()).wdm_key_ne(&("betty", Path::<4, 4, 4>::new())));
/// ```
fn wdm_key_ne<OtherKey>(&self, other: &OtherKey) -> bool
where
OtherKey: Keylike<MCL, MCC, MPL, S>,
S: PartialEq,
{
self.wdm_subspace_id() != other.wdm_subspace_id() || self.wdm_path() != other.wdm_path()
}
}
impl<const MCL: usize, const MCC: usize, const MPL: usize, S, T> KeylikeExt<MCL, MCC, MPL, S> for T where
T: Keylike<MCL, MCC, MPL, S> + ?Sized
{
}