wire-codec 1.2.0

Binary frame codec and protocol codec toolkit. Length-prefixed, varint-prefixed, delimiter-based, checksummed, and custom framing strategies. Built-in varint, zigzag, bitfield, packed struct, CRC, and COBS encoding. Allocation-free core, no_std, runtime-agnostic, with optional tokio-util and futures codec adapters.
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
//! Core traits implemented by every encodable value.
//!
//! [`Encode`] writes a value into a [`WriteBuf`]; [`Decode`] reads one out of a
//! [`ReadBuf`]. The traits are deliberately minimal so callers can compose them
//! with any framing strategy in the [`framing`][`crate::framing`] module.
//!
//! # Provided implementations
//!
//! | Type | Wire form |
//! | ---- | --------- |
//! | `u8`, `i8` | one byte |
//! | `bool` | one byte, `0` or `1`; any other byte decodes as [`Error::InvalidEncoding`] |
//! | `[u8; N]` | `N` raw bytes (copied on decode) |
//! | `&'de [u8; N]` | `N` raw bytes (zero-copy decode) |
//! | `[u8]` | raw bytes, encode only (the length must be framed separately) |
//! | [`Be<T>`] / [`Le<T>`] | `T` in big / little endian, for every integer from `u16` to `i128`, and `f32` / `f64` |
//! | [`Varint<T>`] | LEB128 varint for `u16`, `u32`, `u64`, `usize`; zigzag varint for `i32`, `i64` |
//! | `(A, B)` up to `(A, B, C, D, E, F)` | the elements in order |
//!
//! Multi-byte integers have no plain `Encode` impl on purpose: the byte order
//! is part of the wire format, so it must be chosen explicitly with a
//! wrapper.
//!
//! Tuples restore the cursor to where the tuple started if any element
//! fails. They check the total `encoded_size` against the remaining room
//! first, so a short buffer is reported as [`Error::BufferFull`] before
//! anything is written, and before any element gets the chance to report
//! a different error. If a later element fails for another reason (a
//! packed field out of range, say), the bytes of the earlier elements remain
//! behind the restored cursor.

use crate::buf::{ReadBuf, WriteBuf};
use crate::error::{Error, Result};
use crate::varint;

/// A value that can be written to a [`WriteBuf`].
///
/// Implementors must guarantee that [`Encode::encoded_size`] returns the exact
/// number of bytes [`Encode::encode`] writes on success.
///
/// # Example
///
/// ```
/// use wire_codec::{Encode, Result, WriteBuf};
///
/// struct Tagged {
///     tag: u8,
///     value: u32,
/// }
///
/// impl Encode for Tagged {
///     fn encoded_size(&self) -> usize { 5 }
///     fn encode(&self, buf: &mut WriteBuf<'_>) -> Result<()> {
///         buf.write_u8(self.tag)?;
///         buf.write_u32_be(self.value)
///     }
/// }
///
/// let mut out = [0u8; 5];
/// let mut buf = WriteBuf::new(&mut out);
/// Tagged { tag: 0x01, value: 0xCAFEBABE }.encode(&mut buf).unwrap();
/// assert_eq!(&out, &[0x01, 0xCA, 0xFE, 0xBA, 0xBE]);
/// ```
pub trait Encode {
    /// Number of bytes [`Encode::encode`] will write on success.
    #[must_use]
    fn encoded_size(&self) -> usize;

