• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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")]
6 use 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)]
13 pub struct NodeId(u64);
14 impl NodeId {
15     /// Create a new NodeId from a u64 value
new(val: u64) -> Self16     pub const fn new(val: u64) -> Self {
17         Self(val)
18     }
19 }
20 
21 impl From<u64> for NodeId {
22     #[inline]
from(raw: u64) -> Self23     fn from(raw: u64) -> Self {
24         Self(raw)
25     }
26 }
27 impl From<NodeId> for u64 {
28     #[inline]
from(id: NodeId) -> Self29     fn from(id: NodeId) -> Self {
30         id.0
31     }
32 }
33 impl From<usize> for NodeId {
34     #[inline]
from(raw: usize) -> Self35     fn from(raw: usize) -> Self {
36         Self(raw as u64)
37     }
38 }
39 impl From<NodeId> for usize {
40     #[inline]
from(id: NodeId) -> Self41     fn from(id: NodeId) -> Self {
42         id.0 as usize
43     }
44 }
45 
46 #[cfg(feature = "taffy_tree")]
47 impl From<DefaultKey> for NodeId {
48     #[inline]
from(key: DefaultKey) -> Self49     fn from(key: DefaultKey) -> Self {
50         Self(key.data().as_ffi())
51     }
52 }
53 
54 #[cfg(feature = "taffy_tree")]
55 impl From<NodeId> for DefaultKey {
56     #[inline]
from(key: NodeId) -> Self57     fn from(key: NodeId) -> Self {
58         KeyData::from_ffi(key.0).into()
59     }
60 }
61