nativeview_store/node/
node_ref.rs1use nativeview_abi::table::Word;
4
5use crate::id::{NodeId, NodeIndex, ResourceId, TableSet};
6use crate::resource::FlatResource;
7use crate::row::RowRef;
8use crate::span_pool::{StrArena, StrSpan};
9use crate::store::{NodeRow, NodeStore};
10use crate::table::{NvTable, TableSlot};
11
12#[derive(Clone, Copy)]
17pub struct NodeRef<'a> {
18 store: &'a NodeStore,
19 index: NodeIndex,
20}
21
22impl<'a> NodeRef<'a> {
23 #[inline(always)]
24 pub(crate) const fn new_unchecked(store: &'a NodeStore, index: NodeIndex) -> Self {
25 Self { store, index }
26 }
27
28 #[inline(always)]
29 pub fn raw_row(self) -> &'a NodeRow {
30 &self.store.nodes[self.index]
31 }
32
33 #[inline(always)]
34 pub fn index(self) -> NodeIndex {
35 self.index
36 }
37
38 #[inline(always)]
39 pub fn id(self) -> NodeId {
40 NodeId::from_index(self.index, self.raw_row().generation)
41 }
42
43 #[inline(always)]
44 pub fn tables(self) -> TableSet {
45 self.raw_row().tables
46 }
47
48 #[inline(always)]
49 pub fn primary_slot(self) -> TableSlot {
50 self.raw_row().primary_slot
51 }
52
53 #[inline(always)]
54 pub fn is_inline(self) -> bool {
55 self.raw_row().is_inline()
56 }
57
58 #[inline(always)]
59 pub fn is_connected(self) -> bool {
60 self.raw_row().is_connected()
61 }
62
63 #[inline(always)]
64 pub fn parent(self) -> Option<NodeId> {
65 let p = self.raw_row().parent;
66 (!p.is_null()).then_some(p)
67 }
68
69 #[inline(always)]
70 pub fn children(self) -> &'a [NodeId] {
71 self.store.children.get(self.raw_row().children)
72 }
73
74 #[inline(always)]
75 pub fn has<T: NvTable>(self) -> bool {
76 self.raw_row().tables.contains(T::slot())
77 }
78
79 #[inline(always)]
80 pub fn has_slot(self, slot: TableSlot) -> bool {
81 self.raw_row().tables.contains(slot)
82 }
83
84 #[inline(always)]
85 pub fn words(self, slot: TableSlot) -> Option<&'a [Word]> {
86 if !self.has_slot(slot) {
87 return None;
88 }
89 let col = self.store.columns[slot.0 as usize].as_ref()?;
90 Some(col.words(col.row_of(self.index)))
91 }
92
93 #[inline(always)]
94 fn words_for<T: NvTable>(self) -> Option<&'a [Word]> {
95 let slot = T::slot();
96 let w = self.words(slot)?;
97 debug_assert_eq!(
98 self.store.columns[slot.0 as usize]
99 .as_ref()
100 .map(|c| c.desc.key()),
101 Some(T::DESC.key),
102 "table slot {} key mismatch for `{}`",
103 slot.0,
104 T::DESC.name
105 );
106 Some(w)
107 }
108
109 #[inline(always)]
110 pub fn try_row<T: NvTable>(self) -> Option<T::Ref<'a>> {
111 self.words_for::<T>()
112 .map(|w| T::from_row_ref(RowRef::new(w, &self.store.text, T::DESC.str_words)))
113 }
114
115 #[inline(always)]
116 pub fn row<T: NvTable>(self) -> T::Ref<'a> {
117 let w = self.words_for::<T>().unwrap_or(T::DESC.defaults);
118 T::from_row_ref(RowRef::new(w, &self.store.text, T::DESC.str_words))
119 }
120
121 #[inline]
123 pub fn try_str(self, span: StrSpan) -> Option<&'a str> {
124 self.store.text.try_str(span)
125 }
126
127 #[inline]
129 pub fn str(self, span: StrSpan) -> &'a str {
130 self.store.text.str(span)
131 }
132
133 #[inline(always)]
134 pub fn resource<R: FlatResource>(self, id: ResourceId) -> Option<R::View<'a>> {
135 self.store.resources.get::<R>(id)
136 }
137
138 #[inline(always)]
139 pub(crate) fn text_arena(self) -> &'a StrArena {
140 &self.store.text
141 }
142}