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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//! The identifiers of a tree and of a node: [`TreeId`] and [`NodeAddr`], the
//! keys under which computed data is stored per node.
//!
//! A **tree** is one placement of a [fragment](crate::model::doc::Fragment)'s
//! node tree in a document. The document's tree arena numbers its trees with
//! [`TreeId`]s in the order they are added, and never renumbers or reuses
//! them. Adding the same `Arc<Fragment>` twice to a document makes two trees,
//! each with its own computed data.
//!
//! A [`TreeId`] can also identify a [synthesized
//! value](crate::guide::concepts_overview#synthesized-values): a small tree
//! that a stage builds while a run is under way, such as the content a `\ref`
//! displays or a formatted citation entry. A synthesized value is held outside
//! the document's trees: a consumer lowers it and renders it like document
//! content, but no stage that scans the document reaches it. Synthesized values
//! are numbered from zero by the stage invocation that builds them, so two
//! invocations assign the same identifiers, and the identifier of a synthesized
//! value means nothing outside the invocation that built it.
//!
//! [`TreeId`] holds both kinds of identifier in 4 bytes, told apart by a tag
//! bit. Code that stores computed data must respect two rules that follow from
//! the numbering of synthesized values:
//!
//! - no table stored in a run's shared store may use as a key a [`NodeAddr`]
//! inside a synthesized value;
//! - a table shown for inspection writes a stored synthesized value out in
//! full, never as its identifier.
//!
//! A [`NodeAddr`] pairs a [`TreeId`] with techy's [`NodeId`], 12 bytes in all.
//! It is `Copy` and ordered, so it can key an ordered map. A [`NodeId`] carries
//! a tag of the tree layout that assigned it, and that tag takes part in
//! equality, ordering and hashing. Two addresses in different trees never
//! compare equal, because their [`TreeId`]s differ. An ordered map keyed by addresses visits them tree by tree,
//! and within a tree in the tree's storage order. That order is deterministic,
//! but it is not document order: document order is defined by the walk,
//! [`DocContent::walk`].
//!
//! [`DocContent::walk`]: crate::model::doc::DocContent::walk
use fmt;
pub use NodeId;
/// The identifier of one tree of a document's tree arena, or of one
/// synthesized value built inside a run.
///
/// The two kinds of identifier are numbered separately: a document tree and a
/// synthesized value with the same number are different identifiers.
/// [`TreeId::tree`] and [`TreeId::value`] create an identifier of each kind;
/// [`TreeId::is_document_tree`] and [`TreeId::is_value`] tell which kind an
/// identifier is, and [`TreeId::index`] returns its number. See the [module
/// documentation](self) for what each kind identifies.
///
/// Every document tree orders before every synthesized value; identifiers of
/// one kind order by their number. The `Display` form is `tree#3` for a
/// document tree and `value#3` for a synthesized value.
///
/// # Examples
///
/// ```
/// use flm_core::model::addr::TreeId;
///
/// let tree = TreeId::tree(3);
/// let value = TreeId::value(3);
/// assert_ne!(tree, value);
/// assert_eq!(tree.index(), value.index());
/// assert!(tree.is_document_tree());
/// assert!(value.is_value());
/// assert!(tree < value);
/// ```
;
/// The address of one node in one tree: the tree's [`TreeId`] and the node's
/// [`NodeId`] in that tree.
///
/// Every computed table that stores data per node uses this as its key, and a
/// table entry uses it to refer to a node of another tree. It is `Copy` and
/// ordered; the [module documentation](self) explains that this order is not
/// document order.