1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//! Node ID.

use core::fmt;
use core::hash::Hash;

/// A trait for internal node ID types.
///
/// This trait is implemented for limited types in `treena` crate.
/// Downstream users cannot implement this to other types.
pub trait InternalNodeId: Copy + Eq + Hash + fmt::Debug + private::SealedInternalNodeId {}

/// A trait for node ID types.
pub trait NodeId: Copy + Eq + Hash + fmt::Debug {
    /// Internal (backend) node ID type.
    type Internal: InternalNodeId;

    /// Converts the custom ID type to the internal node ID type.
    #[must_use]
    fn from_internal(id: Self::Internal) -> Self;
    /// Converts the internal node ID type to the custom ID type.
    #[must_use]
    fn to_internal(self) -> Self::Internal;
}

impl<T: InternalNodeId> NodeId for T {
    type Internal = Self;

    #[inline]
    fn from_internal(id: Self::Internal) -> Self {
        id
    }
    #[inline]
    fn to_internal(self) -> Self::Internal {
        self
    }
}

/// Implements `NodeId` trait for the given node ID type.
///
/// # Examples
///
/// ```
/// use treena::impl_dynamic_node_id;
/// use treena::dynamic::NodeIdUsize;
///
/// // Tuple struct.
/// #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
/// pub struct MyId(NodeIdUsize);
/// impl_dynamic_node_id!(MyId, NodeIdUsize, 0);
///
/// // Struct.
/// #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
/// pub struct MyId2 {
///     underlying: NodeIdUsize,
/// };
/// impl_dynamic_node_id!(MyId2, NodeIdUsize, underlying);
/// ```
#[macro_export]
macro_rules! impl_dynamic_node_id {
    ($outer:ty, $internal:ty, $field:tt) => {
        impl $crate::dynamic::NodeId for $outer {
            type Internal = $internal;

            #[inline]
            fn from_internal(id: Self::Internal) -> Self {
                Self { $field: id }
            }
            #[inline]
            fn to_internal(self) -> Self::Internal {
                self.$field
            }
        }
    };
}

/// An extention trait for [`NodeId`] to expose
/// [`SealedInternalNodeId`][`private::SealedInternalNodeId`] methods.
pub(super) trait NodeIdExt: Sized {
    /// Returns the raw `usize` value.
    #[must_use]
    fn to_usize(self) -> usize;
}

impl<Id: NodeId> NodeIdExt for Id {
    #[inline]
    fn to_usize(self) -> usize {
        private::SealedInternalNodeId::to_usize(self.to_internal())
    }
}

/// Defines an internal node ID type.
macro_rules! define_internal_id_type {
    ($ty:ident, $backend:ty, $intstr:expr) => {
        /// Node ID that can be used at most
        #[doc = concat!(" `", $intstr, "::MAX - 1`")]
        ///  nodes.
        ///
        /// The ordering (`PartialOrd` and `Ord`) for node IDs are only provided for
        /// use with some containers who wants ordered key types (such as `BTreeSet`).
        /// Note that it is **not** guaranteed that the ordering of a key has some
        /// relation to the order the node is created.
        /// This also means that the users must use `Debug` formatting only for dumping
        /// the value, but not for manipulating internal integer value extracted by
        /// `Debug` trait.
        #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
        pub struct $ty($backend);

        // Prevent `{:#?}` from printing the value in redundant 3 lines.
        impl fmt::Debug for $ty {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                write!(f, "NodeId({:?})", self.0)
            }
        }

        impl InternalNodeId for $ty {}

        impl private::SealedInternalNodeId for $ty {
            #[inline]
            #[must_use]
            fn to_usize(self) -> usize {
                self.0.get()
            }

            #[inline]
            #[must_use]
            fn from_usize(v: usize) -> Option<Self> {
                <$backend>::new(v).map(Self)
            }
        }
    };
}

define_internal_id_type!(NodeIdU8, crate::nonmax::NonMaxU8, "u8");
define_internal_id_type!(NodeIdU16, crate::nonmax::NonMaxU16, "u16");
define_internal_id_type!(NodeIdU32, crate::nonmax::NonMaxU32, "u32");
define_internal_id_type!(NodeIdU64, crate::nonmax::NonMaxU64, "u64");
define_internal_id_type!(NodeIdUsize, crate::nonmax::NonMaxUsize, "usize");

/// Private module to provide [`Sealed`][`private::SealedInternalNodeId`] trait.
mod private {
    /// A trait to seal [`InternalNodeId`][`super::InternalNodeId`] and
    /// provide functions only for internal use.
    ///
    /// This trait cannot be `use`d by downstream crates, so it is safe to put
    /// internal-use-only functions here.
    /// Hiding these function makes it impossible for users to create internal
    /// node ID from arbitrary values safely. This makes it easy to change
    /// internal structure of node IDs in non-breaking way.
    pub trait SealedInternalNodeId: Sized {
        /// Returns the raw `usize` value.
        #[must_use]
        fn to_usize(self) -> usize;
        /// Creates a node ID from the raw `usize` value.
        ///
        /// This should return `None` when the node ID creation fails.
        #[must_use]
        fn from_usize(v: usize) -> Option<Self>;
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use core::mem;

    #[test]
    fn niche_optimized() {
        assert_eq!(
            mem::size_of::<NodeIdUsize>(),
            mem::size_of::<Option<NodeIdUsize>>(),
            "`Option<NodeId>` type must have the same size as \
             `NodeId` type due to niche optimization"
        );
    }
}