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
use std::{fmt, hash, ops};

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

#[doc(hidden)]
pub trait Storage: buffer::Seal {
    fn as_slice(&self) -> &[u8];
}

/// Bytes with a statically selected storage policy and no wrapper overhead.
#[repr(transparent)]
pub struct Bytes<S> {
    storage: S,
}

/// A non-owning byte slice valid for `'a`.
#[derive(Clone, Copy, Default)]
#[repr(transparent)]
pub struct Borrowed<'a> {
    slice: &'a [u8],
}

#[derive(Clone)]
enum RetainedRepr {
    Frozen {
        frozen: buffer::Frozen,
        start: u32,
        len: u32,
    },
    Shared(storage::Shared),
}

/// Bytes retained beyond their callback through pooled or shared ownership.
#[derive(Clone)]
pub struct Retained {
    repr: RetainedRepr,
}

/// Pool-backed bytes whose allocation lifetime remains tied to the pool.
#[derive(Clone)]
pub struct Pooled<'pool> {
    frozen: buffer::Frozen<pool::Borrowed<'pool>>,
    start: u32,
    len: u32,
}

/// Read-only bytes that can be retained beyond their current callback.
pub trait Retainable: buffer::Seal + AsRef<[u8]> + Sized {
    fn is_empty(&self) -> bool {
        self.as_ref().is_empty()
    }

    /// Promotes borrowed bytes by copying and owned bytes by transferring ownership.
    #[must_use]
    fn into_retained(self) -> Bytes<Retained>;
}

impl<'a> Bytes<Borrowed<'a>> {
    #[must_use]
    pub fn get(self, range: ops::Range<usize>) -> Option<Self> {
        if !range.is_within(self.storage.slice.len()) {
            return None;
        }
        Some(Self {
            storage: Borrowed {
                slice: &self.storage.slice[range],
            },
        })
    }
}

impl Bytes<Retained> {
    #[must_use]
    pub fn copy_from_slice(slice: &[u8]) -> Self {
        Self::from(storage::Shared::copy_from_slice(slice))
    }

    pub fn resident_bytes(&self) -> usize {
        match &self.storage.repr {
            RetainedRepr::Frozen { frozen, .. } => frozen.capacity(),
            RetainedRepr::Shared(shared) => shared.resident_bytes(),
        }
    }

    #[must_use]
    pub fn into_shared(self) -> storage::Shared {
        match self.storage.repr {
            RetainedRepr::Frozen { frozen, start, len } => storage::Shared::copy_from_slice(
                &frozen.as_slice()[start as usize..(start + len) as usize],
            ),
            RetainedRepr::Shared(shared) => shared,
        }
    }

    #[must_use]
    pub fn get(mut self, range: ops::Range<usize>) -> Option<Self> {
        self.storage.try_slice_in_place(range).then_some(self)
    }

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

    fn consume_valid(&mut self, amount: usize) {
        debug_assert!(amount <= self.len());
        if amount == self.storage.len() {
            self.storage.repr = RetainedRepr::Shared(storage::Shared::new());
            return;
        }
        match &mut self.storage.repr {
            RetainedRepr::Frozen { start, len, .. } => {
                *start += amount as u32;
                *len -= amount as u32;
            }
            RetainedRepr::Shared(shared) => shared.consume_valid(amount),
        }
    }
}

impl Bytes<Pooled<'_>> {
    pub fn resident_bytes(&self) -> usize {
        self.storage.frozen.capacity()
    }

    #[must_use]
    pub fn get(mut self, range: ops::Range<usize>) -> Option<Self> {
        self.storage.try_slice_in_place(range).then_some(self)
    }

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

    #[must_use]
    pub fn into_shared(self) -> storage::Shared {
        if self.storage.len == 0 {
            return storage::Shared::new();
        }
        let span = storage::raw::Span::copy_from_bounded_slice(self.as_slice(), self.storage.len);
        storage::Shared::from_span(span)
    }

    fn consume_valid(&mut self, amount: usize) {
        debug_assert!(amount <= self.len());
        self.storage.start += amount as u32;
        self.storage.len -= amount as u32;
    }
}

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

impl Retained {
    fn len(&self) -> usize {
        match &self.repr {
            RetainedRepr::Frozen { len, .. } => *len as usize,
            RetainedRepr::Shared(shared) => shared.len(),
        }
    }

    fn try_slice_in_place(&mut self, range: ops::Range<usize>) -> bool {
        match &mut self.repr {
            RetainedRepr::Frozen { start, len, .. } => {
                if !range.is_within(*len as usize) {
                    return false;
                }
                if range.is_empty() {
                    self.repr = RetainedRepr::Shared(storage::Shared::new());
                    return true;
                }
                *start += range.start as u32;
                *len = range.len() as u32;
                true
            }
            RetainedRepr::Shared(shared) => shared.try_slice_in_place(range),
        }
    }
}

impl Pooled<'_> {
    fn len(&self) -> usize {
        self.len as usize
    }

    fn try_slice_in_place(&mut self, range: ops::Range<usize>) -> bool {
        if !range.is_within(self.len()) {
            return false;
        }
        self.start += range.start as u32;
        self.len = range.len() as u32;
        true
    }
}