    /// Write `self` into `buf`, advancing its write position.
    ///
    /// # Errors
    ///
    /// Returns an error if `buf` lacks capacity for [`Encode::encoded_size`]
    /// bytes or if a sub-encoder reports an invariant violation.
    fn encode(&self, buf: &mut WriteBuf<'_>) -> Result<()>;
}

/// A value that can be read from a [`ReadBuf`].
///
/// The lifetime parameter `'de` ties the decoded value to the borrowed input,
/// enabling zero-copy decoding of borrowed payloads (`&'de [u8]`, `&'de str`).
///
/// # Example
///
/// ```
/// use wire_codec::{Decode, ReadBuf, Result};
///
/// struct Tagged {
///     tag: u8,
///     value: u32,
/// }
///
/// impl<'de> Decode<'de> for Tagged {
///     fn decode(buf: &mut ReadBuf<'de>) -> Result<Self> {
///         let tag = buf.read_u8()?;
///         let value = buf.read_u32_be()?;
///         Ok(Self { tag, value })
///     }
/// }
///
/// let mut buf = ReadBuf::new(&[0x01, 0xCA, 0xFE, 0xBA, 0xBE]);
/// let tagged = Tagged::decode(&mut buf).unwrap();
/// assert_eq!(tagged.tag, 0x01);
/// assert_eq!(tagged.value, 0xCAFEBABE);
/// ```
pub trait Decode<'de>: Sized {
    /// Read a value of `Self` from `buf`, advancing its read position.
    ///
    /// # Errors
    ///
    /// Returns an error if `buf` is exhausted before the value finishes, or if
    /// the encoded bytes violate the codec's structural rules.
    fn decode(buf: &mut ReadBuf<'de>) -> Result<Self>;
}

impl Encode for u8 {
    #[inline]
    fn encoded_size(&self) -> usize {
        1
    }
    #[inline]
    fn encode(&self, buf: &mut WriteBuf<'_>) -> Result<()> {
        buf.write_u8(*self)
    }
}

impl<'de> Decode<'de> for u8 {
    #[inline]
    fn decode(buf: &mut ReadBuf<'de>) -> Result<Self> {
        buf.read_u8()
    }
}

impl Encode for i8 {
    #[inline]
    fn encoded_size(&self) -> usize {
        1
    }
    #[inline]
    fn encode(&self, buf: &mut WriteBuf<'_>) -> Result<()> {
        buf.write_i8(*self)
    }
}

impl<'de> Decode<'de> for i8 {
    #[inline]
    fn decode(buf: &mut ReadBuf<'de>) -> Result<Self> {
        buf.read_i8()
    }
}

impl Encode for bool {
    #[inline]
    fn encoded_size(&self) -> usize {
        1
    }
    #[inline]
    fn encode(&self, buf: &mut WriteBuf<'_>) -> Result<()> {
        buf.write_u8(u8::from(*self))
    }
}

/// Strict: only `0` and `1` are accepted, so every value has one encoding.
impl<'de> Decode<'de> for bool {
    #[inline]
    fn decode(buf: &mut ReadBuf<'de>) -> Result<Self> {
        let value = match buf.peek_u8()? {
            0 => false,
            1 => true,
            _ => return Err(Error::InvalidEncoding),
        };
        buf.consume(1);
        Ok(value)
    }
}

impl Encode for [u8] {
    #[inline]
    fn encoded_size(&self) -> usize {
        self.len()
    }
    #[inline]
    fn encode(&self, buf: &mut WriteBuf<'_>) -> Result<()> {
        buf.write_bytes(self)
    }
}

impl<const N: usize> Encode for [u8; N] {
    #[inline]
    fn encoded_size(&self) -> usize {
        N
    }
    #[inline]
    fn encode(&self, buf: &mut WriteBuf<'_>) -> Result<()> {
        buf.write_bytes(self)
    }
}

impl<'de, const N: usize> Decode<'de> for [u8; N] {
    #[inline]
    fn decode(buf: &mut ReadBuf<'de>) -> Result<Self> {
        buf.read_array()
    }
}

impl<'de, const N: usize> Decode<'de> for &'de [u8; N] {
    #[inline]
    fn decode(buf: &mut ReadBuf<'de>) -> Result<Self> {
        buf.read_array_ref()
    }
}

/// Big-endian wrapper: encodes and decodes `T` most significant byte first.
///
/// # Example
///
/// ```
/// use wire_codec::{Be, Decode, Encode, ReadBuf, WriteBuf};
///
/// let mut out = [0u8; 4];
/// let mut buf = WriteBuf::new(&mut out);
/// Be(0xCAFE_u16).encode(&mut buf).unwrap();
/// Be(-2_i16).encode(&mut buf).unwrap();
/// assert_eq!(out, [0xCA, 0xFE, 0xFF, 0xFE]);
///
/// let mut r = ReadBuf::new(&out);
/// assert_eq!(Be::<u16>::decode(&mut r).unwrap().0, 0xCAFE);
/// assert_eq!(Be::<i16>::decode(&mut r).unwrap(), Be(-2));
/// ```
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Be<T>(pub T);

/// Little-endian wrapper: encodes and decodes `T` least significant byte
/// first.
///
/// # Example
///
/// ```
/// use wire_codec::{Decode, Encode, Le, ReadBuf, WriteBuf};
///
/// let mut out = [0u8; 4];
/// let mut buf = WriteBuf::new(&mut out);
/// Le(1.0_f32).encode(&mut buf).unwrap();
/// assert_eq!(out, [0x00, 0x00, 0x80, 0x3F]);
/// assert_eq!(Le::<f32>::decode(&mut ReadBuf::new(&out)).unwrap(), Le(1.0));
/// ```
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Le<T>(pub T);

