nearest 0.4.5

Self-relative pointer library for region-based allocation
Documentation
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
//! # nearest
//!
//! Self-relative pointers and region-based allocation for Rust.
//!
//! `nearest` stores entire data graphs in a single contiguous byte buffer where
//! all internal pointers are 4-byte `i32` offsets relative to their own address.
//! This makes [`Clone`] a plain `memcpy` — no pointer fixup needed.
//!
//! Mutations use a branded [`Session`] API (ghost-cell pattern) where [`Ref`]
//! tokens carry a compile-time brand preventing cross-session use or escape —
//! all at zero runtime cost.
//!
//! # Core types
//!
//! | Type | Size | Role |
//! |------|------|------|
//! | [`Region<T>`] | heap | Owning contiguous buffer; root `T` at byte 0 |
//! | [`Near<T>`] | 4 B | Self-relative pointer (`NonZero<i32>` offset) |
//! | [`NearList<T>`] | 8 B | Segmented linked list (offset + length) |
//! | [`Session`] | — | Branded mutable session for safe mutation |
//! | [`Ref`] | 4 B | Branded position token (`Copy`, no borrow) |
//! | [`Flat`] | — | Marker trait: no `Drop`, no heap pointers |
//! | [`Emit`] | — | Builder trait for declarative construction |
//!
//! # Defining types
//!
//! [`#[derive(Flat)]`](derive@Flat) generates both the [`Flat`] safety marker
//! and the [`Emit`] builder, including a `make()` factory for each struct or
//! enum variant.
//!
//! ## Structs
//!
//! ```
//! use nearest::{Flat, Near, NearList};
//!
//! #[derive(Flat, Debug)]
//! struct Func {
//!   name: u32,
//!   entry: Near<Block>,
//! }
//!
//! #[derive(Flat, Debug)]
//! struct Block {
//!   id: u32,
//!   insts: NearList<u32>,
//! }
//! ```
//!
//! Fields may be primitives (`u8`–`u64`, `i32`–`i64`, `bool`), [`Near<T>`],
//! [`NearList<T>`], `Option<Near<T>>`, or any other [`Flat`] type.
//!
//! ## Enums
//!
//! Enums require `#[repr(C, u8)]` (or `#[repr(u8)]` for unit-only enums):
//!
//! ```
//! # #![feature(offset_of_enum)]
//! use nearest::Flat;
//!
//! #[derive(Flat, Copy, Clone, Debug, PartialEq)]
//! #[repr(C, u8)]
//! enum Value {
//!   Int(u32),
//!   Bool(bool),
//! }
//! ```
//!
//! Each data variant gets a `make_<variant>()` factory. Unit variants need
//! no factory.
//!
//! # Constructing a region
//!
//! [`Region::new`] takes any [`Emit<T>`](Emit) builder — typically the `make()`
//! factory generated by [`#[derive(Flat)]`](derive@Flat). Builder arguments
//! correspond to fields in declaration order. [`Near<T>`] fields accept any
//! `impl Emit<T>` (usually a nested `make()`), and [`NearList<T>`] fields
//! accept any `IntoIterator` whose items implement [`Emit`]:
//!
//! ```
//! use nearest::{Flat, Near, NearList, Region, empty, near, list};
//!
//! # #[derive(Flat, Debug)]
//! # struct Func { name: u32, entry: Near<Block> }
//! # #[derive(Flat, Debug)]
//! # struct Block { id: u32, insts: NearList<u32> }
//! let region = Region::new(Func::make(
//!   1,                                        // name: u32
//!   near(Block::make(0, list([10u32, 20, 30]))),  // entry: Near<Block>
//! ));
//!
//! assert_eq!(region.name, 1);
//! assert_eq!(region.entry.insts.len(), 3);
//! ```
//!
//! Use [`empty()`] for empty [`NearList`] fields:
//!
//! ```
//! # use nearest::{Flat, NearList, Region, empty};
//! # #[derive(Flat, Debug)]
//! # struct Block { id: u32, insts: NearList<u32> }
//! let region = Region::new(Block::make(0, empty()));
//! assert!(region.insts.is_empty());
//! ```
//!
//! # Reading data
//!
//! [`Region<T>`] implements [`Deref<Target = T>`](core::ops::Deref), giving
//! direct access to the root value. [`Near<T>`] also dereferences to `&T`:
//!
//! ```
//! # use nearest::{Flat, Near, NearList, Region, empty, near, list};
//! # #[derive(Flat, Debug)]
//! # struct Func { name: u32, entry: Near<Block> }
//! # #[derive(Flat, Debug)]
//! # struct Block { id: u32, insts: NearList<u32> }
//! # let region = Region::new(Func::make(1, near(Block::make(0, list([10u32, 20, 30])))));
//! // Region<T>: Deref<Target = T>
//! assert_eq!(region.name, 1);
//!
//! // Near<T>: Deref<Target = T>
//! let block: &Block = &region.entry;
//! assert_eq!(block.id, 0);
//!
//! // NearList<T>: indexing and iteration
//! assert_eq!(block.insts[0], 10);
//! let sum: u32 = block.insts.iter().sum();
//! assert_eq!(sum, 60);
//! ```
//!
//! # Mutating with sessions
//!
//! [`Region::session`] opens a branded closure where mutations are performed
//! through [`Ref`] position tokens. The ghost-cell pattern guarantees refs
//! cannot escape the closure or be used across sessions — enforced at
//! compile time with zero runtime cost.
//!
//! ```
//! # use nearest::{Flat, NearList, Region, empty, list};
//! #[derive(Flat, Debug)]
//! struct Block {
//!   id: u32,
//!   items: NearList<u32>,
//! }
//!
//! let mut region = Region::new(Block::make(1, list([10u32, 20, 30])));
//!
//! region.session(|s| {
//!   // Navigate to a field, then overwrite it
//!   let id = s.nav(s.root(), |b| &b.id);
//!   s.set(id, 42);
//!
//!   // Replace an entire list
//!   let items = s.nav(s.root(), |b| &b.items);
//!   s.splice_list(items, [100u32, 200]);
//! });
//!
//! assert_eq!(region.id, 42);
//! assert_eq!(region.items.len(), 2);
//! assert_eq!(region.items[0], 100);
//! ```
//!
//! ## Navigation
//!
//! | Method | Purpose |
//! |--------|---------|
//! | [`Session::root`] | Ref to the root value |
//! | [`Session::nav`] | Navigate to a sub-field |
//! | [`Session::follow`] | Dereference a [`Near<T>`] ref |
//! | [`Session::at`] | Read the value at a ref |
//!
//! ## Scalar mutation
//!
//! | Method | Purpose |
//! |--------|---------|
//! | [`Session::set`] | Overwrite a `Copy` field in place |
//! | [`Session::write`] | Overwrite with an [`Emit`] builder |
//!
//! ## Pointer and list replacement
//!
//! | Method | Purpose |
//! |--------|---------|
//! | [`Session::splice`] | Replace the target of a [`Near<T>`] |
//! | [`Session::splice_list`] | Replace the contents of a [`NearList<T>`] |
//!
//! ## List operations
//!
//! | Method | Purpose |
//! |--------|---------|
//! | [`Session::push_front`] | Prepend one element (O(1)) |
//! | [`Session::push_back`] | Append with cached [`ListTail`] (O(1)) |
//! | [`Session::extend_list`] | Append multiple elements |
//! | [`Session::filter_list`] | Keep elements matching a predicate |
//! | [`Session::map_list`] | Transform `Copy` elements in place |
//! | [`Session::reverse_list`] | Reverse element order |
//! | [`Session::sort_list`] | Sort elements by comparator |
//! | [`Session::dedup_list`] | Remove consecutive duplicates |
//!
//! ## Cursor API
//!
//! For deeply nested navigation, [`Session::cursor`] provides a chainable
//! interface:
//!
//! ```
//! # use nearest::{Flat, Near, NearList, Region, empty, near, list};
//! # #[derive(Flat, Debug)]
//! # struct Func { name: u32, entry: Near<Block> }
//! # #[derive(Flat, Debug)]
//! # struct Block { id: u32, insts: NearList<u32> }
//! # let mut region = Region::new(Func::make(1, near(Block::make(0, list([10u32, 20, 30])))));
//! region.session(|s| {
//!   s.cursor()
//!     .at(|f| &f.entry)
//!     .follow()
//!     .at(|b| &b.id)
//!     .set(99);
//! });
//!
//! assert_eq!(region.entry.id, 99);
//! ```
//!
//! # Serialization
//!
//! [`Region::as_bytes`] returns the raw buffer contents as a `&[u8]`, suitable
//! for writing to a file or sending over the network. [`Region::from_bytes`]
//! copies the bytes into an aligned buffer, then runs [`Flat::validate`] to
//! check bounds, alignment, pointer validity, and type-specific invariants
//! (e.g. enum discriminants, `bool` values) before reconstructing the region.
//!
//! ```
//! # use nearest::{Flat, NearList, Region, list};
//! #[derive(Flat, Debug)]
//! struct Node {
//!   id: u32,
//!   children: NearList<u32>,
//! }
//!
//! let original = Region::new(Node::make(1, list([10u32, 20, 30])));
//! let bytes = original.as_bytes();
//!
//! // bytes can be persisted to disk, sent over the network, etc.
//! let restored: Region<Node> = Region::from_bytes(bytes).unwrap();
//! assert_eq!(restored.id, 1);
//! assert_eq!(restored.children.len(), 3);
//! ```
//!
//! Invalid or corrupted data is rejected with a [`ValidateError`]:
//!
//! ```
//! use nearest::{Flat, Near, Region, ValidateError, near};
//!
//! #[derive(Flat, Debug)]
//! struct Flags {
//!   active: bool,
//!   label: Near<u32>,
//! }
//!
//! let region = Region::new(Flags::make(true, near(42u32)));
//! let mut bytes = region.as_bytes().to_vec();
//! bytes[core::mem::offset_of!(Flags, active)] = 2; // corrupt the bool
//! assert!(Region::<Flags>::from_bytes(&bytes).is_err());
//! ```
//!
//! # Compaction
//!
//! Mutations are append-only — replaced data becomes dead bytes in the
//! buffer. [`Region::trim`] deep-copies only reachable data into a fresh
//! buffer, reclaiming the waste:
//!
//! ```
//! # use nearest::{Flat, NearList, Region, empty, list};
//! # #[derive(Flat, Debug)]
//! # struct Block { id: u32, items: NearList<u32> }
//! let mut region = Region::new(Block::make(1, list([10u32, 20, 30])));
//! let before = region.byte_len();
//!
//! region.session(|s| {
//!   let items = s.nav(s.root(), |b| &b.items);
//!   s.splice_list(items, [100u32]);
//! });
//! assert!(region.byte_len() > before);
//!
//! region.trim();
//! assert!(region.byte_len() <= before);
//! assert_eq!(region.items[0], 100);
//! ```
//!
//! After trimming, every [`NearList`] is compacted to a single contiguous
//! segment, making indexing O(1).
//!
//! # Cloning
//!
//! Self-relative offsets are position-independent, so [`Clone`] for
//! [`Region<T>`] is a byte-for-byte buffer copy — no pointer fixup or
//! graph traversal:
//!
//! ```
//! # use nearest::{Flat, NearList, Region, list};
//! # #[derive(Flat, Debug)]
//! # struct Block { id: u32, items: NearList<u32> }
//! let region = Region::new(Block::make(1, list([10u32, 20, 30])));
//! let cloned = region.clone();
//!
//! assert_eq!(cloned.id, region.id);
//! assert_eq!(cloned.items[0], region.items[0]);
//! ```
//!
//! # Features
//!
//! | Feature | Default | Enables |
//! |---------|---------|---------|
//! | **`alloc`** | yes | Heap-backed [`AlignedBuf`], `Region::new`, `Region::trim`, growable sessions |
//! | **`std`** | no | Implies `alloc`; reserved for future `std`-only APIs |
//! | **`serde`** | no | [`Serialize`](::serde::Serialize) / [`Deserialize`](::serde::Deserialize) for `Region<T>` |
//!
//! ## `no_std` support
//!
//! `nearest` is `#![no_std]` by default. Disabling the `alloc` feature
//! removes all heap allocation — use [`FixedBuf<N>`] for stack-backed
//! regions with a compile-time capacity:
//!
//! ```toml
//! [dependencies]
//! nearest = { version = "0.2", default-features = false }
//! ```
//!
//! ```
//! use nearest::{Flat, NearList, Region, FixedBuf, empty, list};
//!
//! #[derive(Flat, Debug)]
//! struct Msg {
//!   id: u32,
//!   tags: NearList<u32>,
//! }
//!
//! // 128-byte inline buffer, no heap allocation.
//! let region: Region<Msg, FixedBuf<128>> =
//!   Region::new_in(Msg::make(1, list([10u32, 20])));
//! assert_eq!(region.id, 1);
//! assert_eq!(region.tags.len(), 2);
//! ```
//!
//! ## `serde`
//!
//! With the **`serde`** feature, `Region<T>` implements `Serialize` and
//! `Deserialize`. The region is serialized as its raw byte buffer (via
//! [`as_bytes`](Region::as_bytes)), and deserialization runs
//! [`from_bytes`](Region::from_bytes) validation automatically.
//!
//! ```toml
//! [dependencies]
//! nearest = { version = "0.2", features = ["serde"] }
//! ```
//!
//! # Safety model
//!
//! `nearest` is an `unsafe`-heavy crate. This section documents the
//! invariants that make the safe public API sound.
//!
//! ## [`Flat`] invariants
//!
//! Every type stored in a region must implement [`Flat`], which guarantees:
//!
//! - **No `Drop`**: A `const` assertion rejects types with destructors at
//!   compile time.
//! - **No heap pointers**: All indirection uses [`Near<T>`] or
//!   [`NearList<T>`] (self-relative offsets), never `Box`, `Vec`, or raw
//!   pointers.
//! - **Correct `deep_copy`**: The derived `deep_copy` copies fields and
//!   patches self-relative offsets to their new positions.
//!
//! ## Provenance
//!
//! Self-relative pointers cannot use strict Rust provenance: `&self.offset`
//! has provenance over only 4 bytes, but the target `T` may be larger.
//! Instead, every buffer allocation exposes its provenance via
//! `expose_provenance`, and reads recover it via `with_exposed_provenance`.
//! This is the canonical pattern for self-relative pointers in Rust,
//! validated by Miri.
//!
//! ## Aliasing
//!
//! Mutation through [`Session`] uses pre-reservation to prevent buffer
//! reallocation, then recovers provenance for reads so the `&T` is not
//! derived from the `&mut Region` — avoiding Stacked Borrows violations.
//!
//! ## Branding
//!
//! [`Region::session`] uses the ghost-cell pattern
//! (`for<'id> FnOnce(&mut Session<'id, …>)`) to make [`Ref`] tokens
//! invariant in their brand lifetime `'id`. This prevents refs from
//! escaping the session closure or being mixed across sessions — enforced
//! by compile-fail tests.
//!
//! ## Alignment
//!
//! The buffer base is aligned to `max(align_of::<Root>(), 8)`. Every
//! allocation within the buffer is aligned to `align_of::<T>()` via
//! padding, with a compile-time assertion that no type exceeds the
//! buffer alignment.
//!
//! ## No-drop
//!
//! The derive macro emits `const { assert!(!needs_drop::<T>()) }` for
//! every derived type, rejecting `Drop` impls at compile time. This
//! guarantees regions can be `memcpy`'d without running destructors.
//!
//! # Minimum Supported Rust Version
//!
//! This crate requires **nightly** Rust (pinned to `nightly-2026-02-10`)
//! due to [`offset_of_enum`](https://github.com/rust-lang/rust/issues/120141).

#![no_std]
#![feature(offset_of_enum)]
#![deny(missing_docs)]

#[cfg(feature = "alloc")]
extern crate alloc;

mod buf;
mod emit;
mod emitter;
mod flat;
pub(crate) mod list;
mod near;
mod patch;
mod region;
/// Branded session API for safe region mutation.
///
/// The ghost-cell pattern (`for<'id>` universally quantified lifetime) ensures
/// that [`Ref<'id, T>`](session::Ref) tokens cannot escape the session closure
/// or be used across different sessions. This gives **compile-time** safety
/// with **zero runtime cost** — `Ref` is just a `u32` position + phantom brand.
pub mod session;
mod validate;

#[cfg(feature = "alloc")]
pub use buf::AlignedBuf;
pub use buf::{Buf, FixedBuf};
pub use emit::{Emit, array, empty, list, maybe, near, none};

/// Not part of the public API. Used by the derive macro.
#[doc(hidden)]
pub mod __private {
  pub use crate::emitter::Pos;
}
pub use flat::Flat;
pub use list::{NearList, NearListIter};
pub use near::Near;
pub use nearest_derive::{Emit, Flat};
pub use patch::Patch;
pub use region::Region;
pub use session::{ListTail, Ref, Session};
pub use validate::ValidateError;