o3 0.16.3

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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
use std::{fmt, hash, ops};

use crate::buffer::{self, RangeExt as _, resident, storage, write};

/// A uniquely owned, non-growing byte allocation. `CAP == 0` selects an exact
/// runtime capacity; a nonzero `CAP` fixes it in the type without added storage.
pub struct Owned<const CAP: u32 = 0> {
    storage: storage::raw::AllocationMut,
    len: u32,
}

impl Owned {
    pub fn try_with_capacity(capacity: usize) -> Result<Self, buffer::CapacityError> {
        let capacity = u32::try_from(capacity)
            .map_err(|_| buffer::CapacityError::new(capacity, u32::MAX as usize))?;
        Ok(Self::with_capacity_u32(capacity))
    }

    pub fn try_build_exact<E>(
        capacity: usize,
        build: impl FnOnce(&mut write::SpareWriter<'_>) -> Result<(), E>,
    ) -> Result<Self, storage::BuildError<E>> {
        use crate::buffer::storage::BuildError;
        let mut value = Self::try_with_capacity(capacity).map_err(BuildError::Capacity)?;
        let mut writer = value.spare_writer();
        build(&mut writer).map_err(BuildError::Build)?;
        let actual = writer.finish();
        if actual != capacity {
            return Err(BuildError::LengthMismatch {
                expected: capacity,
                actual,
            });
        }
        Ok(value)
    }

    pub fn try_filled(len: usize, byte: u8) -> Result<Self, buffer::CapacityError> {
        let mut value = Self::try_with_capacity(len)?;
        value.storage.bytes_mut().fill(byte);
        value.len = len as u32;
        Ok(value)
    }
}

impl<const CAP: u32> Owned<CAP> {
    #[must_use]
    pub fn new() -> Self {
        Self::with_capacity_u32(CAP)
    }

    fn with_capacity_u32(capacity: u32) -> Self {
        use crate::buffer::storage::raw::AllocationMut;
        Self {
            storage: AllocationMut::with_capacity_u32(capacity),
            len: 0,
        }
    }

    pub fn capacity(&self) -> usize {
        if CAP == 0 {
            self.storage.capacity()
        } else {
            CAP as usize
        }
    }

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

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

    pub fn as_slice(&self) -> &[u8] {
        self.storage.initialized(self.len())
    }

    pub fn as_mut_slice(&mut self) -> &mut [u8] {
        self.storage.bytes_mut().initialized(self.len as usize)
    }

    pub fn try_extend(&mut self, src: &[u8]) -> Result<(), buffer::CapacityError> {
        let start = self.len();
        let capacity = self.capacity();
        let end = start
            .checked_add(src.len())
            .ok_or_else(|| buffer::CapacityError::new(usize::MAX, capacity))?;
        if end > capacity {
            return Err(buffer::CapacityError::new(end, capacity));
        }
        self.storage.bytes_mut().copy_from_slice(start, src);
        self.len = end as u32;
        Ok(())
    }

    pub fn try_extend_from_slices<const N: usize>(
        &mut self,
        slices: [&[u8]; N],
    ) -> Result<(), buffer::CapacityError> {
        let start = self.len();
        let capacity = self.capacity();
        let mut end = start;
        for slice in &slices {
            end = end
                .checked_add(slice.len())
                .ok_or_else(|| buffer::CapacityError::new(usize::MAX, capacity))?;
            if end > capacity {
                return Err(buffer::CapacityError::new(end, capacity));
            }
        }
        let mut offset = start;
        for slice in slices {
            self.storage.bytes_mut().copy_from_slice(offset, slice);
            offset += slice.len();
        }
        self.len = end as u32;
        Ok(())
    }

    pub fn try_push(&mut self, byte: u8) -> Result<(), buffer::CapacityError> {
        let offset = self.len();
        let capacity = self.capacity();
        if offset == capacity {
            return Err(buffer::CapacityError::new(
                offset.saturating_add(1),
                capacity,
            ));
        }
        self.storage.bytes_mut().write_byte(offset, byte);
        self.len += 1;
        Ok(())
    }

    pub fn spare_writer(&mut self) -> write::SpareWriter<'_> {
        self.storage.bytes_mut().spare_writer(&mut self.len)
    }

