o3 0.11.0

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
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
mod bytes;
pub mod inline;
mod owned;
mod pool;
pub mod queue;
mod shared;
mod storage;
pub mod view;

use std::{
    error::Error,
    fmt,
    mem::MaybeUninit,
    ops::Range,
    ptr::{self, NonNull, copy_nonoverlapping},
    slice,
};

pub use bytes::{Borrowed, ByteSink, Bytes, RetainBytes, Retained, SliceWriter};
pub use owned::Owned;
pub use pool::{
    Cursor, FixedPoolCapacity, Frozen, Initialized, Layout, Lease, Plan, Pool, PoolCapacity,
    RuntimePoolCapacity, State, Uninitialized,
};
pub use shared::{Shared, strings::SharedStr};
use storage::refs::LocalRefCount;

use crate::ThreadBound;

/// Reports the logical byte prefix that an exclusive owner may consume.
pub trait PrefixLength {
    fn prefix_len(&self) -> usize;
}

/// Owns a logical byte prefix that can be consumed after it is proven to fit.
/// Only [`ValidatedPrefix`] can construct the proof passed to this method.
pub trait PrefixConsumer: PrefixLength {
    fn consume_validated_prefix(&mut self, proof: PrefixProof);

    fn try_consume_prefix(
        &mut self,
        amount: usize,
    ) -> Result<ValidatedPrefix<'_, Self>, CapacityError> {
        ValidatedPrefix::try_new(self, amount)
    }

    fn consume_prefix_up_to(&mut self, requested: usize) -> usize {
        let prefix = ValidatedPrefix::up_to(self, requested);
        let amount = prefix.len();
        prefix.commit();
        amount
    }
}

/// An unforgeable proof that one prefix fits its exclusively borrowed owner.
#[doc(hidden)]
pub struct PrefixProof {
    amount: usize,
}

impl PrefixProof {
    fn new(amount: usize) -> Self {
        Self { amount }
    }

    pub const fn amount(&self) -> usize {
        self.amount
    }
}

/// Proof that `amount` fits one exclusively borrowed target prefix.
#[must_use]
pub struct ValidatedPrefix<'a, T: PrefixConsumer + ?Sized> {
    target: &'a mut T,
    proof: PrefixProof,
}

impl<'a, T: PrefixConsumer + ?Sized> ValidatedPrefix<'a, T> {
    fn try_new(target: &'a mut T, amount: usize) -> Result<Self, CapacityError> {
        let available = target.prefix_len();
        if amount > available {
            return Err(CapacityError::new(amount, available));
        }
        Ok(Self {
            target,
            proof: PrefixProof::new(amount),
        })
    }

    /// Proves the largest prefix no longer than `requested`.
    fn up_to(target: &'a mut T, requested: usize) -> Self {
        let amount = requested.min(target.prefix_len());
        Self {
            target,
            proof: PrefixProof::new(amount),
        }
    }

    const fn len(&self) -> usize {
        self.proof.amount()
    }
}

impl<T: PrefixConsumer + ?Sized> ValidatedPrefix<'_, T> {
    /// Applies the validated mutation exactly once.
    pub fn commit(self) {
        self.target.consume_validated_prefix(self.proof);
    }
}

pub const BLOCK_CAPACITY: u32 = 64 * 1024;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PoolLayoutError {
    ZeroCapacity,
    SlotOverflow,
    CapacityOverflow,
}

impl fmt::Display for PoolLayoutError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::ZeroCapacity => f.write_str("buffer pool capacity must be positive"),
            Self::SlotOverflow => f.write_str("buffer pool slot count overflow"),
            Self::CapacityOverflow => f.write_str("buffer pool allocation size overflow"),
        }
    }
}

impl Error for PoolLayoutError {}

#[derive(Clone, Copy, PartialEq, Eq)]
pub struct CapacityError {
    attempted: usize,
    capacity: usize,
}

impl CapacityError {
    const fn new(attempted: usize, capacity: usize) -> Self {
        Self {
            attempted,
            capacity,
        }
    }
}

impl fmt::Debug for CapacityError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("CapacityError")
            .field("attempted", &self.attempted)
            .field("capacity", &self.capacity)
            .finish()
    }
}

