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
//! Brand identity: the invariant lifetime that fuses a token to its cells.
//!
//! A *brand* is an [invariant] lifetime parameter `'brand`. Invariance is what
//! makes branding sound: two distinct [`brand_scope`] invocations receive
//! lifetimes that the compiler will never unify, so a token minted in one scope
//! can never be passed off as the token of another. This is the same mechanism
//! that underpins `GhostCell`, generalised here across multiple token families.
//!
//! [invariant]: https://doc.rust-lang.org/nomicon/subtyping.html#variance
use PhantomData;
use ExclusiveToken;
/// A zero-sized marker that is **invariant** in `'brand` and unconditionally
/// `Send + Sync`.
///
/// `fn(&'brand ()) -> &'brand ()` places `'brand` in both argument and return
/// position, forcing invariance, while function pointers are always `Send` and
/// `Sync`, so the marker never perturbs the auto-trait inference of its host.
pub type InvariantLifetime<'brand> = ;
/// Covariant lifetime marker for a shared-borrow window.
pub type BorrowWindow<'a> = ;
/// Shared zero-sized branding witness used by owner and shared-read tokens.
///
/// The `'brand` proof is always present; `Extra` lets each token family thread
/// its own auto-trait posture or borrow window through the same carrier
/// without adding runtime state.
pub
/// The private input to a fresh token-family factory.
///
/// It is intentionally linear: the factory creates one value for one
/// higher-ranked brand, and a token family consumes it while constructing its
/// concrete token. This keeps the fresh-brand proof in one generic boundary
/// while allowing token families to retain distinct auto-trait postures.
pub
/// A zero-sized family that constructs one concrete token per fresh brand.
pub
/// Define a brand-owner token family: the token struct, its [`TokenFamily`]
/// selector, the unsafe constructor, its [`Debug`](core::fmt::Debug) impl, the
/// two `BrandOwner` impls, and — for families that permit sharing — a `share`
/// constructor yielding a [`SharedReadToken`](crate::token::SharedReadToken).
///
/// This is the single authoritative copy of the minting argument. Every owner
/// token is laid out as an invariant brand proof plus an optional
/// confinement-marker field, is minted from one [`FreshBrand`], and is
/// witnessed as the brand's read/write XOR proof by exactly one `unsafe` impl.
/// Folding the families here keeps that `unsafe` reasoning — and its SAFETY
/// comment — from drifting between otherwise-identical copies.
///
/// * `name` / `family` — the public token type and its crate-private family
/// selector, both documented with the supplied attributes.
/// * `debug` — the [`Debug`](core::fmt::Debug) string for the token.
/// * `marker_extra` / `marker_init` — the extra ZST payload threaded through
/// the shared [`BrandMarker`] carrier to preserve a family's auto-trait
/// posture (e.g. `PhantomData<*const ()>` for `!Send`).
/// * `share` — `yes` to emit the `share` constructor, `no` to omit it.
// SAFETY: every family mints exactly one owner token for each fresh
// brand, so borrowing this token is the canonical proof of that brand's
// read/write XOR. A family's auto-trait posture only restricts where the
// unique proof may travel (e.g. `!Send` pins it to one thread); it never
// weakens the aliasing guarantees attached to borrowing the token.
unsafe
$cratebrand_owner_token_share!;
};
}
pub use brand_owner_token;
/// Emit the `share` constructor for a brand-owner token, or nothing.
///
/// Split out of [`brand_owner_token`] so the `share` body — and its `unsafe`
/// SAFETY argument — is written once, yet can be omitted for families whose
/// confinement posture forbids fanning out read capability (e.g.
/// `ThreadLocalToken`, which must not hand a `Send + Sync` read token across
/// threads).
pub use brand_owner_token_share;
/// Run a callback with a token from one fresh, higher-ranked brand.
///
/// The family selects the token's auto-trait and capability posture; the
/// callback result may escape, but the family token and its brand cannot.
pub
/// Open a fresh branding scope and hand its unique [`ExclusiveToken`] to `f`.
///
/// The higher-ranked bound `for<'brand>` universally quantifies the brand, so
/// `'brand` cannot escape the closure and cannot unify with any other scope's
/// brand. Consequently the token passed to `f` is provably the *only*
/// `ExclusiveToken<'brand>` in existence—the cornerstone of every downstream
/// access proof.
///
/// # Examples
///
/// ```
/// use melinoe::{brand_scope, MelinoeCell};
///
/// let doubled = brand_scope(|mut token| {
/// let cell = MelinoeCell::new(21_u32);
/// *cell.borrow_mut(&mut token) *= 2;
/// *cell.borrow(&token)
/// });
/// assert_eq!(doubled, 42);
/// ```
///
/// # Multi-XOR by composition
///
/// Several independent exclusion domains are obtained by *nesting* `brand_scope`,
/// not by arity-specific variants: each nested scope is a fresh, non-unifiable
/// brand, so a `&mut` into one region and a `&mut` into another may be held
/// simultaneously, disjointness proven at compile time.
///
/// ```
/// use melinoe::{brand_scope, MelinoeCell};
///
/// brand_scope(|mut ta| {
/// brand_scope(|mut tb| {
/// let a = MelinoeCell::new(10_u64);
/// let b = MelinoeCell::new(32_u64);
/// let mut ma = a.borrow_mut(&mut ta);
/// let mb = b.borrow_mut(&mut tb); // distinct brand ⇒ second live `&mut` is legal
/// *ma += *mb;
/// assert_eq!(*a.borrow(&ta), 42);
/// })
/// });
/// ```