use crate::Pointer;
use std::{
cmp::Ordering,
hash::{Hash, Hasher},
ops::{Deref, DerefMut},
};
#[derive(Debug, Clone, Copy, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PointerIdentity<P: Pointer>(pub P);
impl<P: Pointer> PointerIdentity<P> {
pub fn new(value: P) -> Self {
Self(value)
}
pub fn inner(&self) -> &P {
&self.0
}
pub fn into_inner(self) -> P {
self.0
}
}
impl<P: Pointer> From<P> for PointerIdentity<P> {
fn from(value: P) -> Self {
Self::new(value)
}
}
impl<P: Pointer> PartialEq for PointerIdentity<P> {
fn eq(&self, other: &Self) -> bool {
self.0.get() == other.0.get()
}
}
impl<P: Pointer> Eq for PointerIdentity<P> {}
impl<P: Pointer> PartialOrd for PointerIdentity<P> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.0.get().cmp(&other.0.get()))
}
}
impl<P: Pointer> Ord for PointerIdentity<P> {
fn cmp(&self, other: &Self) -> Ordering {
self.0.get().cmp(&other.0.get())
}
}
impl<P: Pointer> Hash for PointerIdentity<P> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.get().hash(state);
}
}
impl<P: Pointer> Deref for PointerIdentity<P> {
type Target = P;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<P: Pointer> DerefMut for PointerIdentity<P> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}