/// LEB128 varint wrapper. Signed integers use the zigzag mapping, matching
/// protobuf's `sint32` / `sint64`.
///
/// Decoding is lenient about overlong encodings, like
/// [`varint::decode_u64`]. Call the `varint::decode_*_canonical` functions
/// directly when canonical form must be enforced.
///
/// # Example
///
/// ```
/// use wire_codec::{Decode, Encode, ReadBuf, Varint, WriteBuf};
///
/// let mut out = [0u8; 8];
/// let mut buf = WriteBuf::new(&mut out);
/// Varint(300_u32).encode(&mut buf).unwrap();
/// Varint(-1_i64).encode(&mut buf).unwrap();
/// assert_eq!(buf.written(), &[0xAC, 0x02, 0x01]);
/// assert_eq!(Varint(300_u32).encoded_size(), 2);
///
/// let mut r = ReadBuf::new(&out[..3]);
/// assert_eq!(Varint::<u32>::decode(&mut r).unwrap(), Varint(300));
/// assert_eq!(Varint::<i64>::decode(&mut r).unwrap(), Varint(-1));
/// ```
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Varint<T>(pub T);

macro_rules! fixed_width_impls {
    ($($ty:ty: $read_be:ident, $write_be:ident, $read_le:ident, $write_le:ident;)*) => {$(
        impl Encode for Be<$ty> {
            #[inline]
            fn encoded_size(&self) -> usize {
                core::mem::size_of::<$ty>()
            }
            #[inline]
            fn encode(&self, buf: &mut WriteBuf<'_>) -> Result<()> {
                buf.$write_be(self.0)
            }
        }

        impl<'de> Decode<'de> for Be<$ty> {
            #[inline]
            fn decode(buf: &mut ReadBuf<'de>) -> Result<Self> {
                buf.$read_be().map(Be)
            }
        }

        impl Encode for Le<$ty> {
            #[inline]
            fn encoded_size(&self) -> usize {
                core::mem::size_of::<$ty>()
            }
            #[inline]
            fn encode(&self, buf: &mut WriteBuf<'_>) -> Result<()> {
                buf.$write_le(self.0)
            }
        }

        impl<'de> Decode<'de> for Le<$ty> {
            #[inline]
            fn decode(buf: &mut ReadBuf<'de>) -> Result<Self> {
                buf.$read_le().map(Le)
            }
        }
    )*};
}

fixed_width_impls! {
    u16: read_u16_be, write_u16_be, read_u16_le, write_u16_le;
    u32: read_u32_be, write_u32_be, read_u32_le, write_u32_le;
    u64: read_u64_be, write_u64_be, read_u64_le, write_u64_le;
    u128: read_u128_be, write_u128_be, read_u128_le, write_u128_le;
    i16: read_i16_be, write_i16_be, read_i16_le, write_i16_le;
    i32: read_i32_be, write_i32_be, read_i32_le, write_i32_le;
    i64: read_i64_be, write_i64_be, read_i64_le, write_i64_le;
    i128: read_i128_be, write_i128_be, read_i128_le, write_i128_le;
    f32: read_f32_be, write_f32_be, read_f32_le, write_f32_le;
    f64: read_f64_be, write_f64_be, read_f64_le, write_f64_le;
}

macro_rules! varint_impls {
    ($($ty:ty: $len:path, $encode:path, $decode:path;)*) => {$(
        impl Encode for Varint<$ty> {
            #[inline]
            fn encoded_size(&self) -> usize {
                $len(self.0)
            }
            #[inline]
            fn encode(&self, buf: &mut WriteBuf<'_>) -> Result<()> {
                $encode(self.0, buf)
            }
        }

        impl<'de> Decode<'de> for Varint<$ty> {
            #[inline]
            fn decode(buf: &mut ReadBuf<'de>) -> Result<Self> {
                $decode(buf).map(Varint)
            }
        }
    )*};
}

