barrique 1.0.0

Portable binary serialiation format
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
use crate::decode::{get, Decode, DecodeBearer, DecodeError};
use crate::encode::{Encode, EncodeBearer, EncodeError};

use core::marker::PhantomData;
use core::mem::MaybeUninit;

mod alloc;
mod seq;
mod tuple;

impl<T> Encode for &T
where
    T: Encode,
{
    fn encode(bearer: &mut impl EncodeBearer, src: &Self) -> Result<(), EncodeError> {
        T::encode(bearer, src)
    }

    fn size_of(&self) -> usize {
        (*self).size_of()
    }
}

macro_rules! impl_int {
    ($type:ty) => {
        impl Encode for $type {
            #[inline]
            fn encode(bearer: &mut impl EncodeBearer, src: &Self) -> Result<(), EncodeError> {
                bearer.write(&src.to_le_bytes()).map_err(|e| e.into())
            }

            #[inline]
            fn size_of(&self) -> usize {
                size_of::<$type>()
            }
        }

        unsafe impl Decode for $type {
            fn decode(
                bearer: &mut impl DecodeBearer,
                dst: &mut MaybeUninit<Self>,
            ) -> Result<(), DecodeError> {
                let bytes = unsafe {
                    bearer
                        .read(size_of::<$type>())?
                        .try_into()
                        // Safety: `DecodeBearer::read()` guarantees to return slice with
                        // exact length as requested
                        .unwrap_unchecked()
                };
                dst.write(<$type>::from_le_bytes(bytes));
                Ok(())
            }
        }
    };
    ($type:ty as $as:ty) => {
        impl Encode for $type {
            #[inline]
            fn encode(bearer: &mut impl EncodeBearer, src: &Self) -> Result<(), EncodeError> {
                bearer
                    .write(&(*src as $as).to_le_bytes())
                    .map_err(|e| e.into())
            }

            #[inline]
            fn size_of(&self) -> usize {
                size_of::<$as>()
            }
        }

        unsafe impl Decode for $type {
            fn decode(
                bearer: &mut impl DecodeBearer,
                dst: &mut MaybeUninit<Self>,
            ) -> Result<(), DecodeError> {
                let bytes = unsafe {
                    bearer
                        .read(size_of::<$as>())?
                        .try_into()
                        // Safety: `DecodeBearer::read()` guarantees to return slice with
                        // exact length as requested
                        .unwrap_unchecked()
                };
                dst.write(<$as>::from_le_bytes(bytes) as $type);
                Ok(())
            }
        }
    };
}

impl_int!(u8);
impl_int!(u16);
impl_int!(u32);
impl_int!(u64);
impl_int!(u128);
impl_int!(i8);
impl_int!(i16);
impl_int!(i32);
impl_int!(i64);
impl_int!(i128);
impl_int!(f32);
impl_int!(f64);
impl_int!(usize as u64);
impl_int!(isize as i64);

impl Encode for bool {
    #[inline]
    fn encode(bearer: &mut impl EncodeBearer, src: &Self) -> Result<(), EncodeError> {
        u8::encode(bearer, &(*src as u8))
    }

    #[inline]
    fn size_of(&self) -> usize {
        size_of::<u8>()
    }
}

unsafe impl Decode for bool {
    #[inline]
    fn decode(
        bearer: &mut impl DecodeBearer,
        dst: &mut MaybeUninit<Self>,
    ) -> Result<(), DecodeError> {
        let bytes = bearer.read(size_of::<u8>())?;
        match unsafe {
            // Safety: `StreamDecoder::read` guarantees to return
            // slice with exact length requested
            bytes.get_unchecked(0)
        } {
            0 => dst.write(false),
            1 => dst.write(true),
            _ => return Err(DecodeError::InvalidPattern),
        };

        Ok(())
    }
}

impl Encode for char {
    fn encode(bearer: &mut impl EncodeBearer, src: &Self) -> Result<(), EncodeError> {
        let mut bytes = [0u8; 4];
        let str = src.encode_utf8(&mut bytes);

        bearer.write(str.as_bytes()).map_err(|e| e.into())
    }

    #[inline]
    fn size_of(&self) -> usize {
        self.len_utf8()
    }
}

