//! This module defines a typed index struct. Useful to introduce type safety when using indexes //! several indexable containers. use crate::prelude::*; // ============= // === Index === // ============= /// Typed newtype for `usize` meant to be used as a typed index. pub struct Index { /// Raw value. pub raw : usize, phantom : PhantomData } impl Index { /// Constructor pub fn new(raw:usize) -> Self { let phantom = default(); Self {raw,phantom} } } // === Impls === impl Copy for Index {} impl Eq for Index {} impl Clone for Index { fn clone(&self) -> Self { *self } } impl Hash for Index { fn hash(&self, state:&mut H) { self.raw.hash(state) } } impl PartialEq for Index { fn eq(&self, other:&Self) -> bool { self.raw == other.raw } } impl From> for usize { fn from(t:Index) -> Self { t.raw } } impl From<&Index> for usize { fn from(t:&Index) -> Self { t.raw } } impl From for Index { fn from(t:usize) -> Self { Self::new(t) } } impl From<&usize> for Index { fn from(t:&usize) -> Self { Self::new(*t) } } impl Debug for Index { fn fmt(&self, f:&mut fmt::Formatter<'_>) -> fmt::Result { write!(f,"{}",self.raw) } } impl Display for Index { fn fmt(&self, f:&mut fmt::Formatter<'_>) -> fmt::Result { write!(f,"{}",self.raw) } }