o3 0.25.1

shared-nothing primitives
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
use std::{
    marker, mem,
    num::NonZero,
    slice::{from_raw_parts, from_raw_parts_mut},
};

use crate::{
    base::num,
    buffer,
    buffer::{
        bytes::bounded,
        pool::{self, owners, state},
        resident, segments,
        storage::provided,
    },
    mem::rc,
};
mod slices;
pub use slices::Slices;
mod store;
pub use store::Store;
mod segment;
pub use segment::Segment;
mod scatter;
pub use scatter::Scatter;
pub(in crate::buffer) use slices::{
    Batch, Copy, Exclusive, Indexed, IndexedMut, Linked, LinkedMut, Prefix, SliceExt,
};

/// # Safety
/// The `buffer::raw::Source` contract must hold. `buffer::Cursor` must expose that same range.
/// Consumption must advance exactly the proved prefix and preserve the remaining bytes.
pub unsafe trait Consume: AsRef<[u8]> {
    fn consume<const MAX: u32>(prefix: Consumed<'_, '_, Self, MAX>);
}

// SAFETY: admission retains this exact initialized slice exclusively.
unsafe impl Consume for &mut [u8] {
    fn consume<const MAX: u32>(prefix: Consumed<'_, '_, Self, MAX>) {
        let (target, end) = prefix.into_parts();
        let bytes = mem::take(target);
        // SAFETY: the source-bound end is within this unchanged slice.
        *target = unsafe { bytes.get_unchecked_mut(end.count().get() as usize..) };
    }
}

// SAFETY: the view retains its immutable byte borrow and original source;
// consumption only selects the admitted suffix of that same borrow.
unsafe impl<S: ?Sized> Consume for buffer::View<'_, S> {
    fn consume<const MAX: u32>(prefix: Consumed<'_, '_, Self, MAX>) {
        let (view, end) = prefix.into_parts();
        // SAFETY: the prefix owns the exact view and its validated bound.
        view.bytes = unsafe { view.bytes.get_unchecked(end.count().get() as usize..) };
    }
}

/// One owner edge that can retain the same backing independently.
/// # Safety
/// `try_retain` preserves backing identity, access, and branded lifetimes.
pub unsafe trait Retain: Sized {
    fn try_retain(&self) -> Result<Self, rc::Error>;
}

// SAFETY: Some retains the same owner and lifetime; None retains no backing.
unsafe impl<T: Retain> Retain for Option<T> {
    fn try_retain(&self) -> Result<Self, rc::Error> {
        self.as_ref().map(T::try_retain).transpose()
    }
}

/// Retains a complete source whose UTF-8 was already validated.
/// # Safety
/// The source's entire unchanged visible range is valid UTF-8.
pub unsafe fn utf8<O: Source>(owner: O) -> buffer::storage::Str<O> {
    // SAFETY: the caller proves UTF-8 for this complete immutable source.
    unsafe { buffer::storage::Str::new(owner) }
}

/// # Safety
/// Preserve the owner, visible range, resident capacity, and owner lifetimes.
/// Allocate, copy, and clone nothing; do not introduce sharing.
pub unsafe trait Convert<T> {
    fn into(self) -> T;
}

// SAFETY: identity retains the complete original value and its byte storage.
unsafe impl<T> Convert<T> for T {
    fn into(self) -> T {
        self
    }
}

/// Storage retaining one exact nonempty physical owner.
pub trait Unary<T> {
    fn item(&self) -> &segments::Item<T>;
}

/// # Safety
/// Source and iterator totals remain equal and unchanged through exhaustion: exact nonoverflowing owner count, visible byte sum and resident charge sum.
/// Success yields exactly that many nonempty owners, never more on failure; preserve backing, borrows and `Output` cardinality (`Many` requires at least two).
pub unsafe trait Sequence<T: buffer::raw::Segment>: Sized + Total {
    type Item;
    type Error;
    type Output: Assemble<T>;
    fn owners(self) -> impl Iterator<Item = Self::Item> + Total;

