gctree 0.35.0

A library for cache-friendly, graph-like, arena-allocated datastructures.
Documentation
//!

pub trait NodeIdx: Copy
    + PartialEq<Self> + Eq
    + PartialOrd<Self> + Ord
    + std::hash::Hash
    + From<usize> // for indexing into Vec<_>
    + std::ops::Deref<Target = usize>
    + std::fmt::Display
    + std::fmt::Debug
{}


/// Create a new type `$name : NodeIdx`.
#[macro_export]
macro_rules! node_idx {
    ($name:ident) => {
        #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
        pub struct $name(usize);

        impl From<usize> for $name {
            fn from(raw: usize) -> Self {
                Self(raw)
            }
        }

        impl std::ops::Deref for $name {
            type Target = usize;

            fn deref(&self) -> &Self::Target {
                &self.0
            }
        }

        impl std::fmt::Debug for $name {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                let Self(idx) = *self;
                write!(f, "@{idx}")?;
                Ok(())
            }
        }

        impl std::fmt::Display for $name {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                let Self(idx) = *self;
                write!(f, "@{idx}")?;
                Ok(())
            }
        }

        impl $crate::new::node_idx::NodeIdx for $name {}
    };
}


#[cfg(test)]
mod tests {
    node_idx![TestIdx];

    #[test]
    fn make_idx_type() {
        let idx = TestIdx::from(0);
        assert_eq!(*idx, 0);
    }
}