sark-core 0.12.0

the L7 layer for dope
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
use std::convert::Infallible;
use std::fmt;
use std::ops::Range;

use o3::buffer::{ByteSink, PoolLayoutError, Pooled, SharedPool, SharedPoolLayout};

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Field<'a> {
    pub name: &'a [u8],
    pub value: &'a [u8],
}

impl<'a> Field<'a> {
    pub const fn new(name: &'a [u8], value: &'a [u8]) -> Self {
        Self { name, value }
    }
}

impl<'field> From<&Field<'field>> for Field<'field> {
    fn from(field: &Field<'field>) -> Self {
        *field
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct OwnedField {
    pub name: Vec<u8>,
    pub value: Vec<u8>,
}

impl OwnedField {
    pub fn new(name: &[u8], value: &[u8]) -> Self {
        Self {
            name: name.to_vec(),
            value: value.to_vec(),
        }
    }

    pub fn as_ref(&self) -> Field<'_> {
        Field::new(&self.name, &self.value)
    }
}

impl From<Field<'_>> for OwnedField {
    fn from(field: Field<'_>) -> Self {
        Self::new(field.name, field.value)
    }
}

impl<'a> From<&'a OwnedField> for Field<'a> {
    fn from(field: &'a OwnedField) -> Self {
        field.as_ref()
    }
}

pub trait FieldStorage {
    type Iter<'a>: Iterator<Item = Field<'a>>
    where
        Self: 'a;

    fn fields(&self) -> Self::Iter<'_>;
}

#[derive(Clone)]
pub struct FieldBlock<S> {
    storage: S,
}

impl<S> FieldBlock<S> {
    pub const fn from_storage(storage: S) -> Self {
        Self { storage }
    }

    pub fn into_storage(self) -> S {
        self.storage
    }

    pub fn storage(&self) -> &S {
        &self.storage
    }
}

impl<S: FieldStorage> FieldBlock<S> {
    pub fn iter(&self) -> S::Iter<'_> {
        self.storage.fields()
    }

    pub fn get(&self, name: &[u8]) -> Option<&[u8]> {
        self.iter()
            .find(|field| field.name == name)
            .map(|field| field.value)
    }
}

impl<S: FieldStorage> fmt::Debug for FieldBlock<S> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.debug_list().entries(self.iter()).finish()
    }
}

impl<S, T> PartialEq<FieldBlock<T>> for FieldBlock<S>
where
    S: FieldStorage,
    T: FieldStorage,
{
    fn eq(&self, other: &FieldBlock<T>) -> bool {
        self.iter().eq(other.iter())
    }
}

impl<S: FieldStorage> Eq for FieldBlock<S> {}

impl<'a, S: FieldStorage> IntoIterator for &'a FieldBlock<S> {
    type Item = Field<'a>;
    type IntoIter = S::Iter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

pub struct OwnedFieldIter<'a> {
    fields: std::slice::Iter<'a, OwnedField>,
}

impl<'a> Iterator for OwnedFieldIter<'a> {
    type Item = Field<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        self.fields.next().map(OwnedField::as_ref)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.fields.size_hint()
    }
}

impl ExactSizeIterator for OwnedFieldIter<'_> {}

impl FieldStorage for Vec<OwnedField> {
    type Iter<'a> = OwnedFieldIter<'a>;

    fn fields(&self) -> Self::Iter<'_> {
        OwnedFieldIter {
            fields: self.iter(),
        }
    }
}

pub type OwnedFieldBlock = FieldBlock<Vec<OwnedField>>;

impl FieldBlock<Vec<OwnedField>> {
    pub const fn new() -> Self {
        Self::from_storage(Vec::new())
    }

    pub fn push(&mut self, name: &[u8], value: &[u8]) {
        self.storage.push(OwnedField::new(name, value));
    }

    pub fn as_slice(&self) -> &[OwnedField] {
        &self.storage
    }
}

impl Default for FieldBlock<Vec<OwnedField>> {
    fn default() -> Self {
        Self::new()
    }
}