    fn reject(self, error: crate::alloc::Error) -> Self::Error;
}

pub trait Assemble<T: buffer::raw::Segment>: Sized {
    fn finish<S: Sequence<T, Output = Self>>(input: segments::Built<T, S>) -> Self;
}

pub trait Total {
    fn count(&self) -> segments::Count;
    fn is_empty(&self) -> bool {
        self.len() == 0
    }

    fn len(&self) -> usize;
    fn resident(&self) -> usize;
}

impl<I, M: Total> Total for buffer::raw::Batch<I, M> {
    fn count(&self) -> segments::Count {
        self.total.count()
    }
    fn len(&self) -> usize {
        self.total.len()
    }
    fn resident(&self) -> usize {
        self.total.resident()
    }
}

/// Immutable scattered bytes with an exact length and borrowed cursor.
///
/// # Safety
/// `start()` has `len()` remaining; offset cursors exist through the end.
pub unsafe trait Chunks {
    type Cursor<'data>: Scatter<'data>
    where
        Self: 'data;

    fn is_empty(&self) -> bool {
        self.len() == 0
    }

    fn len(&self) -> usize;
    fn start(&self) -> Self::Cursor<'_>;
    fn cursor(&self, offset: usize) -> Option<Self::Cursor<'_>> {
        Scatter::seek(self.start(), offset)
    }
}

/// An owned admission retaining its operation proof until storage accepts it.
/// # Safety
/// Move only the original owner; the projection must retain its lifetime and exactly `bytes()`.
pub unsafe trait Input<T> {
    fn bytes(&self) -> &[u8];
    fn into_item(self) -> (T, impl for<'a> FnOnce(&'a mut T) -> &'a mut [u8]);
}

/// Projects fixed-residency items admitted by their original native context.
pub trait Fixed<T> {
    fn resident(&self) -> NonZero<u32>;

    /// # Safety
    /// Own this context's unique item; its full retained backing fits `resident()`.
    /// Its prefix is initialized; `limit` is that ceiling and backing/layout live for `'a`.
    unsafe fn project<'a, 'id>(&'a self, item: &'a mut T, limit: Limit<'id>)
    -> Projection<'a, 'id>;

    /// # Safety
    /// Own its context's unique initialized item; full backing fits `resident()` and lives for `'d`.
    /// Its layout and `resident()` match this arena's unchanged layout and resident ceiling.
    unsafe fn insert<'d>(
        vacant: provided::Vacant<'d, T, (), &'d Self>,
        item: T,
    ) -> provided::Unique<'d>
    where
        Self: Sized,
    {
        let context = vacant.context();
        vacant.insert_with(item, |item, limit| {
            // SAFETY: native admission supplied this exact context and item;
            // the arena retains both and issues the context's resident ceiling.
            unsafe { context.project(item, limit) }
        })
    }
}

/// Exact arenas entered together with their external accounting generation.
/// # Safety
/// Roots must borrow their original arenas. The terminal must reclaim every item and charge.
pub unsafe trait Arenas: Unpin {
    type Roots<'d>: 'd
    where
        Self: 'd;

    /// # Safety
    /// Invoke the terminal once after all issued access ends, while every charged budget lives.
    unsafe fn enter(&mut self) -> (Self::Roots<'_>, impl FnOnce() + '_);
}

// SAFETY: disjoint owners retain their exact roots and terminate in reverse order.
unsafe impl<A: Arenas, B: Arenas> Arenas for (A, B) {
    type Roots<'d>
        = (A::Roots<'d>, B::Roots<'d>)
    where
        Self: 'd;

    unsafe fn enter(&mut self) -> (Self::Roots<'_>, impl FnOnce() + '_) {
        // SAFETY: the caller supplies the same generation and terminal ordering for both owners.
        let (first, finish) = unsafe { self.0.enter() };
        let (second, end) = unsafe { self.1.enter() };
        ((first, second), move || {
            end();
            finish();
        })
    }
}