impl<S: Storage> Bytes<S> {
    pub fn as_slice(&self) -> &[u8] {
        self.storage.as_slice()
    }

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

    pub fn is_empty(&self) -> bool {
        self.as_slice().is_empty()
    }
}

impl<S> Bytes<S>
where
    Self: Retainable,
{
    #[must_use]
    pub fn into_retained(self) -> Bytes<Retained> {
        <Self as Retainable>::into_retained(self)
    }
}

impl buffer::Seal for Borrowed<'_> {}

impl Storage for Borrowed<'_> {
    fn as_slice(&self) -> &[u8] {
        self.slice
    }
}

impl buffer::Seal for Retained {}

impl Storage for Retained {
    fn as_slice(&self) -> &[u8] {
        match &self.repr {
            RetainedRepr::Frozen { frozen, start, len } => {
                &frozen.as_slice()[*start as usize..(*start + *len) as usize]
            }
            RetainedRepr::Shared(shared) => shared.as_slice(),
        }
    }
}

impl buffer::Seal for Pooled<'_> {}

impl Storage for Pooled<'_> {
    fn as_slice(&self) -> &[u8] {
        &self.frozen.as_slice()[self.start as usize..(self.start + self.len) as usize]
    }
}

impl buffer::Seal for Bytes<Borrowed<'_>> {}

impl Retainable for Bytes<Borrowed<'_>> {
    fn into_retained(self) -> Bytes<Retained> {
        Bytes {
            storage: Retained {
                repr: RetainedRepr::Shared(storage::Shared::copy_from_slice(self.storage.slice)),
            },
        }
    }
}

impl buffer::Seal for Bytes<Retained> {}

impl Retainable for Bytes<Retained> {
    fn is_empty(&self) -> bool {
        match &self.storage.repr {
            RetainedRepr::Frozen { frozen, len, .. } => frozen.is_empty() || *len == 0,
            RetainedRepr::Shared(shared) => shared.is_empty(),
        }
    }

    fn into_retained(self) -> Bytes<Retained> {
        self
    }
}

impl buffer::Seal for Bytes<Pooled<'_>> {}

impl Retainable for Bytes<Pooled<'_>> {
    fn is_empty(&self) -> bool {
        self.storage.frozen.is_empty() || self.storage.len == 0
    }

    fn into_retained(self) -> Bytes<Retained> {
        Bytes {
            storage: Retained {
                repr: RetainedRepr::Frozen {
                    frozen: self.storage.frozen.into_owned(),
                    start: self.storage.start,
                    len: self.storage.len,
                },
            },
        }
    }
}

impl buffer::PrefixConsumer for Bytes<Pooled<'_>> {
    fn consume_validated_prefix(&mut self, proof: buffer::PrefixProof) {
        self.consume_valid(proof.amount());
    }
}

impl Retainable for storage::Shared {
    fn into_retained(self) -> Bytes<Retained> {
        Bytes {
            storage: Retained {
                repr: RetainedRepr::Shared(self),
            },
        }
    }
}

impl<S: Clone> Clone for Bytes<S> {
    fn clone(&self) -> Self {
        Self {
            storage: self.storage.clone(),
        }
    }
}

impl<S: Copy> Copy for Bytes<S> {}

impl<S: Storage> AsRef<[u8]> for Bytes<S> {
    fn as_ref(&self) -> &[u8] {
        self.as_slice()
    }
}

impl<S: Storage> buffer::PrefixLength for Bytes<S> {
    fn prefix_len(&self) -> usize {
        self.len()
    }
}

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

impl<S: Storage> Eq for Bytes<S> {}

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

impl<S: Storage> fmt::Debug for Bytes<S> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("Bytes").field(&self.as_slice()).finish()
    }
}

impl<'a> From<&'a [u8]> for Bytes<Borrowed<'a>> {
    fn from(value: &'a [u8]) -> Self {
        Self {
            storage: Borrowed { slice: value },
        }
    }
}

impl<'a, const N: usize> From<&'a [u8; N]> for Bytes<Borrowed<'a>> {
    fn from(value: &'a [u8; N]) -> Self {
        Self::from(value.as_slice())
    }
}

impl From<storage::Shared> for Bytes<Retained> {
    fn from(value: storage::Shared) -> Self {
        Self {
            storage: Retained {
                repr: RetainedRepr::Shared(value),
            },
        }
    }
}

impl From<buffer::Frozen> for Bytes<Retained> {
    fn from(value: buffer::Frozen) -> Self {
        let len = value.len() as u32;
        Self {
            storage: Retained {
                repr: RetainedRepr::Frozen {
                    frozen: value,
                    start: 0,
                    len,
                },
            },
        }
    }
}

impl<'pool> From<buffer::Frozen<pool::Borrowed<'pool>>> for Bytes<Pooled<'pool>> {
    fn from(value: buffer::Frozen<pool::Borrowed<'pool>>) -> Self {
        let len = value.len() as u32;
        Self {
            storage: Pooled {
                frozen: value,
                start: 0,
                len,
            },
        }
    }
}