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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//! The value model of the serialized form: [`SerialValue`], [`SerialEntry`],
//! [`TableId`], and the [`SerialIndex`] bound. Nothing here names a source, a state, or
//! a spec — every kind of object is written in these terms alike.
use Cow;
use String;
use Vec;
/// A serialized value: the in-memory, format-independent form that every
/// serialization produces and every deserialization reads.
///
/// A value is produced by a [`SerializableValue`](crate::serialize::SerializableValue)
/// implementation or by the driver of a table, and a whole serialized object is such a
/// value together with the identifier of what it describes: a [`SerialEntry`].
///
/// The variant set is deliberately small, so that every value has exactly one JSON
/// rendering — JSON is the format the public serialization contract is stated in — and
/// so that two values render identically exactly when they compare equal. That is why
/// there are no floating-point numbers and no sized-integer variants (every integer is
/// an [`Int`](SerialValue::Int)), why map keys are strings and never begin with `$`,
/// and why the order of a [`Map`](SerialValue::Map)'s entries is part of the value.
///
/// The two variants without a native JSON form, [`Bytes`](SerialValue::Bytes) and
/// [`Index`](SerialValue::Index), render as reserved one-entry objects whose key begins
/// with `$`; no other value can produce such a key.
///
/// # Nesting depth
///
/// A value's *nesting depth* is the number of lists and maps that enclose its most
/// deeply nested part ([`nesting_depth`](SerialValue::nesting_depth)); it is bounded
/// by [`MAX_NESTING_DEPTH`](SerialValue::MAX_NESTING_DEPTH) at every point where a
/// value enters or leaves a session or a serialized form — read through serde,
/// converted to or from a [`Segment`](crate::serialize::Segment), interned into a
/// session — so that a malicious input cannot make a reader recurse without limit.
/// See the constant.
///
/// # Rendering through serde
///
/// With the `serde` cargo feature the type implements `Serialize` and `Deserialize`.
/// The rendering depends on the format: a human-readable one (serde's
/// `is_human_readable()`) gets the canonical rendering, every other format a compact
/// one. Both read back to the identical value.
///
/// The canonical rendering — not yet frozen, see "Stability of the serialized form" in
/// the [module documentation](crate::serialize) — stated for JSON: `Null` → `null`,
/// `Bool` → boolean, `Int` → number, `Str` → string, `List` → array, `Map` → object in
/// entry order; `Bytes` → the one-entry object `{"$bytes": "<base64>"}` (standard
/// alphabet, `=` padding, no line breaks); `Index` → the one-entry object
/// `{"$index": [<table>, <index>]}` (two integers: the table's ordinal, then the
/// position).
///
/// Those two keys are the only keys beginning with `$` the rendering ever writes. A
/// `Map` holding a key that begins with `$` is a rendering error — the prefix is
/// reserved and there is no escaping — and on reading, an object key beginning with `$`
/// that is not one of the two reserved forms is an error, as are floating-point
/// numbers, integers outside `i64`, and malformed reserved objects.
///
/// The compact rendering is the externally tagged form of this enum (the variant name,
/// then its data), with `Bytes` written through the format's
/// `serialize_bytes`/`deserialize_bytes` methods, `Index` as the two-integer pair, and
/// `Map` as a serde map.
/// The result of serializing one object: the object's identifier and its data.
///
/// This is what a
/// [`SerializableObject::serialize_object`](crate::serialize::SerializableObject::serialize_object)
/// call returns, and what a table stores for each object it holds.
///
/// The `identifier` names the kind of object `data` describes: a deliberately chosen,
/// stable string owned by whoever defines the object type — never a Rust type name. It
/// is a `Cow<'static, str>`, so that a fixed identifier can be a string literal while a
/// type whose identifier depends on the instance supplies an owned string. A table that
/// holds objects of one kind only does not write the identifier out; even then the
/// entry has a real, non-empty identifier.
/// The ordinal of a table: which numbered table a [`SerialValue::Index`] refers to.
///
/// Tables are numbered deterministically, in the order they are registered with a
/// [`SerdeSession`](crate::serialize::SerdeSession). User code receives `TableId`s —
/// inside typed table positions and on
/// [`TableHandle`](crate::serialize::TableHandle)s — and passes them along, but never
/// creates them.
///
/// A `TableId` is meaningful only relative to the session that assigned it: two
/// sessions that register the same tables in different orders number them differently.
/// Inside a segment, table references use the writing session's ids and are translated
/// by table *name* when another session absorbs the segment (see
/// [`SerdeSession::push_segment`](crate::serialize::SerdeSession::push_segment)). A
/// typed position held in Rust code is not translated, so it is exchanged between
/// sessions as its table's name and its `u32` index (see
/// [`SerialIndex`](crate::serialize::SerialIndex)).
;
/// The bound every typed table position satisfies: a `Copy` value holding both the
/// [`TableId`] of its table and the `u32` position within it.
///
/// A typed position is the `Index` type of an
/// [`ObjectSerdeDriver`](crate::serialize::ObjectSerdeDriver). It holds the same two
/// parts as a [`SerialValue::Index`], so that a position in one table cannot be mistaken
/// for a position in another and its serialized form needs no further context.
///
/// Position types are defined with the [`serial_index!`](crate::serialize::serial_index)
/// macro, which supplies this impl together with the conversions to and from the
/// serialized form; each kind of table has its own position type, defined next to
/// the driver of that table. Positions are created by the session (a
/// [`SerializeContext::intern`](crate::serialize::SerializeContext::intern) call
/// returns one) or rebuilt from a bare index with
/// [`TableHandle::position`](crate::serialize::TableHandle::position); on the
/// reading side, the session validates both parts.
///
/// **A typed position is scoped to the session that created it.** Its `TableId` is that
/// session's, and two sessions that register the same tables in different orders number
/// them differently: a position from a writing session, passed unchanged to a reading
/// session, names the wrong table there
/// ([`DeserializeError::WrongTable`](crate::serialize::DeserializeError::WrongTable)).
///
/// References *inside* entries need no such care: a segment stores positions as `u32`
/// indices scoped to the stream, and the reading session translates the table
/// references by table name. A position held in Rust code and passed from one session
/// to another is exchanged as its table's name
/// ([`ObjectSerdeDriver::table_name`](crate::serialize::ObjectSerdeDriver::table_name))
/// and its `u32` index ([`index`](SerialIndex::index)), and rebuilt on the receiving
/// side with `TableHandle::position`.