/// A mutable projection proven to fit its resident byte ceiling.
#[repr(transparent)]
pub struct Projection<'a, 'id> {
    bytes: &'a mut [u8],
    operation: marker::PhantomData<fn(&'id mut ()) -> &'id mut ()>,
}

impl<'a> Projection<'a, '_> {
    pub(in crate::buffer) const fn into_inner(self) -> &'a mut [u8] {
        self.bytes
    }
}

/// Immutable UTF-8 in one exact allocation bound to its paying domain.
pub type Text<'d> = buffer::storage::Str<resident::Exact<'d>>;

/// Exclusive initialized suffix retained through its output operation.
pub struct Output<'a, L = pool::Layout> {
    target: &'a mut pool::Span<owners::Plain<L>, pool::Owned, state::Initialized>,
}

impl<'a, L> Output<'a, L> {
    pub(in crate::buffer) const fn new(
        target: &'a mut pool::Span<owners::Plain<L>, pool::Owned, state::Initialized>,
    ) -> Self {
        Self { target }
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    pub fn len(&self) -> usize {
        self.target.capacity().get() as usize - self.target.len()
    }

    pub fn as_mut_slice(&mut self) -> &mut [u8] {
        let (data, start) = buffer::raw::Prefix::prefix(self.target);
        // SAFETY: Initialized retains every byte of this exact exclusive suffix.
        unsafe { range_mut(data.cast(), start as usize, self.len()) }
    }

    /// # Safety
    /// The foreign operation must prove `len` fits this unchanged output.
    pub unsafe fn complete(self, len: NonZero<usize>) -> pool::Complete<'a, L> {
        unsafe { pool::Complete::new(self.target, len) }
    }
}

const _: () = assert!(size_of::<Output<'_>>() == size_of::<usize>());

/// An adapter exposing its complete unchanged byte extent through one inner owner.
/// # Safety
/// Projections retain the exact backing, lifetime, bytes, and restriction invariants.
pub unsafe trait Project<T: ?Sized>: AsRef<[u8]> {
    fn as_inner(&self) -> &T;
    /// # Safety
    /// Only restrict the extent, preserving its nonempty owner and empty policy.
    unsafe fn as_inner_mut(&mut self) -> &mut T;
}

// SAFETY: the transparent bound exposes the unchanged extent of its original owner.
unsafe impl<T: AsRef<[u8]>> Project<T> for bounded::Value<T> {
    fn as_inner(&self) -> &T {
        &self.0
    }
    unsafe fn as_inner_mut(&mut self) -> &mut T {
        &mut self.0
    }
}

/// # Safety
/// Shared access preserves its exact backing, range, and contents until exclusive mutation.
/// Writes preserve the initialized prefix; successful writes append their complete input.
pub unsafe trait Source: AsRef<[u8]> {}

// SAFETY: the stronger cursor contract requires this stable-source contract.
unsafe impl<T: buffer::raw::Consume> Source for T {}

/// # Safety
/// `AsMut::as_mut` returns the full Source range with its pre-call pointer and length, unchanged after the loan.
/// The call changes no owner, descriptor, extent, or bytes; caller slice writes are allowed, and other exclusive operations retain their own contracts.
pub unsafe trait Mutable: Source + AsMut<[u8]> {}

/// # Safety
/// Every `provided::Mutable::try_take` implementation returns the exact requested prefix
/// and retains its disjoint suffix, contents, owner and lifetimes; failure changes nothing.
pub unsafe trait Split: Mutable {}

/// # Safety
/// Freezing retains the exact backing, initialized range, contents, owner and lifetimes.
/// It allocates, copies and clones nothing and introduces no sharing.
pub unsafe trait Freeze: Source + Sized {
    type Output: Source;

    fn freeze(self) -> Self::Output;
}

/// # Safety
/// Retain source borrows and stable `len`; initialize exactly that prefix once.
/// Never read uninitialized output, retain its access, or invalidate source borrows.
pub unsafe trait Initializer {
    fn len(&self) -> usize;

    fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// # Safety
    /// `target` exclusively retains at least `self.len()` writable bytes.
    unsafe fn initialize(&self, target: *mut mem::MaybeUninit<u8>);
}

pub(in crate::buffer) use store::{
    Backing, Debit, Growth, Header, Heap, Policy, Rc, Space, Token, Unique,
};

mod length;
pub use length::Length;
mod limit;
pub use limit::Limit;
mod consumed;
pub use consumed::Consumed;
pub(in crate::buffer) use consumed::Selection;
mod slice;
pub use slice::Slice;

/// One proven non-empty span borrowed from its source.
#[repr(transparent)]
pub struct Chunk<'data, const MAX: usize = { usize::MAX }> {
    bytes: &'data [u8],
}