#[derive(Clone)]
pub struct PackedFields<S> {
    first: S,
    second: Option<S>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum PackedFieldError<E> {
    Write(E),
    ComponentTooLarge { len: usize },
    ValueLengthMismatch { expected: usize, actual: usize },
}

fn write_packed_prefix<W: ByteSink>(
    writer: &mut W,
    name: &[u8],
    value_len: usize,
) -> Result<(), PackedFieldError<W::Error>> {
    let name_len = packed_len(name.len())?;
    let value_len = packed_len(value_len)?;
    writer
        .write_slices([&name_len, &value_len, name])
        .map_err(PackedFieldError::Write)
}

fn packed_len<E>(len: usize) -> Result<[u8; 4], PackedFieldError<E>> {
    match u32::try_from(len) {
        Ok(len) => Ok(len.to_ne_bytes()),
        Err(_) => Err(PackedFieldError::ComponentTooLarge { len }),
    }
}

fn write_packed_field<W: ByteSink>(
    writer: &mut W,
    field: Field<'_>,
) -> Result<(), PackedFieldError<W::Error>> {
    let name_len = packed_len(field.name.len())?;
    let value_len = packed_len(field.value.len())?;
    writer
        .write_slices([&name_len, &value_len, field.name, field.value])
        .map_err(PackedFieldError::Write)
}

fn packed_capacity(fields: &[Field<'_>]) -> Option<usize> {
    fields.iter().try_fold(0usize, |size, field| {
        u32::try_from(field.name.len()).ok()?;
        u32::try_from(field.value.len()).ok()?;
        field
            .name
            .len()
            .checked_add(field.value.len())?
            .checked_add(8)?
            .checked_add(size)
    })
}

impl<S> PackedFields<S> {
    pub const fn new(first: S) -> Self {
        Self {
            first,
            second: None,
        }
    }
}

impl<S: AsRef<[u8]>> FieldStorage for PackedFields<S> {
    type Iter<'a>
        = PackedFieldIter<'a>
    where
        S: 'a;

    fn fields(&self) -> Self::Iter<'_> {
        PackedFieldIter {
            current: self.first.as_ref(),
            second: self.second.as_ref().map(AsRef::as_ref),
        }
    }
}

pub type PooledFieldBlock = FieldBlock<PackedFields<Pooled>>;
pub type VecFieldBlock = FieldBlock<PackedFields<Vec<u8>>>;

impl FieldBlock<PackedFields<Pooled>> {
    pub fn from_pooled(pooled: Pooled) -> Self {
        Self::from_storage(PackedFields::new(pooled))
    }