    #[must_use]
    pub fn freeze(self) -> Shared {
        let Self { storage, len } = self;
        if len == 0 {
            return Shared::new();
        }
        // SAFETY: every constructor and writer maintains `len <= storage.capacity()`.
        let span = unsafe {
            use crate::buffer::storage::raw::Span;
            Span::new_unchecked(storage.freeze(), 0, len)
        };
        Shared::from_span(span)
    }
}

impl<const CAP: u32> Default for Owned<CAP> {
    fn default() -> Self {
        Self::new()
    }
}

impl<const CAP: u32> Clone for Owned<CAP> {
    fn clone(&self) -> Self {
        let mut clone = Self::with_capacity_u32(self.storage.capacity() as u32);
        if self.len != 0 {
            clone
                .storage
                .bytes_mut()
                .copy_from_allocation(0, &self.storage, 0, self.len());
            clone.len = self.len;
        }
        clone
    }
}

impl<const CAP: u32> AsRef<[u8]> for Owned<CAP> {
    fn as_ref(&self) -> &[u8] {
        self.as_slice()
    }
}

impl<const CAP: u32> buffer::PrefixLength for Owned<CAP> {
    fn prefix_len(&self) -> usize {
        self.len()
    }
}

impl<const CAP: u32> AsMut<[u8]> for Owned<CAP> {
    fn as_mut(&mut self) -> &mut [u8] {
        self.as_mut_slice()
    }
}

impl<const CAP: u32> ops::Deref for Owned<CAP> {
    type Target = [u8];

    fn deref(&self) -> &Self::Target {
        self.as_slice()
    }
}

impl<const CAP: u32> ops::DerefMut for Owned<CAP> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.as_mut_slice()
    }
}

impl<const CAP: u32, const OTHER: u32> PartialEq<Owned<OTHER>> for Owned<CAP> {
    fn eq(&self, other: &Owned<OTHER>) -> bool {
        self.as_slice() == other.as_slice()
    }
}

impl<const CAP: u32> Eq for Owned<CAP> {}

impl<const CAP: u32> hash::Hash for Owned<CAP> {
    fn hash<H: hash::Hasher>(&self, state: &mut H) {
        self.as_slice().hash(state);
    }
}

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

const VEC_ZERO_COPY_MIN: usize = 512;

/// An immutable byte view whose materialized pointer keeps ownership off reads.
#[derive(Clone)]
pub struct Shared {
    ptr: *const u8,
    len: usize,
    owner: storage::raw::Owner,
}

const _: () = assert!(size_of::<Shared>() == size_of::<usize>() * 3);

impl Shared {
    #[must_use]
    pub const fn new() -> Self {
        use std::ptr::NonNull;

        Self {
            ptr: NonNull::<u8>::dangling().as_ptr(),
            len: 0,
            owner: storage::raw::Owner::NONE,
        }
    }

    #[must_use]
    pub const fn from_static(s: &'static [u8]) -> Self {
        Self {
            ptr: s.as_ptr(),
            len: s.len(),
            owner: storage::raw::Owner::NONE,
        }
    }

    pub(in crate::buffer) fn from_span(span: storage::raw::Span) -> Self {
        let (allocation, ptr, len) = span.into_parts();
        Self {
            ptr,
            len,
            owner: storage::raw::Owner::from_allocation(allocation),
        }
    }

    pub(in crate::buffer) fn from_resident_span(span: storage::raw::Span<resident::Lease>) -> Self {
        let (allocation, ptr, len) = span.into_parts();
        Self {
            ptr,
            len,
            owner: storage::raw::Owner::from_resident(allocation),
        }
    }

    pub(super) fn from_vec(buf: Vec<u8>) -> Self {
        if buf.is_empty() {
            return Self::new();
        }
        if buf.len() < VEC_ZERO_COPY_MIN {
            return Self::copy_from_slice(&buf);
        }
        Self::from_vec_owner(buf)
    }

    fn from_vec_owner(buf: Vec<u8>) -> Self {
        use std::rc::Rc;

        let buf = Rc::new(buf);
        let ptr = buf.as_ptr();
        let len = buf.len();
        Self {
            ptr,
            len,
            owner: storage::raw::Owner::from_vec(buf),
        }
    }

    #[must_use]
    pub fn copy_from_slice(s: &[u8]) -> Self {
        use crate::buffer::storage::raw::Span;
        if s.is_empty() {
            return Self::new();
        }
        match Span::copy_from_slice(s) {
            Some(span) => Self::from_span(span),
            None => Self::from_vec_owner(s.to_vec()),
        }
    }

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

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