varint_impls! {
    u16: varint::encoded_len_u16, varint::encode_u16, varint::decode_u16;
    u32: varint::encoded_len_u32, varint::encode_u32, varint::decode_u32;
    u64: varint::encoded_len_u64, varint::encode_u64, varint::decode_u64;
    usize: varint::encoded_len_usize, varint::encode_usize, varint::decode_usize;
    i32: varint::encoded_len_i32, varint::encode_i32, varint::decode_i32;
    i64: varint::encoded_len_i64, varint::encode_i64, varint::decode_i64;
}

macro_rules! tuple_impls {
    ($(($($name:ident $idx:tt),+);)*) => {$(
        impl<$($name: Encode),+> Encode for ($($name,)+) {
            #[inline]
            fn encoded_size(&self) -> usize {
                0 $(+ self.$idx.encoded_size())+
            }
            #[inline]
            fn encode(&self, buf: &mut WriteBuf<'_>) -> Result<()> {
                // `encoded_size` is exact by contract: check the room once so
                // a short buffer writes nothing at all.
                if self.encoded_size() > buf.remaining() {
                    return Err(Error::BufferFull);
                }
                let start = buf.position();
                let result = (|| {
                    $(self.$idx.encode(buf)?;)+
                    Ok(())
                })();
                if result.is_err() {
                    buf.rewind(start);
                }
                result
            }
        }

        impl<'de, $($name: Decode<'de>),+> Decode<'de> for ($($name,)+) {
            #[inline]
            fn decode(buf: &mut ReadBuf<'de>) -> Result<Self> {
                let mut scratch = buf.clone();
                let value = ($($name::decode(&mut scratch)?,)+);
                *buf = scratch;
                Ok(value)
            }
        }
    )*};
}

tuple_impls! {
    (A 0, B 1);
    (A 0, B 1, C 2);
    (A 0, B 1, C 2, D 3);
    (A 0, B 1, C 2, D 3, E 4);
    (A 0, B 1, C 2, D 3, E 4, F 5);
}

#[cfg(test)]
mod tests {
    use super::*;

    /// An element that always fails to encode, for atomicity tests.
    struct Rejects;

    impl Encode for Rejects {
        fn encoded_size(&self) -> usize {
            1
        }
        fn encode(&self, _buf: &mut WriteBuf<'_>) -> Result<()> {
            Err(Error::InvalidEncoding)
        }
    }

    fn round_trip<T>(value: T) -> T
    where
        T: Encode + for<'de> Decode<'de> + PartialEq + core::fmt::Debug,
    {
        let mut out = [0u8; 64];
        let mut buf = WriteBuf::new(&mut out);
        value.encode(&mut buf).unwrap();
        let n = buf.position();
        assert_eq!(n, value.encoded_size(), "encoded_size must be exact");
        let mut r = ReadBuf::new(&out[..n]);
        let decoded = T::decode(&mut r).unwrap();
        assert!(r.is_empty());
        decoded
    }

    #[test]
    fn primitive_round_trips() {
        assert_eq!(round_trip(0xABu8), 0xAB);
        assert_eq!(round_trip(-5i8), -5);
        assert!(round_trip(true));
        assert!(!round_trip(false));
        assert_eq!(round_trip([1u8, 2, 3]), [1, 2, 3]);
    }

    #[test]
    fn wrapper_round_trips() {
        assert_eq!(round_trip(Be(u16::MAX)), Be(u16::MAX));
        assert_eq!(round_trip(Le(0x0102_0304_u32)), Le(0x0102_0304));
        assert_eq!(round_trip(Be(u64::MAX - 1)), Be(u64::MAX - 1));
        assert_eq!(round_trip(Le(u128::MAX / 3)), Le(u128::MAX / 3));
        assert_eq!(round_trip(Be(i32::MIN)), Be(i32::MIN));
        assert_eq!(round_trip(Le(i64::MIN)), Le(i64::MIN));
        assert_eq!(round_trip(Be(i128::MIN)), Be(i128::MIN));
        assert_eq!(round_trip(Le(-3i16)), Le(-3));
        assert_eq!(round_trip(Be(2.5f64)), Be(2.5));
        assert_eq!(round_trip(Le(-0.5f64)), Le(-0.5));
        assert_eq!(round_trip(Be(9.0f32)), Be(9.0));
        for v in [0u16, 300, u16::MAX] {
            assert_eq!(round_trip(Varint(v)), Varint(v));
        }
        for v in [0usize, 1 << 20, usize::MAX] {
            assert_eq!(round_trip(Varint(v)), Varint(v));
        }
        assert_eq!(round_trip(Varint(u32::MAX)), Varint(u32::MAX));
        assert_eq!(round_trip(Varint(u64::MAX)), Varint(u64::MAX));
        assert_eq!(round_trip(Varint(i32::MIN)), Varint(i32::MIN));
        assert_eq!(round_trip(Varint(i64::MAX)), Varint(i64::MAX));
    }

