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
//! The [`serial_index!`] macro: the one sanctioned way to define a typed table
//! position type (a [`SerialIndex`](crate::serialize::SerialIndex) implementer). The
//! macro is exported at the crate root (as every `macro_rules!` macro is) and
//! documented at its canonical path, `techy::serialize::serial_index`.
/// Defines a typed table position type: a `Copy` value naming a table and a position
/// within it.
///
/// Such a type is the `Index` type of an
/// [`ObjectSerdeDriver`](crate::serialize::ObjectSerdeDriver). It holds the
/// [`TableId`](crate::serialize::TableId) of its table and the `u32` position within it,
/// and it implements [`SerialIndex`](crate::serialize::SerialIndex). Each kind of table
/// has its own position type, so that a position in one table cannot be passed where a
/// position in another is expected.
///
/// # Syntax
///
/// One invocation defines one type, written as a unit struct declaration with a
/// trailing semicolon:
///
/// ```text
/// serial_index! {
/// <attributes, doc comments included>
/// <visibility> struct <Name>;
/// }
/// ```
///
/// No other form is accepted: no fields, no generic parameters, and no second struct in
/// the same invocation.
///
/// # Examples
///
/// ```
/// use techy::serialize::{SerialIndex, TableId};
///
/// techy::serial_index! {
/// /// A position in the table of leaves.
/// pub struct LeafIndex;
/// }
///
/// // Positions come from the session (`SerializeContext::intern`); code that holds
/// // one reads its two parts through the `SerialIndex` trait.
/// fn describe(position: LeafIndex) -> (TableId, u32) {
/// (position.table(), position.index())
/// }
/// ```
///
/// # What it generates
///
/// - the struct, with the given attributes (documentation included) and visibility, and
/// its two parts as private fields;
/// - the derives `Copy`, `Clone`, `Debug`, `PartialEq`, `Eq`, `Hash`, `PartialOrd`, and
/// `Ord`;
/// - the `SerialIndex` impl, whose
/// [`from_parts`](crate::serialize::SerialIndex::from_parts) is the type's only
/// constructor and whose [`table`](crate::serialize::SerialIndex::table) and
/// [`index`](crate::serialize::SerialIndex::index) are its only accessors;
/// - the crate's own conversions to and from a
/// [`SerialValue::Index`](crate::serialize::SerialValue::Index), so that the type can
/// be a field of the crate's serialized structures;
/// - with the `serde` cargo feature, serde's `Serialize` and `Deserialize`, through
/// which the type converts to and from a `SerialValue::Index` in the bridge
/// (`to_value` / `from_value`) and renders as the two-integer pair
/// `(table ordinal, index)` in any other format.
///
/// A program does not normally build positions itself: the session returns one when an
/// object is interned, and
/// [`TableHandle::position`](crate::serialize::TableHandle::position) rebuilds one from
/// a bare `u32` index. A typed position is scoped to the session that produced it — see
/// [`SerialIndex`](crate::serialize::SerialIndex).
}
$crate__serial_index_serde_impls!;
};
}
/// The serde half of [`serial_index!`]: present exactly when techy is built with the
/// `serde` cargo feature (the macro is defined under that feature, so a downstream
/// crate's own feature set does not matter). Not public API.
/// The serde half of [`serial_index!`] when techy is built without the `serde` cargo
/// feature: nothing. Not public API.