    pub fn from_fields(fields: &[Field<'_>]) -> Result<Self, PoolLayoutError> {
        let capacity = packed_capacity(fields).ok_or(PoolLayoutError::CapacityOverflow)?;
        let layout = SharedPoolLayout::new(1, capacity.max(1))?;
        let pool = SharedPool::from_layout(layout);
        let mut lease = pool.try_acquire().ok_or(PoolLayoutError::SlotOverflow)?;
        let mut writer = lease.spare_writer();
        for field in fields {
            write_packed_field(&mut writer, *field)
                .map_err(|_| PoolLayoutError::CapacityOverflow)?;
        }
        drop(writer);
        Ok(Self::from_pooled(lease.freeze()))
    }

    pub fn from_headers(fields: &[Field<'_>]) -> Result<Self, PoolLayoutError> {
        Self::from_fields(fields)
    }

    pub fn append(&mut self, other: Self) -> Result<(), Self> {
        if self.storage.second.is_some() || other.storage.second.is_some() {
            return Err(other);
        }
        self.storage.second = Some(other.storage.first);
        Ok(())
    }

    pub fn to_owned(&self) -> Vec<OwnedField> {
        self.iter().map(OwnedField::from).collect()
    }
}

impl FieldBlock<PackedFields<Vec<u8>>> {
    pub const fn new() -> Self {
        Self::from_storage(PackedFields::new(Vec::new()))
    }

    pub fn with_capacity(capacity: usize) -> Self {
        Self::from_storage(PackedFields::new(Vec::with_capacity(capacity)))
    }

    pub fn push(&mut self, name: &[u8], value: &[u8]) -> Result<(), PackedFieldError<Infallible>> {
        write_packed_field(&mut self.storage.first, Field::new(name, value))
    }

    pub fn push_encoded(
        &mut self,
        name: &[u8],
        value_len: usize,
        encode: impl FnOnce(&mut FieldValueWriter<'_>),
    ) -> Result<(), PackedFieldError<Infallible>> {
        let field_start = self.storage.first.len();
        write_packed_prefix(&mut self.storage.first, name, value_len)?;
        let start = self.storage.first.len();
        encode(&mut FieldValueWriter {
            bytes: &mut self.storage.first,
        });
        let actual = self.storage.first.len() - start;
        if actual != value_len {
            self.storage.first.truncate(field_start);
            return Err(PackedFieldError::ValueLengthMismatch {
                expected: value_len,
                actual,
            });
        }
        Ok(())
    }

    pub fn try_push_parts<E>(
        &mut self,
        encode_name: impl FnOnce(&mut FieldValueWriter<'_>) -> Result<(), E>,
        encode_value: impl FnOnce(&mut FieldValueWriter<'_>, usize) -> Result<(), E>,
    ) -> Result<(usize, usize), PackedFieldError<E>> {
        let field_start = self.storage.first.len();
        self.storage.first.extend_from_slice(&[0; 8]);
        let name_start = self.storage.first.len();
        if let Err(error) = encode_name(&mut FieldValueWriter {
            bytes: &mut self.storage.first,
        }) {
            self.storage.first.truncate(field_start);
            return Err(PackedFieldError::Write(error));
        }
        let value_start = self.storage.first.len();
        let name_len = value_start - name_start;
        if let Err(error) = encode_value(
            &mut FieldValueWriter {
                bytes: &mut self.storage.first,
            },
            name_len,
        ) {
            self.storage.first.truncate(field_start);
            return Err(PackedFieldError::Write(error));
        }
        let end = self.storage.first.len();
        let value_len = end - value_start;
        let name_len_bytes = match packed_len(name_len) {
            Ok(len) => len,
            Err(error) => {
                self.storage.first.truncate(field_start);
                return Err(error);
            }
        };
        let value_len_bytes = match packed_len(value_len) {
            Ok(len) => len,
            Err(error) => {
                self.storage.first.truncate(field_start);
                return Err(error);
            }
        };
        self.storage.first[field_start..field_start + 4].copy_from_slice(&name_len_bytes);
        self.storage.first[field_start + 4..name_start].copy_from_slice(&value_len_bytes);
        Ok((name_len, value_len))
    }

    pub fn as_bytes(&self) -> &[u8] {
        &self.storage.first
    }

    pub fn iter_with_value_ranges(&self) -> PackedFieldRangeIter<'_> {
        PackedFieldRangeIter {
            current: &self.storage.first,
            offset: 0,
        }
    }
}

impl Default for FieldBlock<PackedFields<Vec<u8>>> {
    fn default() -> Self {
        Self::new()
    }
}

pub struct FieldValueWriter<'a> {
    bytes: &'a mut Vec<u8>,
}

impl FieldValueWriter<'_> {
    pub fn push(&mut self, byte: u8) {
        self.bytes.push(byte);
    }

    pub fn extend_from_slice(&mut self, bytes: &[u8]) {
        self.bytes.extend_from_slice(bytes);
    }
}

pub struct PackedFieldIter<'a> {
    current: &'a [u8],
    second: Option<&'a [u8]>,
}

impl<'a> Iterator for PackedFieldIter<'a> {
    type Item = Field<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.current.is_empty() {
            self.current = self.second.take()?;
        }
        let Some((field, _, end)) = parse_packed_field(self.current) else {
            self.current = &[];
            self.second = None;
            return None;
        };
        self.current = &self.current[end..];
        Some(field)
    }
}

pub struct PackedFieldRangeIter<'a> {
    current: &'a [u8],
    offset: usize,
}

impl<'a> Iterator for PackedFieldRangeIter<'a> {
    type Item = (Field<'a>, Range<usize>);

    fn next(&mut self) -> Option<Self::Item> {
        let Some((field, value_start, end)) = parse_packed_field(self.current) else {
            self.current = &[];
            return None;
        };
        let value_range = self.offset + value_start..self.offset + end;
        self.current = &self.current[end..];
        self.offset += end;
        Some((field, value_range))
    }
}

fn parse_packed_field(bytes: &[u8]) -> Option<(Field<'_>, usize, usize)> {
    if bytes.len() < 8 {
        return None;
    }
    let name_len = u32::from_ne_bytes(bytes[..4].try_into().ok()?) as usize;
    let value_len = u32::from_ne_bytes(bytes[4..8].try_into().ok()?) as usize;
    let value_start = 8usize.checked_add(name_len)?;
    let end = value_start.checked_add(value_len)?;
    if end > bytes.len() {
        return None;
    }
    Some((
        Field::new(&bytes[8..value_start], &bytes[value_start..end]),
        value_start,
        end,
    ))
}