    #[test]
    fn byte_order_is_explicit() {
        let mut out = [0u8; 4];
        let mut buf = WriteBuf::new(&mut out);
        Be(0x0102u16).encode(&mut buf).unwrap();
        Le(0x0102u16).encode(&mut buf).unwrap();
        assert_eq!(out, [0x01, 0x02, 0x02, 0x01]);
    }

    #[test]
    fn tuples_encode_in_order() {
        let value = (1u8, Be(0x0203u16), Varint(300u32), true);
        let mut out = [0u8; 16];
        let mut buf = WriteBuf::new(&mut out);
        value.encode(&mut buf).unwrap();
        assert_eq!(buf.written(), &[1, 2, 3, 0xAC, 0x02, 1]);
        assert_eq!(round_trip(value), value);
        assert_eq!(round_trip((1u8, 2u8)), (1, 2));
        assert_eq!(round_trip((1u8, 2u8, 3u8)), (1, 2, 3));
        assert_eq!(round_trip((1u8, 2u8, 3u8, 4u8, 5u8)), (1, 2, 3, 4, 5));
        assert_eq!(
            round_trip((1u8, 2u8, 3u8, 4u8, 5u8, 6u8)),
            (1, 2, 3, 4, 5, 6)
        );
    }

    #[test]
    fn tuple_decode_and_encode_are_atomic() {
        // Second element is short: the cursor must not keep the first byte.
        let mut r = ReadBuf::new(&[7, 0x01]);
        assert_eq!(<(u8, Be<u16>)>::decode(&mut r), Err(Error::UnexpectedEof));
        assert_eq!(r.position(), 0);

        let mut out = [0xEEu8; 2];
        let mut buf = WriteBuf::new(&mut out);
        assert_eq!((1u8, Be(2u16)).encode(&mut buf), Err(Error::BufferFull));
        assert_eq!(buf.position(), 0);
        assert_eq!(out, [0xEE, 0xEE], "a short buffer writes nothing");

        // A later element failing for another reason restores the cursor.
        let mut out = [0u8; 4];
        let mut buf = WriteBuf::new(&mut out);
        assert_eq!((1u8, Rejects).encode(&mut buf), Err(Error::InvalidEncoding));
        assert_eq!(buf.position(), 0);
    }

    #[test]
    fn bool_is_strict() {
        let mut r = ReadBuf::new(&[2]);
        assert_eq!(bool::decode(&mut r), Err(Error::InvalidEncoding));
        assert_eq!(r.position(), 0);
        assert_eq!(
            bool::decode(&mut ReadBuf::new(&[])),
            Err(Error::UnexpectedEof)
        );
    }

    #[test]
    fn byte_slices_and_array_refs() {
        let mut out = [0u8; 8];
        let mut buf = WriteBuf::new(&mut out);
        let raw: &[u8] = b"wire";
        assert_eq!(raw.encoded_size(), 4);
        raw.encode(&mut buf).unwrap();
        assert_eq!(buf.written(), b"wire");

        let data = *b"abcdef";
        let mut r = ReadBuf::new(&data);
        let head = <&[u8; 4]>::decode(&mut r).unwrap();
        assert_eq!(head, b"abcd");
        assert_eq!(<[u8; 4]>::decode(&mut r), Err(Error::UnexpectedEof));
    }

    #[test]
    fn encode_errors_propagate() {
        let mut out = [0u8; 1];
        let mut buf = WriteBuf::new(&mut out);
        assert_eq!(Be(1u32).encode(&mut buf), Err(Error::BufferFull));
        assert_eq!(Le(1u32).encode(&mut buf), Err(Error::BufferFull));
        assert_eq!(Varint(300u32).encode(&mut buf), Err(Error::BufferFull));
        assert_eq!(buf.position(), 0);
        let mut r = ReadBuf::new(&[0x80]);
        assert_eq!(Varint::<u64>::decode(&mut r), Err(Error::UnexpectedEof));
        assert_eq!(Le::<u16>::decode(&mut r), Err(Error::UnexpectedEof));
        assert_eq!(Be::<u16>::decode(&mut r), Err(Error::UnexpectedEof));
    }
}