    /// Bytes retained by the complete backing allocation.
    /// Static bytes retain no allocation.
    pub fn resident_bytes(&self) -> usize {
        self.owner.resident_bytes()
    }

    pub fn as_slice(&self) -> &[u8] {
        unsafe {
            use std::slice::from_raw_parts;
            from_raw_parts(self.ptr, self.len)
        }
    }

    #[must_use]
    pub fn get(&self, range: impl ops::RangeBounds<usize>) -> Option<Self> {
        use std::ops::Bound;

        let start = match range.start_bound() {
            Bound::Included(&n) => n,
            Bound::Excluded(&n) => n.checked_add(1)?,
            Bound::Unbounded => 0,
        };
        let end = match range.end_bound() {
            Bound::Included(&n) => n.checked_add(1)?,
            Bound::Excluded(&n) => n,
            Bound::Unbounded => self.len,
        };
        let range = start..end;
        if !range.is_within(self.len) {
            return None;
        }
        if range.is_empty() {
            return Some(Self::new());
        }
        Some(Self {
            ptr: unsafe { self.ptr.add(range.start) },
            len: range.len(),
            owner: self.owner.clone(),
        })
    }

    pub(in crate::buffer) fn try_slice_in_place(&mut self, range: ops::Range<usize>) -> bool {
        if !range.is_within(self.len) {
            return false;
        }
        if range.is_empty() {
            self.clear();
            return true;
        }
        self.ptr = unsafe { self.ptr.add(range.start) };
        self.len = range.len();
        true
    }

    pub fn try_advance(&mut self, n: usize) -> bool {
        let len = self.len;
        self.try_slice_in_place(n..len)
    }

    pub(in crate::buffer) fn consume_valid(&mut self, amount: usize) {
        debug_assert!(amount <= self.len);
        if amount == self.len {
            self.clear();
            return;
        }
        self.ptr = unsafe { self.ptr.add(amount) };
        self.len -= amount;
    }
    pub fn clear(&mut self) {
        *self = Self::new();
    }
}

impl Default for Shared {
    fn default() -> Self {
        Self::new()
    }
}

impl buffer::Seal for Shared {}

impl AsRef<[u8]> for Shared {
    fn as_ref(&self) -> &[u8] {
        self.as_slice()
    }
}

impl buffer::PrefixLength for Shared {
    fn prefix_len(&self) -> usize {
        self.len()
    }
}

impl buffer::PrefixConsumer for Shared {
    fn consume_validated_prefix(&mut self, proof: buffer::PrefixProof) {
        self.consume_valid(proof.amount());
    }
}

impl ops::Deref for Shared {
    type Target = [u8];

    fn deref(&self) -> &[u8] {
        self.as_slice()
    }
}

impl From<&'static [u8]> for Shared {
    fn from(value: &'static [u8]) -> Self {
        Self::from_static(value)
    }
}

impl<const N: usize> From<&'static [u8; N]> for Shared {
    fn from(value: &'static [u8; N]) -> Self {
        Self::from_static(value)
    }
}

impl From<Vec<u8>> for Shared {
    fn from(value: Vec<u8>) -> Self {
        Self::from_vec(value)
    }
}

impl<const CAP: u32> From<storage::Owned<CAP>> for Shared {
    fn from(value: storage::Owned<CAP>) -> Self {
        value.freeze()
    }
}

impl From<String> for Shared {
    fn from(value: String) -> Self {
        Self::from_vec(value.into_bytes())
    }
}

impl From<&str> for Shared {
    fn from(value: &str) -> Self {
        Self::copy_from_slice(value.as_bytes())
    }
}

impl PartialEq for Shared {
    fn eq(&self, other: &Self) -> bool {
        self.as_slice() == other.as_slice()
    }
}

impl PartialEq<[u8]> for Shared {
    fn eq(&self, other: &[u8]) -> bool {
        self.as_slice() == other
    }
}

impl PartialEq<&[u8]> for Shared {
    fn eq(&self, other: &&[u8]) -> bool {
        self.as_slice() == *other
    }
}

impl Eq for Shared {}

impl hash::Hash for Shared {
    fn hash<H: hash::Hasher>(&self, state: &mut H) {
        self.as_slice().hash(state);
    }
}

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