impl fmt::Display for CapacityError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "capacity exceeded: attempted {}, capacity {}",
            self.attempted, self.capacity
        )
    }
}

impl Error for CapacityError {}

fn checked_append_len<const N: usize>(
    start: usize,
    capacity: usize,
    slices: &[&[u8]; N],
) -> Result<usize, CapacityError> {
    let mut end = start;
    for slice in slices {
        end = end
            .checked_add(slice.len())
            .ok_or_else(|| CapacityError::new(usize::MAX, capacity))?;
        if end > capacity {
            return Err(CapacityError::new(end, capacity));
        }
    }
    Ok(end)
}

fn write_vec_slices<const N: usize>(out: &mut Vec<u8>, slices: [&[u8]; N]) {
    let additional = slices
        .iter()
        .fold(0usize, |len, slice| len.saturating_add(slice.len()));
    let start = out.len();
    out.reserve(additional);
    let mut offset = start;
    for slice in slices {
        // SAFETY: the aggregate reserve covers every copy and safe borrowing
        // prevents the sources from aliasing this vector.
        unsafe { copy_nonoverlapping(slice.as_ptr(), out.as_mut_ptr().add(offset), slice.len()) };
        offset += slice.len();
    }
    // SAFETY: every byte in `start..offset` was initialized above.
    unsafe { out.set_len(offset) };
}

/// Failure to construct an exact-length [`Owned`] buffer.
///
/// [`Owned`]: crate::buffer::Owned
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ExactBuildError<E> {
    /// The requested length exceeds the buffer representation.
    Capacity(CapacityError),
    /// The encoder returned an error.
    Build(E),
    /// The encoder completed without initializing the requested number of bytes.
    LengthMismatch { expected: usize, actual: usize },
}

impl<E: fmt::Display> fmt::Display for ExactBuildError<E> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Capacity(error) => error.fmt(f),
            Self::Build(error) => error.fmt(f),
            Self::LengthMismatch { expected, actual } => {
                write!(
                    f,
                    "exact buffer length mismatch: expected {expected}, wrote {actual}"
                )
            }
        }
    }
}

impl<E> Error for ExactBuildError<E>
where
    E: Error + 'static,
{
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            Self::Capacity(error) => Some(error),
            Self::Build(error) => Some(error),
            Self::LengthMismatch { .. } => None,
        }
    }
}

pub struct SpareWriter<'a> {
    ptr: NonNull<MaybeUninit<u8>>,
    capacity: usize,
    written: usize,
    target: &'a mut u32,
    _thread: ThreadBound,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ExactWriteError {
    expected: usize,
    actual: usize,
}

impl fmt::Display for ExactWriteError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "exact write incomplete: expected {}, wrote {}",
            self.expected, self.actual
        )
    }
}

impl Error for ExactWriteError {}

/// An exact-length write reservation that rolls back unless committed.
/// Safe writes are confined to the reserved extent.
#[must_use = "dropping a write transaction rolls its bytes back"]
pub struct WriteTxn<'writer, 'target> {
    writer: &'writer mut SpareWriter<'target>,
    start: usize,
    end: usize,
    committed: bool,
}

trait RangeExt {
    fn is_within(&self, len: usize) -> bool;
}

impl RangeExt for Range<usize> {
    fn is_within(&self, len: usize) -> bool {
        self.start <= self.end && self.end <= len
    }
}

impl<'a> SpareWriter<'a> {
    pub fn len(&self) -> usize {
        self.written
    }

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

    pub fn as_mut_slice(&mut self) -> &mut [u8] {
        unsafe { slice::from_raw_parts_mut(self.ptr.as_ptr().cast(), self.written) }
    }

    pub fn truncate(&mut self, len: usize) {
        self.written = self.written.min(len);
    }

    /// Reserves exactly `len` bytes for an all-or-nothing safe write.
    pub fn try_transaction(&mut self, len: usize) -> Result<WriteTxn<'_, 'a>, CapacityError> {
        let end = self
            .written
            .checked_add(len)
            .ok_or_else(|| CapacityError::new(usize::MAX, self.capacity))?;
        if end > self.capacity {
            return Err(CapacityError::new(end, self.capacity));
        }
        Ok(WriteTxn {
            start: self.written,
            end,
            writer: self,
            committed: false,
        })
    }