/// Decode bytes into a UTF-8 character.
///
/// # Safety
///
/// - `leading` must be valid first UTF-8 sequence byte for given length
///   of continuous bytes.
///
/// Violating this requirement will not trigger language UB, instead,
/// core library level UB is produced
unsafe fn decode_char_from_bytes(leading: u8, remaining: &[u8]) -> Option<char> {
    let get = |i: usize| {
        if remaining[i] & 0xC0 == 0x80 {
            return Some((remaining[i] & 0x3F) as u32);
        }
        None
    };

    let codepoint = match remaining.len() {
        1 => (leading as u32 & 0x1F) << 6 | get(0)?,
        2 => (leading as u32 & 0x0F) << 12 | get(0)? << 6 | get(1)?,
        3 => (leading as u32 & 0x07) << 18 | get(0)? << 12 | get(1)? << 6 | get(2)?,
        _ => unreachable!(),
    };

    Some(unsafe {
        // Safety: UTF-8 validated in the `get` closure, `leading` checked to
        // guaranteed by the caller to be valid
        char::from_u32_unchecked(codepoint)
    })
}

unsafe impl Decode for char {
    fn decode(
        bearer: &mut impl DecodeBearer,
        dst: &mut MaybeUninit<Self>,
    ) -> Result<(), DecodeError> {
        let leading = unsafe {
            // Safety: `DecodeBearer::read()` guarantees to return slice with
            // exact length as requested
            *bearer.read(1)?.get_unchecked(0)
        };

        let len = match leading {
            0x0..=0x7F => {
                let char = unsafe {
                    // Safety: match expression checked byte to be within ASCII range
                    char::from_u32_unchecked(leading as u32)
                };
                dst.write(char);
                return Ok(());
            }
            0xC0..=0xDF => 1,
            0xE0..=0xEF => 2,
            0xF0..=0xF7 => 3,
            _ => return Err(DecodeError::InvalidPattern),
        };

        let char = unsafe {
            // Safety: `leading` has valid bit pattern for length of remaining bytes
            decode_char_from_bytes(leading, bearer.read(len)?).ok_or(DecodeError::InvalidPattern)?
        };
        dst.write(char);
        Ok(())
    }
}

impl<T> Encode for Option<T>
where
    T: Encode,
{
    #[inline]
    fn encode(bearer: &mut impl EncodeBearer, src: &Self) -> Result<(), EncodeError> {
        u8::encode(bearer, &(src.is_some() as u8))?;
        match src {
            Some(v) => T::encode(bearer, v),
            None => Ok(()),
        }
    }

    #[inline]
    fn size_of(&self) -> usize {
        1 + self.as_ref().map_or(0, |v| v.size_of())
    }
}

unsafe impl<T> Decode for Option<T>
where
    T: Decode,
{
    #[inline]
    fn decode(
        bearer: &mut impl DecodeBearer,
        dst: &mut MaybeUninit<Self>,
    ) -> Result<(), DecodeError> {
        match unsafe {
            // Safety: `DecodeBearer::read()` guarantees to return slice with
            // exact length as requested
            bearer.read(1)?.get_unchecked(0)
        } {
            0 => dst.write(None),
            1 => dst.write(Some(get(bearer)?)),
            _ => return Err(DecodeError::InvalidPattern),
        };
        Ok(())
    }
}

impl<T, E> Encode for Result<T, E>
where
    T: Encode,
    E: Encode,
{
    #[inline]
    fn encode(bearer: &mut impl EncodeBearer, src: &Self) -> Result<(), EncodeError> {
        u8::encode(bearer, &(src.is_ok() as u8))?;
        match src {
            Ok(v) => T::encode(bearer, v),
            Err(e) => E::encode(bearer, e),
        }
    }

    #[inline]
    fn size_of(&self) -> usize {
        match self {
            Ok(v) => v.size_of(),
            Err(e) => e.size_of(),
        }
    }
}

unsafe impl<T, E> Decode for Result<T, E>
where
    T: Decode,
    E: Decode,
{
    #[inline]
    fn decode(
        bearer: &mut impl DecodeBearer,
        dst: &mut MaybeUninit<Self>,
    ) -> Result<(), DecodeError> {
        match unsafe {
            // Safety: `DecodeBearer::read()` guarantees to return slice with
            // exact length as requested
            bearer.read(1)?.get_unchecked(0)
        } {
            0 => dst.write(Err(get(bearer)?)),
            1 => dst.write(Ok(get(bearer)?)),
            _ => return Err(DecodeError::InvalidPattern),
        };
        Ok(())
    }
}

impl<T> Encode for PhantomData<T> {
    #[inline]
    fn encode(_: &mut impl EncodeBearer, _: &Self) -> Result<(), EncodeError> {
        Ok(())
    }

    #[inline]
    fn size_of(&self) -> usize {
        0
    }
}

