taffy/tree/
node.rs

1//! UI node types and related data structures.
2//!
3//! Layouts are composed of multiple nodes, which live in a tree-like data structure.
4
5#[cfg(feature = "taffy_tree")]
6use slotmap::{DefaultKey, Key, KeyData};
7
8/// A type representing the id of a single node in a tree of nodes
9///
10/// Internally it is a wrapper around a u64 and a `NodeId` can be converted to and from
11/// and u64 if needed.
12#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
13pub struct NodeId(u64);
14impl NodeId {
15    /// Create a new NodeId from a u64 value
16    pub const fn new(val: u64) -> Self {
17        Self(val)
18    }
19}
20
21impl From<u64> for NodeId {
22    #[inline]
23    fn from(raw: u64) -> Self {
24        Self(raw)
25    }
26}
27impl From<NodeId> for u64 {
28    #[inline]
29    fn from(id: NodeId) -> Self {
30        id.0
31    }
32}
33impl From<usize> for NodeId {
34    #[inline]
35    fn from(raw: usize) -> Self {
36        Self(raw as u64)
37    }
38}
39impl From<NodeId> for usize {
40    #[inline]
41    fn from(id: NodeId) -> Self {
42        id.0 as usize
43    }
44}
45
46#[cfg(feature = "taffy_tree")]
47impl From<DefaultKey> for NodeId {
48    #[inline]
49    fn from(key: DefaultKey) -> Self {
50        Self(key.data().as_ffi())
51    }
52}
53
54#[cfg(feature = "taffy_tree")]
55impl From<NodeId> for DefaultKey {
56    #[inline]
57    fn from(key: NodeId) -> Self {
58        KeyData::from_ffi(key.0).into()
59    }
60}