    pub fn try_push(&mut self, byte: u8) -> Result<(), CapacityError> {
        if self.written == self.capacity {
            return Err(CapacityError::new(self.written + 1, self.capacity));
        }
        unsafe {
            self.ptr
                .as_ptr()
                .add(self.written)
                .write(MaybeUninit::new(byte))
        };
        self.written += 1;
        Ok(())
    }

    /// Appends one contiguous slice after validating its complete length.
    pub fn try_extend(&mut self, src: &[u8]) -> Result<(), CapacityError> {
        let end = self
            .written
            .checked_add(src.len())
            .ok_or_else(|| CapacityError::new(usize::MAX, self.capacity))?;
        if end > self.capacity {
            return Err(CapacityError::new(end, self.capacity));
        }
        unsafe {
            copy_nonoverlapping(
                src.as_ptr(),
                self.ptr.as_ptr().add(self.written).cast(),
                src.len(),
            )
        };
        self.written = end;
        Ok(())
    }

    /// Appends every slice after validating their aggregate length.
    ///
    /// On failure, neither the writer length nor its target length changes.
    pub fn try_extend_from_slices<const N: usize>(
        &mut self,
        slices: [&[u8]; N],
    ) -> Result<(), CapacityError> {
        let end = checked_append_len(self.written, self.capacity, &slices)?;
        let mut offset = self.written;
        for src in slices {
            unsafe {
                copy_nonoverlapping(
                    src.as_ptr(),
                    self.ptr.as_ptr().add(offset).cast(),
                    src.len(),
                )
            };
            offset += src.len();
        }
        self.written = end;
        Ok(())
    }

    pub fn finish(self) -> usize {
        self.written
    }

    unsafe fn new(ptr: *mut MaybeUninit<u8>, capacity: usize, target: &'a mut u32) -> Self {
        debug_assert!(capacity <= (u32::MAX - *target) as usize);
        Self {
            ptr: unsafe { NonNull::new_unchecked(ptr) },
            capacity,
            written: 0,
            target,
            _thread: ThreadBound::NEW,
        }
    }

    fn commit(&mut self) {
        if self.is_empty() {
            return;
        }
        *self.target = self.target.wrapping_add(self.written as u32);
        self.written = 0;
    }
}

impl WriteTxn<'_, '_> {
    fn written(&self) -> usize {
        self.writer.written - self.start
    }

    pub fn remaining(&self) -> usize {
        self.end - self.writer.written
    }

    pub fn try_extend(&mut self, src: &[u8]) -> Result<(), CapacityError> {
        if src.len() > self.remaining() {
            return Err(CapacityError::new(
                self.written().saturating_add(src.len()),
                self.end - self.start,
            ));
        }
        self.writer.try_extend(src)
    }

    /// Returns the initialized portion of this transaction for in-place work.
    pub fn initialized_mut(&mut self) -> &mut [u8] {
        &mut self.writer.as_mut_slice()[self.start..]
    }

    /// Makes the exact initialized reservation visible in the parent writer.
    pub fn commit(mut self) -> Result<(), ExactWriteError> {
        let actual = self.written();
        let expected = self.end - self.start;
        if actual != expected {
            return Err(ExactWriteError { expected, actual });
        }
        self.committed = true;
        Ok(())
    }
}

impl Drop for WriteTxn<'_, '_> {
    fn drop(&mut self) {
        if !self.committed {
            self.writer.truncate(self.start);
        }
    }
}

impl Drop for SpareWriter<'_> {
    fn drop(&mut self) {
        self.commit();
    }
}

/// # Safety
/// `buf` is valid through `*tail`, and `*head <= *tail`.
unsafe fn compact(buf: *mut MaybeUninit<u8>, head: &mut u32, tail: &mut u32) {
    if *head == 0 {
        return;
    }
    let len = (*tail - *head) as usize;
    if len != 0 {
        unsafe { ptr::copy(buf.add(*head as usize), buf, len) };
    }
    *head = 0;
    *tail = len as u32;
}

/// # Safety
/// `amount <= *tail - *head`.
unsafe fn consume(head: &mut u32, tail: &mut u32, amount: usize) {
    *head = head.wrapping_add(amount as u32);
    if *head == *tail {
        *head = 0;
        *tail = 0;
    }
}