dmv 0.3.1

Provides identity values that are restricted to user-specified scopes
use core::{
    fmt::{self, Debug, Display},
    hash::Hash,
};
use std::{marker::PhantomData, pin::Pin};

use crate::id::Comparable;
use crate::{RawRepr, Scope};

use self::handle::DmvHandle;

/// Useful trait aliases of [`Id`] with common types.
pub mod aliases;
pub mod handle;

/// The [`Id`] part of the [`crate::Dmv`] system.
///
/// See the [`crate::Dmv`] and [`Id`] documentation for more details.
pub struct Id<S: Scope, T: Comparable>(T, PhantomData<*const S>);

impl<S: Scope, T: Comparable> Id<S, T> {
    pub(crate) fn new(val: T) -> Pin<Box<Self>> {
        Box::pin(Self(val, PhantomData))
    }
}

impl<S: Scope, T: RawRepr + Comparable> crate::Id<S, T> for Pin<Box<Id<S, T>>> {
    type Handle = DmvHandle<S, T>;

    fn handle(&self) -> Self::Handle {
        DmvHandle::new(self)
    }
}

impl<S: Scope, T: Display + Comparable> Display for Pin<Box<Id<S, T>>> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        Display::fmt(&self.0, f)
    }
}

impl<S: Scope, T: Debug + Comparable> Debug for Pin<Box<Id<S, T>>> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        Debug::fmt(&self.0, f)
    }
}

impl<S: Scope, T: Comparable> PartialEq for Id<S, T> {
    fn eq(&self, other: &Self) -> bool {
        PartialEq::eq(&self.0, &other.0)
    }
}

impl<S: Scope, T: Comparable> Eq for Id<S, T> {}

impl<S: Scope, T: Comparable> PartialOrd for Id<S, T> {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        PartialOrd::partial_cmp(&self.0, &other.0)
    }
}

impl<S: Scope, T: Comparable> Ord for Id<S, T> {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        Ord::cmp(&self.0, &other.0)
    }
}

impl<S: Scope, T: Comparable + Hash> Hash for Id<S, T> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        Hash::hash(&self.0, state);
    }
}