/// Borrows exactly `len` initialized bytes at `start` of one allocation.
/// # Safety
/// `pointer.add(start)` must be valid. A nonempty selected range must contain
/// `len` initialized bytes in one allocation retained for `'a`, without
/// overlapping exclusive access. The nonnull selected pointer and extent must
/// satisfy [`from_raw_parts`], including its `isize` bound and empty-range rules.
pub(in crate::buffer) unsafe fn range<'a>(
    pointer: *const u8,
    start: usize,
    len: usize,
) -> &'a [u8] {
    // SAFETY: the caller proved this exact initialized subrange of one allocation.
    unsafe { from_raw_parts(pointer.add(start), len) }
}

/// Borrows the same initialized subrange exclusively.
///
/// # Safety
/// As [`range`], with exclusive access to the selected range for `'a`.
/// Other accesses during that loan must be disjoint; the selected pointer and
/// extent must satisfy [`from_raw_parts_mut`], including its empty-range rules.
pub(in crate::buffer) unsafe fn range_mut<'a>(
    pointer: *mut u8,
    start: usize,
    len: usize,
) -> &'a mut [u8] {
    // SAFETY: the caller proved this exact initialized subrange and its exclusive retention.
    unsafe { from_raw_parts_mut(pointer.add(start), len) }
}

impl<'data, const MAX: usize> Chunk<'data, MAX> {
    /// Retains one exact initialized span from its original immutable borrow.
    ///
    /// # Safety
    /// `bytes` is non-empty and `bytes.len() <= MAX`.
    pub unsafe fn new(bytes: &'data [u8]) -> Self {
        Self { bytes }
    }

    pub const fn bytes(&self) -> &'data [u8] {
        self.bytes
    }

    pub const fn len(&self) -> num::bounded::NonZeroUsize<1, MAX> {
        // SAFETY: construction proves the retained span is non-empty and bounded by MAX.
        unsafe { num::bounded::raw::nonzero_usize(self.bytes.len()) }
    }
}

const _: () = {
    assert!(mem::size_of::<Chunk<'static>>() == mem::size_of::<&[u8]>());
    assert!(mem::size_of::<Option<Chunk<'static>>>() == mem::size_of::<&[u8]>());
};

/// Copies slices consecutively without publishing a length.
///
/// # Safety
/// The finite source sequence's total length must fit `isize` without overflow.
/// `target` must be nonnull and retain exclusive writable storage for that range
/// in one allocation; an aligned dangling pointer is allowed for a zero total.
/// Sources must remain valid and disjoint from the entire destination throughout
/// the call. Iterator callbacks must preserve these conditions and must not
/// access the destination.
pub unsafe fn copy_slices<'a>(sources: impl IntoIterator<Item = &'a [u8]>, target: *mut u8) {
    let mut offset = 0;
    for source in sources {
        // SAFETY: the caller retains all sources and their complete disjoint target.
        unsafe { source.copy_to(target.add(offset)) };
        offset += source.len();
    }
}