unsafe impl<T> Decode for PhantomData<T> {
    #[inline]
    fn decode(_: &mut impl DecodeBearer, _: &mut MaybeUninit<Self>) -> Result<(), DecodeError> {
        Ok(())
    }
}

impl Encode for () {
    #[inline]
    fn encode(_: &mut impl EncodeBearer, _: &Self) -> Result<(), EncodeError> {
        Ok(())
    }

    #[inline]
    fn size_of(&self) -> usize {
        0
    }
}

unsafe impl Decode for () {
    #[inline]
    fn decode(_: &mut impl DecodeBearer, _: &mut MaybeUninit<Self>) -> Result<(), DecodeError> {
        Ok(())
    }
}

impl<T> Encode for [T]
where
    T: Encode,
{
    #[inline]
    fn encode(bearer: &mut impl EncodeBearer, src: &Self) -> Result<(), EncodeError> {
        u32::encode(bearer, &(src.len() as u32))?;
        for item in src {
            T::encode(bearer, item)?;
        }
        Ok(())
    }

    #[inline]
    fn size_of(&self) -> usize {
        self.iter().map(|v| v.size_of()).sum::<usize>() + size_of::<u32>()
    }
}

impl<T, const N: usize> Encode for [T; N]
where
    T: Encode,
{
    #[inline]
    fn encode(bearer: &mut impl EncodeBearer, src: &Self) -> Result<(), EncodeError> {
        for item in src {
            T::encode(bearer, item)?;
        }
        Ok(())
    }

    #[inline]
    fn size_of(&self) -> usize {
        self.iter().map(|v| v.size_of()).sum()
    }
}

/// Panic-safety guard for [`Decode`] implementation of unsized array types.
///
/// # Drop safety
///
/// Owner must call `mem::forget` when underlying array is fully initialized to
/// avoid double dropping
pub(super) struct ArrayDecodingGuard<T> {
    ptr: *mut MaybeUninit<T>,
    len: usize,
}

impl<T> ArrayDecodingGuard<T> {
    /// Create a new [`ArrayDecodingGuard`].
    ///
    /// # Safety
    ///
    /// - `ptr` must be properly aligned and point to valid memory.
    ///
    /// - owner must call `mem::forget` when underlying array is fully initialized to
    ///   avoid double dropping
    pub unsafe fn new(ptr: *mut MaybeUninit<T>) -> Self {
        Self { ptr, len: 0 }
    }

    /// Get a mutable reference to last slot of this array.
    pub fn slot(&mut self) -> &mut MaybeUninit<T> {
        unsafe {
            // Safety:
            // - `len` can be incremented only via `::add()` method, which requires caller
            //   to guarantee increment not to overflow underlying array.
            // - constructor method requires `ptr` assigned to be properly aligned
            &mut *self.ptr.add(self.len)
        }
    }

    /// Increment inner length by one.
    ///
    /// # Safety
    ///
    /// - incrementing the length must not overflow underlying array or `isize::MAX`
    pub unsafe fn add(&mut self) {
        self.len = unsafe {
            // Safety: caller guarantees inner length to not overflow underlying array
            self.len.unchecked_add(1)
        }
    }
}

impl<T> Drop for ArrayDecodingGuard<T> {
    #[cold]
    fn drop(&mut self) {
        unsafe {
            // Safety: owner guarantees to call `mem::forget`
            core::ptr::drop_in_place(core::ptr::slice_from_raw_parts_mut(self.ptr, self.len))
        }
    }
}

unsafe impl<T, const N: usize> Decode for [T; N]
where
    T: Decode,
{
    fn decode(
        bearer: &mut impl DecodeBearer,
        dst: &mut MaybeUninit<Self>,
    ) -> Result<(), DecodeError> {
        let mut guard = unsafe {
            // Safety: passed pointer is valid and pointing to `array` initialized above
            ArrayDecodingGuard::new(dst.as_mut_ptr().cast::<MaybeUninit<T>>())
        };

        for _ in 0..N {
            let slot = guard.slot();
            T::decode(bearer, slot)?;
            unsafe {
                // Safety: write will not overflow since we're
                // iterating strictly up to `N`
                guard.add()
            }
        }

        core::mem::forget(guard);
        Ok(())
    }
}

impl Encode for str {
    #[inline]
    fn encode(bearer: &mut impl EncodeBearer, src: &Self) -> Result<(), EncodeError> {
        <[u8]>::encode(bearer, src.as_bytes())
    }

    #[inline]
    fn size_of(&self) -> usize {
        self.len()
    }
}