netidx_core/
pack.rs

1//! Efficient binary serialization with forward compatibility.
2//!
3//! Pack provides a compact binary encoding for netidx's wire protocol. Unlike
4//! `serde` with `bincode` or similar serializers, Pack is designed for protocol
5//! evolution and forward compatibility.
6//!
7//! ## Key Features
8//!
9//! - **Efficient encoding**: LEB128 varints, length-prefixed strings, zero-copy where possible
10//! - **Forward compatible**: Length-wrapped encoding allows old decoders to skip unknown fields
11//! - **Extensible protocols**: Add new fields to structs and enum variants without breaking compatibility
12//! - **Derivable**: Use `#[derive(Pack)]` from the `netidx-derive` crate
13//!
14//! ## Why Pack Instead of Serde?
15//!
16//! While `serde` with `bincode` or other binary formats provides fast
17//! serialization, Pack is specifically designed to be fast and
18//! **extensible**:
19//!
20//! - **Length-wrapped encoding**: By default each encoded value is
21//! prefixed with its length, allowing decoders to skip unknown fields
22//! in structs and variants.
23//! - **Protocol evolution**: New fields can be added to the end of
24//! structs, and to the end of struct variants without breaking old
25//! decoders. New enum cases can be added to the end of enums and are
26//! safe as long as they are never sent to old decoders.
27//!
28//! ## Example
29//!
30//! ```ignore
31//! use netidx_core::pack::Pack;
32//! use bytes::{Bytes, BytesMut};
33//!
34//! #[derive(Pack)]
35//! struct Message {
36//!     id: u32,
37//!     data: String,
38//! }
39//!
40//! let msg = Message { id: 42, data: "hello".into() };
41//! let buf = utils::pack(&msg)?;
42//!
43//! let decoded = Message::decode(&mut buf.freeze())?;
44//! ```
45//!
46//! ## The Pack Trait
47//!
48//! The core trait provides three methods:
49//!
50//! - `encoded_len()`: Calculate the encoded size
51//! - `encode()`: Encode to a buffer
52//! - `decode()`: Decode from a buffer
53//!
54//! Most types should use `#[derive(Pack)]` rather than implementing manually.
55
56use arcstr::ArcStr;
57use arrayvec::{ArrayString, ArrayVec};
58use bytes::{buf, Buf, BufMut, Bytes, BytesMut};
59use chrono::{
60    naive::{NaiveDate, NaiveDateTime},
61    prelude::*,
62};
63use compact_str::CompactString;
64use indexmap::{IndexMap, IndexSet};
65use poolshark::{
66    global::{take_any, GPooled},
67    local::LPooled,
68    Poolable,
69};
70use rust_decimal::Decimal;
71use std::{
72    any::Any,
73    boxed::Box,
74    cell::RefCell,
75    cmp::{self, Eq},
76    collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque},
77    default::Default,
78    error, fmt,
79    hash::{BuildHasher, Hash},
80    mem, net,
81    ops::{Deref, DerefMut},
82    result, str,
83    sync::Arc,
84    time::Duration,
85};
86
87/// Error type for Pack encoding and decoding operations.
88#[derive(Debug, Clone, Copy)]
89pub enum PackError {
90    UnknownTag,
91    TooBig,
92    InvalidFormat,
93    BufferShort,
94    Application(u64),
95}
96
97impl fmt::Display for PackError {
98    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
99        write!(f, "{:?}", self)
100    }
101}
102
103impl error::Error for PackError {}
104
105/// Binary serialization trait with forward compatibility.
106///
107/// Pack provides efficient binary encoding designed for protocol evolution.
108/// Most types should use `#[derive(Pack)]` from `netidx-derive` rather than
109/// implementing this trait manually.
110///
111/// # Encoding Format
112///
113/// By default derive(Pack) uses length-wrapped encoding: each value
114/// is prefixed with its encoded length, allowing decoders to skip
115/// unknown data. This enables:
116///
117/// - Adding new fields to the end of structs
118/// - Adding new variants to enums (so long as you don't send them to old decoders)
119/// - Adding new fields to the end of struct variants
120///
121/// Old decoders will skip over extra data when they are done
122/// decoding, allowing protocol extensibility with the performance of
123/// a fixed field decoder.
124///
125/// # Order Matters
126///
127/// Pack is a fixed field decoder, so the order of fields in struct
128/// types, enum types, and enum structs matters. The encoder packs
129/// each struct field in the order it is declared in the type, and the
130/// decoder expects fields to appear in that order. This avoids the
131/// overhead of tagged fields, but means that new fields can only be
132/// added at the end, and fields can never be reordered.
133///
134/// Enum tags are similarly allocated in type declaration order, so
135/// when adding new fields to enums, you must add them at the end of
136/// the type (unless you write a custom Pack implementation).
137///
138/// # Example
139///
140/// ```ignore
141/// use netidx_core::pack::Pack;
142///
143/// #[derive(Pack)]
144/// struct Point {
145///     x: i32,
146///     y: i32,
147/// }
148/// ```
149pub trait Pack {
150    fn const_encoded_len() -> Option<usize> {
151        None
152    }
153    fn encoded_len(&self) -> usize;
154    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError>;
155    fn decode(buf: &mut impl Buf) -> Result<Self, PackError>
156    where
157        Self: std::marker::Sized;
158
159    fn decode_into(&mut self, buf: &mut impl Buf) -> Result<(), PackError>
160    where
161        Self: std::marker::Sized,
162    {
163        Ok(*self = <Self as Pack>::decode(buf)?)
164    }
165}
166
167impl<T: Pack + Any + Send + Sync + Poolable> Pack for GPooled<T> {
168    fn encoded_len(&self) -> usize {
169        <T as Pack>::encoded_len(&**self)
170    }
171
172    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
173        <T as Pack>::encode(&**self, buf)
174    }
175
176    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
177        let mut t = take_any::<T>(1000, 1000);
178        <T as Pack>::decode_into(&mut *t, buf)?;
179        Ok(t)
180    }
181
182    fn decode_into(&mut self, buf: &mut impl Buf) -> Result<(), PackError> {
183        <T as Pack>::decode_into(&mut **self, buf)
184    }
185}
186
187impl Pack for net::SocketAddr {
188    fn encoded_len(&self) -> usize {
189        match self {
190            net::SocketAddr::V4(_) => 7,
191            net::SocketAddr::V6(_) => 27,
192        }
193    }
194
195    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
196        match self {
197            net::SocketAddr::V4(v4) => {
198                buf.put_u8(0);
199                buf.put_u32(u32::from_be_bytes(v4.ip().octets()));
200                buf.put_u16(v4.port());
201            }
202            net::SocketAddr::V6(v6) => {
203                buf.put_u8(1);
204                for s in &v6.ip().segments() {
205                    buf.put_u16(*s);
206                }
207                buf.put_u16(v6.port());
208                buf.put_u32(v6.flowinfo());
209                buf.put_u32(v6.scope_id());
210            }
211        }
212        Ok(())
213    }
214
215    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
216        match <u8 as Pack>::decode(buf)? {
217            0 => {
218                let ip =
219                    net::Ipv4Addr::from(u32::to_be_bytes(<u32 as Pack>::decode(buf)?));
220                let port = <u16 as Pack>::decode(buf)?;
221                Ok(net::SocketAddr::V4(net::SocketAddrV4::new(ip, port)))
222            }
223            1 => {
224                let mut segments = [0u16; 8];
225                for i in 0..8 {
226                    segments[i] = <u16 as Pack>::decode(buf)?;
227                }
228                let port = <u16 as Pack>::decode(buf)?;
229                let flowinfo = <u32 as Pack>::decode(buf)?;
230                let scope_id = <u32 as Pack>::decode(buf)?;
231                let ip = net::Ipv6Addr::from(segments);
232                let v6 = net::SocketAddrV6::new(ip, port, flowinfo, scope_id);
233                Ok(net::SocketAddr::V6(v6))
234            }
235            _ => return Err(PackError::UnknownTag),
236        }
237    }
238}
239
240impl Pack for Bytes {
241    fn encoded_len(&self) -> usize {
242        let len = Bytes::len(self);
243        varint_len(len as u64) + len
244    }
245
246    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
247        encode_varint(Bytes::len(self) as u64, buf);
248        Ok(buf.put_slice(&*self))
249    }
250
251    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
252        let len = decode_varint(buf)?;
253        if len as usize > buf.remaining() {
254            Err(PackError::TooBig)
255        } else {
256            Ok(buf.copy_to_bytes(len as usize))
257        }
258    }
259}
260
261#[derive(Debug)]
262pub struct BoundedBytes<const L: usize>(pub Bytes);
263
264impl<const L: usize> Deref for BoundedBytes<L> {
265    type Target = Bytes;
266
267    fn deref(&self) -> &Self::Target {
268        &self.0
269    }
270}
271
272impl<const L: usize> DerefMut for BoundedBytes<L> {
273    fn deref_mut(&mut self) -> &mut Self::Target {
274        &mut self.0
275    }
276}
277
278impl<const L: usize> Pack for BoundedBytes<L> {
279    fn encoded_len(&self) -> usize {
280        let len = Bytes::len(&self.0);
281        varint_len(len as u64) + len
282    }
283
284    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
285        if Bytes::len(self) > L {
286            Err(PackError::TooBig)
287        } else {
288            <Bytes as Pack>::encode(self, buf)
289        }
290    }
291
292    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
293        let len = decode_varint(buf)? as usize;
294        if len > L || len > buf.remaining() {
295            Err(PackError::TooBig)
296        } else {
297            Ok(BoundedBytes(buf.copy_to_bytes(len)))
298        }
299    }
300}
301
302impl Pack for String {
303    fn encoded_len(&self) -> usize {
304        let len = String::len(self);
305        varint_len(len as u64) + len
306    }
307
308    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
309        encode_varint(String::len(self) as u64, buf);
310        Ok(buf.put_slice(self.as_bytes()))
311    }
312
313    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
314        let len = decode_varint(buf)? as usize;
315        if len > buf.remaining() {
316            return Err(PackError::TooBig);
317        }
318        let mut v = vec![0; len];
319        buf.copy_to_slice(&mut v);
320        match String::from_utf8(v) {
321            Ok(s) => Ok(s),
322            Err(_) => Err(PackError::InvalidFormat),
323        }
324    }
325}
326
327impl Pack for CompactString {
328    fn encoded_len(&self) -> usize {
329        let len = CompactString::len(self);
330        varint_len(len as u64) + len
331    }
332
333    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
334        encode_varint(CompactString::len(self) as u64, buf);
335        Ok(buf.put_slice(self.as_bytes()))
336    }
337
338    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
339        thread_local! {
340            static TMP: RefCell<Vec<u8>> = RefCell::new(Vec::new());
341        }
342        let len = decode_varint(buf)? as usize;
343        if len > buf.remaining() {
344            return Err(PackError::TooBig);
345        }
346        TMP.with(|tmp| {
347            let mut tmp = tmp.borrow_mut();
348            tmp.resize(len, 0);
349            buf.copy_to_slice(&mut tmp);
350            if tmp.capacity() > cmp::max(1024, tmp.len() << 2) {
351                tmp.shrink_to(1024)
352            }
353            match CompactString::from_utf8(&tmp[..]) {
354                Ok(s) => Ok(s),
355                Err(_) => Err(PackError::InvalidFormat),
356            }
357        })
358    }
359}
360
361impl<const C: usize> Pack for ArrayString<C> {
362    fn encoded_len(&self) -> usize {
363        let len = ArrayString::len(self);
364        varint_len(len as u64) + len
365    }
366
367    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
368        encode_varint(ArrayString::len(self) as u64, buf);
369        Ok(buf.put_slice(self.as_bytes()))
370    }
371
372    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
373        let len = decode_varint(buf)? as usize;
374        if len > buf.remaining() || len > C {
375            return Err(PackError::TooBig);
376        }
377        let mut v = [0u8; C];
378        buf.copy_to_slice(&mut v[..len]);
379        let mut res: ArrayString<C> = ArrayString::new();
380        match str::from_utf8(&v[..len]) {
381            Ok(s) => {
382                res.push_str(s);
383                Ok(res)
384            }
385            Err(_) => Err(PackError::InvalidFormat),
386        }
387    }
388}
389
390impl Pack for Arc<str> {
391    fn encoded_len(&self) -> usize {
392        let s: &str = &*self;
393        let len = s.len();
394        varint_len(len as u64) + len
395    }
396
397    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
398        let s: &str = &*self;
399        encode_varint(s.len() as u64, buf);
400        Ok(buf.put_slice(self.as_bytes()))
401    }
402
403    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
404        let len = decode_varint(buf)? as usize;
405        if len > buf.remaining() {
406            return Err(PackError::TooBig);
407        }
408        let mut v = vec![0; len];
409        buf.copy_to_slice(&mut v);
410        match String::from_utf8(v) {
411            Ok(s) => Ok(Arc::from(s)),
412            Err(_) => Err(PackError::InvalidFormat),
413        }
414    }
415}
416
417impl Pack for triomphe::Arc<str> {
418    fn encoded_len(&self) -> usize {
419        let s: &str = &*self;
420        let len = s.len();
421        varint_len(len as u64) + len
422    }
423
424    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
425        let s: &str = &*self;
426        encode_varint(s.len() as u64, buf);
427        Ok(buf.put_slice(self.as_bytes()))
428    }
429
430    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
431        let len = decode_varint(buf)? as usize;
432        if len > buf.remaining() {
433            return Err(PackError::TooBig);
434        }
435        let mut v = vec![0; len];
436        buf.copy_to_slice(&mut v);
437        match String::from_utf8(v) {
438            Ok(s) => Ok(triomphe::Arc::from(s)),
439            Err(_) => Err(PackError::InvalidFormat),
440        }
441    }
442}
443
444impl Pack for ArcStr {
445    fn encoded_len(&self) -> usize {
446        let s: &str = &*self;
447        let len = s.len();
448        varint_len(len as u64) + len
449    }
450
451    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
452        let s: &str = &*self;
453        encode_varint(s.len() as u64, buf);
454        Ok(buf.put_slice(self.as_bytes()))
455    }
456
457    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
458        let len = decode_varint(buf)? as usize;
459        if len > buf.remaining() {
460            Err(PackError::TooBig)
461        } else {
462            let res = match str::from_utf8(&buf.chunk()[0..len]) {
463                Ok(s) => Ok(ArcStr::from(s)),
464                Err(_) => Err(PackError::InvalidFormat),
465            };
466            buf.advance(len);
467            res
468        }
469    }
470}
471
472pub fn varint_len(value: u64) -> usize {
473    ((((value | 1).leading_zeros() ^ 63) * 9 + 73) >> 6) as usize
474}
475
476pub fn encode_varint(mut value: u64, buf: &mut impl BufMut) {
477    for _ in 0..10 {
478        if value < 0x80 {
479            buf.put_u8(value as u8);
480            break;
481        } else {
482            buf.put_u8(((value & 0x7F) | 0x80) as u8);
483            value >>= 7;
484        }
485    }
486}
487
488pub fn i32_zz(n: i32) -> u32 {
489    ((n << 1) ^ (n >> 31)) as u32
490}
491
492pub fn i32_uzz(n: u32) -> i32 {
493    ((n >> 1) as i32) ^ (((n as i32) << 31) >> 31)
494}
495
496pub fn i64_zz(n: i64) -> u64 {
497    ((n << 1) ^ (n >> 63)) as u64
498}
499
500pub fn i64_uzz(n: u64) -> i64 {
501    ((n >> 1) as i64) ^ (((n as i64) << 63) >> 63)
502}
503
504pub fn decode_varint(buf: &mut impl Buf) -> Result<u64, PackError> {
505    let bytes = buf.chunk();
506    let mut value = 0;
507    for i in 0..10 {
508        if i >= bytes.len() {
509            return Err(PackError::BufferShort);
510        }
511        let byte = bytes[i];
512        value |= ((byte & 0x7F) as u64) << (i * 7);
513        if byte <= 0x7F {
514            buf.advance(i + 1);
515            return Ok(value);
516        }
517    }
518    buf.advance(10);
519    Err(PackError::InvalidFormat)
520}
521
522pub fn len_wrapped_len(len: usize) -> usize {
523    let vlen = varint_len((len + varint_len(len as u64)) as u64);
524    len + vlen
525}
526
527pub fn len_wrapped_encode<B, T, F>(buf: &mut B, t: &T, f: F) -> Result<(), PackError>
528where
529    B: BufMut,
530    T: Pack,
531    F: FnOnce(&mut B) -> Result<(), PackError>,
532{
533    encode_varint(t.encoded_len() as u64, buf);
534    f(buf)
535}
536
537pub fn len_wrapped_decode<B, T, F>(buf: &mut B, f: F) -> Result<T, PackError>
538where
539    B: Buf,
540    T: Pack + 'static,
541    F: FnOnce(&mut buf::Take<&mut B>) -> Result<T, PackError>,
542{
543    let len = decode_varint(buf)?;
544    // this has to be 1 not 2 because empty structs will be encoded as
545    // length 1
546    if len < 1 {
547        return Err(PackError::BufferShort);
548    }
549    let mut limited = buf.take((len - varint_len(len) as u64) as usize);
550    let r = f(&mut limited);
551    if limited.has_remaining() {
552        limited.advance(limited.remaining());
553    }
554    r
555}
556
557impl Pack for f32 {
558    fn const_encoded_len() -> Option<usize> {
559        Some(mem::size_of::<f32>())
560    }
561
562    fn encoded_len(&self) -> usize {
563        mem::size_of::<f32>()
564    }
565
566    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
567        Ok(buf.put_f32(*self))
568    }
569
570    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
571        if buf.remaining() < mem::size_of::<f32>() {
572            Err(PackError::BufferShort)
573        } else {
574            Ok(buf.get_f32())
575        }
576    }
577}
578
579impl Pack for f64 {
580    fn const_encoded_len() -> Option<usize> {
581        Some(mem::size_of::<f64>())
582    }
583
584    fn encoded_len(&self) -> usize {
585        mem::size_of::<f64>()
586    }
587
588    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
589        Ok(buf.put_f64(*self))
590    }
591
592    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
593        if buf.remaining() < mem::size_of::<f64>() {
594            Err(PackError::BufferShort)
595        } else {
596            Ok(buf.get_f64())
597        }
598    }
599}
600
601impl Pack for Decimal {
602    fn const_encoded_len() -> Option<usize> {
603        Some(16)
604    }
605
606    fn encoded_len(&self) -> usize {
607        Self::const_encoded_len().unwrap()
608    }
609
610    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
611        Ok(buf.put_slice(&self.serialize()[..]))
612    }
613
614    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
615        if buf.remaining() < Self::const_encoded_len().unwrap() {
616            Err(PackError::BufferShort)
617        } else {
618            let mut b = [0u8; 16];
619            buf.copy_to_slice(&mut b);
620            Ok(Decimal::deserialize(b))
621        }
622    }
623}
624
625impl Pack for u128 {
626    fn const_encoded_len() -> Option<usize> {
627        Some(mem::size_of::<u128>())
628    }
629
630    fn encoded_len(&self) -> usize {
631        mem::size_of::<u128>()
632    }
633
634    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
635        Ok(buf.put_u128(*self))
636    }
637
638    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
639        if buf.remaining() < mem::size_of::<u128>() {
640            Err(PackError::BufferShort)
641        } else {
642            Ok(buf.get_u128())
643        }
644    }
645}
646
647impl Pack for i128 {
648    fn const_encoded_len() -> Option<usize> {
649        Some(mem::size_of::<i128>())
650    }
651
652    fn encoded_len(&self) -> usize {
653        mem::size_of::<i128>()
654    }
655
656    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
657        Ok(buf.put_i128(*self))
658    }
659
660    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
661        if buf.remaining() < mem::size_of::<i128>() {
662            Err(PackError::BufferShort)
663        } else {
664            Ok(buf.get_i128())
665        }
666    }
667}
668
669impl Pack for u64 {
670    fn const_encoded_len() -> Option<usize> {
671        Some(mem::size_of::<u64>())
672    }
673
674    fn encoded_len(&self) -> usize {
675        mem::size_of::<u64>()
676    }
677
678    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
679        Ok(buf.put_u64(*self))
680    }
681
682    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
683        if buf.remaining() < mem::size_of::<u64>() {
684            Err(PackError::BufferShort)
685        } else {
686            Ok(buf.get_u64())
687        }
688    }
689}
690
691impl Pack for i64 {
692    fn const_encoded_len() -> Option<usize> {
693        Some(mem::size_of::<i64>())
694    }
695
696    fn encoded_len(&self) -> usize {
697        mem::size_of::<i64>()
698    }
699
700    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
701        Ok(buf.put_i64(*self))
702    }
703
704    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
705        if buf.remaining() < mem::size_of::<i64>() {
706            Err(PackError::BufferShort)
707        } else {
708            Ok(buf.get_i64())
709        }
710    }
711}
712
713#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Default, Debug)]
714pub struct Z64(pub u64);
715
716impl Deref for Z64 {
717    type Target = u64;
718
719    fn deref(&self) -> &Self::Target {
720        &self.0
721    }
722}
723
724impl DerefMut for Z64 {
725    fn deref_mut(&mut self) -> &mut Self::Target {
726        &mut self.0
727    }
728}
729
730impl Pack for Z64 {
731    fn encoded_len(&self) -> usize {
732        varint_len(**self)
733    }
734
735    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
736        Ok(encode_varint(**self, buf))
737    }
738
739    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
740        Ok(Z64(decode_varint(buf)?))
741    }
742}
743
744impl Pack for u32 {
745    fn const_encoded_len() -> Option<usize> {
746        Some(mem::size_of::<u32>())
747    }
748
749    fn encoded_len(&self) -> usize {
750        mem::size_of::<u32>()
751    }
752
753    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
754        Ok(buf.put_u32(*self))
755    }
756
757    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
758        if buf.remaining() < mem::size_of::<u32>() {
759            Err(PackError::BufferShort)
760        } else {
761            Ok(buf.get_u32())
762        }
763    }
764}
765
766impl Pack for i32 {
767    fn const_encoded_len() -> Option<usize> {
768        Some(mem::size_of::<i32>())
769    }
770
771    fn encoded_len(&self) -> usize {
772        mem::size_of::<i32>()
773    }
774
775    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
776        Ok(buf.put_i32(*self))
777    }
778
779    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
780        if buf.remaining() < mem::size_of::<i32>() {
781            Err(PackError::BufferShort)
782        } else {
783            Ok(buf.get_i32())
784        }
785    }
786}
787
788impl Pack for u16 {
789    fn const_encoded_len() -> Option<usize> {
790        Some(mem::size_of::<u16>())
791    }
792
793    fn encoded_len(&self) -> usize {
794        mem::size_of::<u16>()
795    }
796
797    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
798        Ok(buf.put_u16(*self))
799    }
800
801    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
802        if buf.remaining() < mem::size_of::<u16>() {
803            Err(PackError::BufferShort)
804        } else {
805            Ok(buf.get_u16())
806        }
807    }
808}
809
810impl Pack for i16 {
811    fn const_encoded_len() -> Option<usize> {
812        Some(mem::size_of::<i16>())
813    }
814
815    fn encoded_len(&self) -> usize {
816        mem::size_of::<i16>()
817    }
818
819    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
820        Ok(buf.put_i16(*self))
821    }
822
823    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
824        if buf.remaining() < mem::size_of::<i16>() {
825            Err(PackError::BufferShort)
826        } else {
827            Ok(buf.get_i16())
828        }
829    }
830}
831
832impl Pack for u8 {
833    fn const_encoded_len() -> Option<usize> {
834        Some(mem::size_of::<u8>())
835    }
836
837    fn encoded_len(&self) -> usize {
838        mem::size_of::<u8>()
839    }
840
841    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
842        Ok(buf.put_u8(*self))
843    }
844
845    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
846        if buf.remaining() < mem::size_of::<u8>() {
847            Err(PackError::BufferShort)
848        } else {
849            Ok(buf.get_u8())
850        }
851    }
852}
853
854impl Pack for i8 {
855    fn const_encoded_len() -> Option<usize> {
856        Some(mem::size_of::<i8>())
857    }
858
859    fn encoded_len(&self) -> usize {
860        mem::size_of::<i8>()
861    }
862
863    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
864        Ok(buf.put_i8(*self))
865    }
866
867    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
868        if buf.remaining() < mem::size_of::<i8>() {
869            Err(PackError::BufferShort)
870        } else {
871            Ok(buf.get_i8())
872        }
873    }
874}
875
876impl<T: Pack, const S: usize> Pack for [T; S] {
877    fn encoded_len(&self) -> usize {
878        self.iter().fold(varint_len(S as u64), |len, t| len + <T as Pack>::encoded_len(t))
879    }
880
881    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
882        encode_varint(S as u64, buf);
883        for t in self {
884            <T as Pack>::encode(t, buf)?
885        }
886        Ok(())
887    }
888
889    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
890        use std::{
891            array,
892            panic::{self, AssertUnwindSafe},
893        };
894        let elts = decode_varint(buf)? as usize;
895        if elts != S {
896            return Err(PackError::InvalidFormat);
897        }
898        // CR estokes: once try_from_fn is stable replace this
899        panic::catch_unwind(AssertUnwindSafe(|| {
900            array::from_fn(|_| <T as Pack>::decode(buf).unwrap())
901        }))
902        .map_err(|_| PackError::InvalidFormat)
903    }
904
905    fn decode_into(&mut self, buf: &mut impl Buf) -> Result<(), PackError> {
906        let elts = decode_varint(buf)? as usize;
907        if elts != S {
908            return Err(PackError::InvalidFormat);
909        }
910        for i in 0..S {
911            self[i] = <T as Pack>::decode(buf)?;
912        }
913        Ok(())
914    }
915}
916
917pub const MAX_VEC: usize = 2 * 1024 * 1024 * 1024;
918
919macro_rules! check_sz {
920    ($elts:expr, $remains:expr, $t:ty) => {
921        let sz = $elts.saturating_mul(mem::size_of::<$t>());
922        if sz > MAX_VEC || sz > $remains << 8 {
923            return Err(PackError::TooBig);
924        }
925    };
926    ($elts:expr, $remains:expr, $t:ty, $u:ty) => {
927        let sz = $elts.saturating_mul(mem::size_of::<$t>() + mem::size_of::<$u>());
928        if sz > MAX_VEC || sz > $remains << 8 {
929            return Err(PackError::TooBig);
930        }
931    };
932}
933
934impl<T: Pack> Pack for Vec<T> {
935    fn encoded_len(&self) -> usize {
936        self.iter().fold(varint_len(Vec::len(self) as u64), |len, t| {
937            len + <T as Pack>::encoded_len(t)
938        })
939    }
940
941    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
942        let len = Vec::len(self);
943        if len * mem::size_of::<T>() > MAX_VEC {
944            return Err(PackError::TooBig);
945        }
946        encode_varint(len as u64, buf);
947        for t in self {
948            <T as Pack>::encode(t, buf)?
949        }
950        Ok(())
951    }
952
953    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
954        let elts = decode_varint(buf)? as usize;
955        check_sz!(elts, buf.remaining(), T);
956        let mut data = Vec::with_capacity(elts);
957        for _ in 0..elts {
958            data.push(<T as Pack>::decode(buf)?);
959        }
960        Ok(data)
961    }
962
963    fn decode_into(&mut self, buf: &mut impl Buf) -> Result<(), PackError> {
964        let elts = decode_varint(buf)? as usize;
965        check_sz!(elts, buf.remaining(), T);
966        if elts > self.capacity() {
967            self.reserve(elts - self.capacity());
968        }
969        for _ in 0..elts {
970            self.push(<T as Pack>::decode(buf)?);
971        }
972        Ok(())
973    }
974}
975
976impl<T: Pack> Pack for Arc<[T]> {
977    fn encoded_len(&self) -> usize {
978        self.iter().fold(varint_len(self.len() as u64), |len, t| {
979            len + <T as Pack>::encoded_len(t)
980        })
981    }
982
983    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
984        encode_varint(self.len() as u64, buf);
985        for t in &**self {
986            <T as Pack>::encode(t, buf)?
987        }
988        Ok(())
989    }
990
991    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
992        let v = <Vec<T> as Pack>::decode(buf)?;
993        Ok(Arc::from(v))
994    }
995}
996
997impl<T: Pack> Pack for triomphe::Arc<[T]> {
998    fn encoded_len(&self) -> usize {
999        self.iter().fold(varint_len(self.len() as u64), |len, t| {
1000            len + <T as Pack>::encoded_len(t)
1001        })
1002    }
1003
1004    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
1005        encode_varint(self.len() as u64, buf);
1006        for t in &**self {
1007            <T as Pack>::encode(t, buf)?
1008        }
1009        Ok(())
1010    }
1011
1012    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
1013        let v = <Vec<T> as Pack>::decode(buf)?;
1014        Ok(triomphe::Arc::from(v))
1015    }
1016}
1017
1018impl<T: Pack, const C: usize> Pack for ArrayVec<T, C> {
1019    fn encoded_len(&self) -> usize {
1020        self.iter().fold(varint_len(ArrayVec::len(self) as u64), |len, t| {
1021            len + <T as Pack>::encoded_len(t)
1022        })
1023    }
1024
1025    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
1026        encode_varint(ArrayVec::len(self) as u64, buf);
1027        for t in self {
1028            <T as Pack>::encode(t, buf)?
1029        }
1030        Ok(())
1031    }
1032
1033    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
1034        let elts = decode_varint(buf)? as usize;
1035        if elts > C {
1036            return Err(PackError::TooBig);
1037        }
1038        let mut data = ArrayVec::new();
1039        for _ in 0..elts {
1040            data.push(<T as Pack>::decode(buf)?);
1041        }
1042        Ok(data)
1043    }
1044
1045    fn decode_into(&mut self, buf: &mut impl Buf) -> Result<(), PackError> {
1046        let elts = decode_varint(buf)? as usize;
1047        if elts > C - self.len() {
1048            return Err(PackError::TooBig);
1049        }
1050        for _ in 0..elts {
1051            self.push(<T as Pack>::decode(buf)?);
1052        }
1053        Ok(())
1054    }
1055}
1056
1057impl<T: Pack> Pack for VecDeque<T> {
1058    fn encoded_len(&self) -> usize {
1059        self.iter().fold(varint_len(VecDeque::len(self) as u64), |len, t| {
1060            len + <T as Pack>::encoded_len(t)
1061        })
1062    }
1063
1064    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
1065        let len = VecDeque::len(self);
1066        if len * mem::size_of::<T>() > MAX_VEC {
1067            return Err(PackError::TooBig);
1068        }
1069        encode_varint(len as u64, buf);
1070        for t in self {
1071            <T as Pack>::encode(t, buf)?
1072        }
1073        Ok(())
1074    }
1075
1076    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
1077        let elts = decode_varint(buf)? as usize;
1078        check_sz!(elts, buf.remaining(), T);
1079        let mut data = VecDeque::with_capacity(elts);
1080        for _ in 0..elts {
1081            data.push_back(<T as Pack>::decode(buf)?);
1082        }
1083        Ok(data)
1084    }
1085
1086    fn decode_into(&mut self, buf: &mut impl Buf) -> Result<(), PackError> {
1087        let elts = decode_varint(buf)? as usize;
1088        check_sz!(elts, buf.remaining(), T);
1089        if elts > self.capacity() {
1090            self.reserve(elts - self.capacity());
1091        }
1092        for _ in 0..elts {
1093            self.push_back(<T as Pack>::decode(buf)?);
1094        }
1095        Ok(())
1096    }
1097}
1098
1099macro_rules! impl_hashmap {
1100    ($ty:ident) => {
1101        impl<K, V, R> Pack for $ty<K, V, R>
1102        where
1103            K: Pack + Hash + Eq,
1104            V: Pack,
1105            R: Default + BuildHasher,
1106        {
1107            fn encoded_len(&self) -> usize {
1108                self.iter().fold(varint_len(self.len() as u64), |len, (k, v)| {
1109                    len + <K as Pack>::encoded_len(k) + <V as Pack>::encoded_len(v)
1110                })
1111            }
1112
1113            fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
1114                let len = self.len();
1115                if len * (mem::size_of::<K>() + mem::size_of::<V>()) > MAX_VEC {
1116                    return Err(PackError::TooBig);
1117                }
1118                encode_varint(self.len() as u64, buf);
1119                for (k, v) in self {
1120                    <K as Pack>::encode(k, buf)?;
1121                    <V as Pack>::encode(v, buf)?;
1122                }
1123                Ok(())
1124            }
1125
1126            fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
1127                let elts = decode_varint(buf)? as usize;
1128                check_sz!(elts, buf.remaining(), K, V);
1129                let mut data = $ty::with_capacity_and_hasher(elts, R::default());
1130                for _ in 0..elts {
1131                    let k = <K as Pack>::decode(buf)?;
1132                    let v = <V as Pack>::decode(buf)?;
1133                    data.insert(k, v);
1134                }
1135                Ok(data)
1136            }
1137
1138            fn decode_into(&mut self, buf: &mut impl Buf) -> Result<(), PackError> {
1139                let elts = decode_varint(buf)? as usize;
1140                check_sz!(elts, buf.remaining(), K, V);
1141                if elts > self.capacity() {
1142                    self.reserve(elts - self.capacity());
1143                }
1144                for _ in 0..elts {
1145                    let k = <K as Pack>::decode(buf)?;
1146                    let v = <V as Pack>::decode(buf)?;
1147                    self.insert(k, v);
1148                }
1149                Ok(())
1150            }
1151        }
1152    };
1153}
1154
1155impl_hashmap!(HashMap);
1156impl_hashmap!(IndexMap);
1157
1158impl<K: Ord + Pack, V: Pack> Pack for BTreeMap<K, V> {
1159    fn encoded_len(&self) -> usize {
1160        self.iter().fold(varint_len(self.len() as u64), |len, (k, v)| {
1161            len + <K as Pack>::encoded_len(k) + <V as Pack>::encoded_len(v)
1162        })
1163    }
1164
1165    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
1166        let len = self.len();
1167        if len * (mem::size_of::<K>() + mem::size_of::<V>()) > MAX_VEC {
1168            return Err(PackError::TooBig);
1169        }
1170        encode_varint(len as u64, buf);
1171        for (k, v) in self {
1172            <K as Pack>::encode(k, buf)?;
1173            <V as Pack>::encode(v, buf)?;
1174        }
1175        Ok(())
1176    }
1177
1178    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
1179        let elts = decode_varint(buf)? as usize;
1180        check_sz!(elts, buf.remaining(), K, V);
1181        let mut data = BTreeMap::default();
1182        for _ in 0..elts {
1183            let k = <K as Pack>::decode(buf)?;
1184            let v = <V as Pack>::decode(buf)?;
1185            data.insert(k, v);
1186        }
1187        Ok(data)
1188    }
1189
1190    fn decode_into(&mut self, buf: &mut impl Buf) -> Result<(), PackError> {
1191        let elts = decode_varint(buf)? as usize;
1192        check_sz!(elts, buf.remaining(), K, V);
1193        for _ in 0..elts {
1194            let k = <K as Pack>::decode(buf)?;
1195            let v = <V as Pack>::decode(buf)?;
1196            self.insert(k, v);
1197        }
1198        Ok(())
1199    }
1200}
1201
1202impl<K: Ord + Pack + Clone, V: Pack + Clone, const SIZE: usize> Pack
1203    for immutable_chunkmap::map::Map<K, V, SIZE>
1204{
1205    fn encoded_len(&self) -> usize {
1206        self.into_iter().fold(varint_len(self.len() as u64), |len, (k, v)| {
1207            len + <K as Pack>::encoded_len(k) + <V as Pack>::encoded_len(v)
1208        })
1209    }
1210
1211    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
1212        let len = self.len();
1213        if len * (mem::size_of::<K>() + mem::size_of::<V>()) > MAX_VEC {
1214            return Err(PackError::TooBig);
1215        }
1216        encode_varint(len as u64, buf);
1217        for (k, v) in self {
1218            <K as Pack>::encode(k, buf)?;
1219            <V as Pack>::encode(v, buf)?;
1220        }
1221        Ok(())
1222    }
1223
1224    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
1225        let elts = decode_varint(buf)? as usize;
1226        check_sz!(elts, buf.remaining(), K, V);
1227        let mut data: LPooled<Vec<(K, V)>> = LPooled::take_sz(1024, 64 * 1024);
1228        let cap = data.capacity();
1229        if cap < elts {
1230            data.reserve(elts - cap);
1231        }
1232        for _ in 0..elts {
1233            let k = <K as Pack>::decode(buf)?;
1234            let v = <V as Pack>::decode(buf)?;
1235            data.push((k, v))
1236        }
1237        Ok(immutable_chunkmap::map::Map::from_iter(data.drain(..)))
1238    }
1239
1240    fn decode_into(&mut self, buf: &mut impl Buf) -> Result<(), PackError> {
1241        let elts = decode_varint(buf)? as usize;
1242        check_sz!(elts, buf.remaining(), K, V);
1243        for _ in 0..elts {
1244            let k = <K as Pack>::decode(buf)?;
1245            let v = <V as Pack>::decode(buf)?;
1246            self.insert_cow(k, v);
1247        }
1248        Ok(())
1249    }
1250}
1251
1252macro_rules! impl_hashset {
1253    ($ty:ident) => {
1254        impl<K, R> Pack for $ty<K, R>
1255        where
1256            K: Pack + Hash + Eq,
1257            R: Default + BuildHasher,
1258        {
1259            fn encoded_len(&self) -> usize {
1260                self.iter().fold(varint_len(self.len() as u64), |len, k| {
1261                    len + <K as Pack>::encoded_len(k)
1262                })
1263            }
1264
1265            fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
1266                let len = self.len();
1267                if len * mem::size_of::<K>() > MAX_VEC {
1268                    return Err(PackError::TooBig);
1269                }
1270                encode_varint(len as u64, buf);
1271                for k in self {
1272                    <K as Pack>::encode(k, buf)?;
1273                }
1274                Ok(())
1275            }
1276
1277            fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
1278                let elts = decode_varint(buf)? as usize;
1279                check_sz!(elts, buf.remaining(), K);
1280                let mut data = $ty::with_capacity_and_hasher(elts, R::default());
1281                for _ in 0..elts {
1282                    data.insert(<K as Pack>::decode(buf)?);
1283                }
1284                Ok(data)
1285            }
1286
1287            fn decode_into(&mut self, buf: &mut impl Buf) -> Result<(), PackError> {
1288                let elts = decode_varint(buf)? as usize;
1289                check_sz!(elts, buf.remaining(), K);
1290                if elts > self.capacity() {
1291                    self.reserve(elts - self.capacity());
1292                }
1293                for _ in 0..elts {
1294                    self.insert(<K as Pack>::decode(buf)?);
1295                }
1296                Ok(())
1297            }
1298        }
1299    };
1300}
1301
1302impl_hashset!(HashSet);
1303impl_hashset!(IndexSet);
1304
1305impl<K: Ord + Pack> Pack for BTreeSet<K> {
1306    fn encoded_len(&self) -> usize {
1307        self.iter().fold(varint_len(self.len() as u64), |len, k| {
1308            len + <K as Pack>::encoded_len(k)
1309        })
1310    }
1311
1312    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
1313        let len = self.len();
1314        if len * mem::size_of::<K>() > MAX_VEC {
1315            return Err(PackError::TooBig);
1316        }
1317        encode_varint(len as u64, buf);
1318        for k in self {
1319            <K as Pack>::encode(k, buf)?;
1320        }
1321        Ok(())
1322    }
1323
1324    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
1325        let elts = decode_varint(buf)? as usize;
1326        check_sz!(elts, buf.remaining(), K);
1327        let mut data = BTreeSet::default();
1328        for _ in 0..elts {
1329            data.insert(<K as Pack>::decode(buf)?);
1330        }
1331        Ok(data)
1332    }
1333
1334    fn decode_into(&mut self, buf: &mut impl Buf) -> Result<(), PackError> {
1335        let elts = decode_varint(buf)? as usize;
1336        check_sz!(elts, buf.remaining(), K);
1337        for _ in 0..elts {
1338            self.insert(<K as Pack>::decode(buf)?);
1339        }
1340        Ok(())
1341    }
1342}
1343
1344impl<K: Ord + Pack + Clone, const SIZE: usize> Pack
1345    for immutable_chunkmap::set::Set<K, SIZE>
1346{
1347    fn encoded_len(&self) -> usize {
1348        self.into_iter().fold(varint_len(self.len() as u64), |len, k| {
1349            len + <K as Pack>::encoded_len(k)
1350        })
1351    }
1352
1353    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
1354        let len = self.len();
1355        if len * mem::size_of::<K>() > MAX_VEC {
1356            return Err(PackError::TooBig);
1357        }
1358        encode_varint(len as u64, buf);
1359        for k in self {
1360            <K as Pack>::encode(k, buf)?;
1361        }
1362        Ok(())
1363    }
1364
1365    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
1366        let elts = decode_varint(buf)? as usize;
1367        check_sz!(elts, buf.remaining(), K);
1368        let mut data: LPooled<Vec<K>> = LPooled::take_sz(1024, 64 * 1024);
1369        let cap = data.capacity();
1370        if cap < elts {
1371            data.reserve(elts - cap);
1372        }
1373        for _ in 0..elts {
1374            data.push(<K as Pack>::decode(buf)?);
1375        }
1376        Ok(immutable_chunkmap::set::Set::from_iter(data.drain(..)))
1377    }
1378
1379    fn decode_into(&mut self, buf: &mut impl Buf) -> Result<(), PackError> {
1380        let elts = decode_varint(buf)? as usize;
1381        check_sz!(elts, buf.remaining(), K);
1382        for _ in 0..elts {
1383            self.insert_cow(<K as Pack>::decode(buf)?);
1384        }
1385        Ok(())
1386    }
1387}
1388
1389impl<T: Pack> Pack for Option<T> {
1390    fn encoded_len(&self) -> usize {
1391        1 + match self {
1392            None => 0,
1393            Some(v) => <T as Pack>::encoded_len(v),
1394        }
1395    }
1396
1397    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
1398        match self {
1399            None => Ok(buf.put_u8(0)),
1400            Some(v) => {
1401                buf.put_u8(1);
1402                <T as Pack>::encode(v, buf)
1403            }
1404        }
1405    }
1406
1407    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
1408        match <u8 as Pack>::decode(buf)? {
1409            0 => Ok(None),
1410            1 => Ok(Some(<T as Pack>::decode(buf)?)),
1411            _ => return Err(PackError::UnknownTag),
1412        }
1413    }
1414}
1415
1416impl<T: Pack, U: Pack> Pack for (T, U) {
1417    fn encoded_len(&self) -> usize {
1418        <T as Pack>::encoded_len(&self.0) + <U as Pack>::encoded_len(&self.1)
1419    }
1420
1421    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
1422        <T as Pack>::encode(&self.0, buf)?;
1423        Ok(<U as Pack>::encode(&self.1, buf)?)
1424    }
1425
1426    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
1427        let fst = <T as Pack>::decode(buf)?;
1428        let snd = <U as Pack>::decode(buf)?;
1429        Ok((fst, snd))
1430    }
1431}
1432
1433impl<T: Pack, U: Pack, V: Pack> Pack for (T, U, V) {
1434    fn encoded_len(&self) -> usize {
1435        <T as Pack>::encoded_len(&self.0)
1436            + <U as Pack>::encoded_len(&self.1)
1437            + <V as Pack>::encoded_len(&self.2)
1438    }
1439
1440    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
1441        <T as Pack>::encode(&self.0, buf)?;
1442        <U as Pack>::encode(&self.1, buf)?;
1443        Ok(<V as Pack>::encode(&self.2, buf)?)
1444    }
1445
1446    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
1447        let fst = <T as Pack>::decode(buf)?;
1448        let snd = <U as Pack>::decode(buf)?;
1449        let trd = <V as Pack>::decode(buf)?;
1450        Ok((fst, snd, trd))
1451    }
1452}
1453
1454impl<T: Pack, U: Pack, V: Pack, W: Pack> Pack for (T, U, V, W) {
1455    fn encoded_len(&self) -> usize {
1456        <T as Pack>::encoded_len(&self.0)
1457            + <U as Pack>::encoded_len(&self.1)
1458            + <V as Pack>::encoded_len(&self.2)
1459            + <W as Pack>::encoded_len(&self.3)
1460    }
1461
1462    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
1463        <T as Pack>::encode(&self.0, buf)?;
1464        <U as Pack>::encode(&self.1, buf)?;
1465        <V as Pack>::encode(&self.2, buf)?;
1466        Ok(<W as Pack>::encode(&self.3, buf)?)
1467    }
1468
1469    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
1470        let fst = <T as Pack>::decode(buf)?;
1471        let snd = <U as Pack>::decode(buf)?;
1472        let trd = <V as Pack>::decode(buf)?;
1473        let fth = <W as Pack>::decode(buf)?;
1474        Ok((fst, snd, trd, fth))
1475    }
1476}
1477
1478impl Pack for bool {
1479    fn const_encoded_len() -> Option<usize> {
1480        Some(mem::size_of::<bool>())
1481    }
1482
1483    fn encoded_len(&self) -> usize {
1484        mem::size_of::<bool>()
1485    }
1486
1487    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
1488        Ok(buf.put_u8(if *self { 1 } else { 0 }))
1489    }
1490
1491    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
1492        match <u8 as Pack>::decode(buf)? {
1493            0 => Ok(false),
1494            1 => Ok(true),
1495            _ => Err(PackError::UnknownTag),
1496        }
1497    }
1498}
1499
1500const YEAR_MASK: u32 = 0xFFFF_FE00;
1501const MONTH_MASK: u32 = 0x0000_01E0;
1502const DAY_MASK: u32 = 0x0000_001F;
1503
1504impl Pack for NaiveDate {
1505    fn const_encoded_len() -> Option<usize> {
1506        Some(mem::size_of::<u32>())
1507    }
1508
1509    fn encoded_len(&self) -> usize {
1510        <Self as Pack>::const_encoded_len().unwrap()
1511    }
1512
1513    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
1514        let i = (self.year() as u32) << 9;
1515        let i = i | self.month() << 5;
1516        let i = i | self.day();
1517        Ok(buf.put_u32(i))
1518    }
1519
1520    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
1521        let i: u32 = Pack::decode(buf)?;
1522        let year = (i & YEAR_MASK) as i32 >> 9;
1523        let month = (i & MONTH_MASK) >> 5;
1524        let day = i & DAY_MASK;
1525        match Self::from_ymd_opt(year, month, day) {
1526            Some(d) => Ok(d),
1527            None => Err(PackError::InvalidFormat),
1528        }
1529    }
1530}
1531
1532impl Pack for NaiveDateTime {
1533    fn const_encoded_len() -> Option<usize> {
1534        Some(mem::size_of::<i64>() + mem::size_of::<u32>())
1535    }
1536
1537    fn encoded_len(&self) -> usize {
1538        <DateTime<Utc> as Pack>::const_encoded_len().unwrap()
1539    }
1540
1541    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
1542        buf.put_i64(self.and_utc().timestamp());
1543        Ok(buf.put_u32(self.and_utc().timestamp_subsec_nanos()))
1544    }
1545
1546    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
1547        let ts = Pack::decode(buf)?;
1548        let ns = Pack::decode(buf)?;
1549        let ndt =
1550            DateTime::from_timestamp(ts, ns).ok_or_else(|| PackError::InvalidFormat)?;
1551        Ok(ndt.naive_utc())
1552    }
1553}
1554
1555impl Pack for DateTime<Utc> {
1556    fn const_encoded_len() -> Option<usize> {
1557        Some(mem::size_of::<i64>() + mem::size_of::<u32>())
1558    }
1559
1560    fn encoded_len(&self) -> usize {
1561        <DateTime<Utc> as Pack>::const_encoded_len().unwrap()
1562    }
1563
1564    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
1565        buf.put_i64(self.timestamp());
1566        Ok(buf.put_u32(self.timestamp_subsec_nanos()))
1567    }
1568
1569    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
1570        let ts = Pack::decode(buf)?;
1571        let ns = Pack::decode(buf)?;
1572        let dt =
1573            DateTime::from_timestamp(ts, ns).ok_or_else(|| PackError::InvalidFormat)?;
1574        Ok(dt)
1575    }
1576}
1577
1578impl Pack for Duration {
1579    fn const_encoded_len() -> Option<usize> {
1580        Some(mem::size_of::<i64>() + mem::size_of::<u32>())
1581    }
1582
1583    fn encoded_len(&self) -> usize {
1584        <Duration as Pack>::const_encoded_len().unwrap()
1585    }
1586
1587    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
1588        buf.put_u64(self.as_secs());
1589        Ok(buf.put_u32(self.subsec_nanos()))
1590    }
1591
1592    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
1593        let secs = Pack::decode(buf)?;
1594        let ns = Pack::decode(buf)?;
1595        Ok(Duration::new(secs, ns))
1596    }
1597}
1598
1599impl Pack for chrono::Duration {
1600    fn encoded_len(&self) -> usize {
1601        mem::size_of::<i64>()
1602    }
1603
1604    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
1605        match self.num_nanoseconds() {
1606            Some(i) => Pack::encode(&i, buf),
1607            None => Err(PackError::InvalidFormat),
1608        }
1609    }
1610
1611    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
1612        Ok(chrono::Duration::nanoseconds(Pack::decode(buf)?))
1613    }
1614}
1615
1616impl Pack for () {
1617    fn const_encoded_len() -> Option<usize> {
1618        Some(0)
1619    }
1620
1621    fn encoded_len(&self) -> usize {
1622        <() as Pack>::const_encoded_len().unwrap()
1623    }
1624
1625    fn encode(&self, _buf: &mut impl BufMut) -> Result<(), PackError> {
1626        Ok(())
1627    }
1628
1629    fn decode(_buf: &mut impl Buf) -> Result<Self, PackError> {
1630        Ok(())
1631    }
1632}
1633
1634impl Pack for uuid::Uuid {
1635    fn const_encoded_len() -> Option<usize> {
1636        Some(mem::size_of::<u128>())
1637    }
1638
1639    fn encoded_len(&self) -> usize {
1640        Self::const_encoded_len().unwrap()
1641    }
1642
1643    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
1644        Pack::encode(&self.as_u128(), buf)
1645    }
1646
1647    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
1648        Ok(uuid::Uuid::from_u128(Pack::decode(buf)?))
1649    }
1650}
1651
1652impl Pack for usize {
1653    fn const_encoded_len() -> Option<usize> {
1654        Some(mem::size_of::<u64>())
1655    }
1656
1657    fn encoded_len(&self) -> usize {
1658        Self::const_encoded_len().unwrap()
1659    }
1660
1661    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
1662        <u64 as Pack>::encode(&(*self as u64), buf)
1663    }
1664
1665    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
1666        Ok(<u64 as Pack>::decode(buf)? as usize)
1667    }
1668}
1669
1670impl<T: Pack, U: Pack> Pack for result::Result<T, U> {
1671    fn encoded_len(&self) -> usize {
1672        1 + match self {
1673            Ok(t) => Pack::encoded_len(t),
1674            Err(u) => Pack::encoded_len(u),
1675        }
1676    }
1677
1678    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
1679        match self {
1680            Ok(t) => {
1681                <u8 as Pack>::encode(&0, buf)?;
1682                Pack::encode(t, buf)
1683            }
1684            Err(u) => {
1685                <u8 as Pack>::encode(&1, buf)?;
1686                Pack::encode(u, buf)
1687            }
1688        }
1689    }
1690
1691    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
1692        match <u8 as Pack>::decode(buf)? {
1693            0 => Ok(Ok(Pack::decode(buf)?)),
1694            1 => Ok(Err(Pack::decode(buf)?)),
1695            _ => Err(PackError::UnknownTag),
1696        }
1697    }
1698}
1699
1700impl<T: Pack> Pack for Arc<T> {
1701    fn encoded_len(&self) -> usize {
1702        Pack::encoded_len(&**self)
1703    }
1704
1705    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
1706        Pack::encode(&**self, buf)
1707    }
1708
1709    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
1710        Ok(Arc::new(Pack::decode(buf)?))
1711    }
1712}
1713
1714impl<T: Pack> Pack for triomphe::Arc<T> {
1715    fn encoded_len(&self) -> usize {
1716        Pack::encoded_len(&**self)
1717    }
1718
1719    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
1720        Pack::encode(&**self, buf)
1721    }
1722
1723    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
1724        Ok(triomphe::Arc::new(Pack::decode(buf)?))
1725    }
1726}
1727
1728thread_local! {
1729    static ERR: RefCell<BytesMut> = RefCell::new(BytesMut::new());
1730}
1731
1732fn write_anyhow(s: &mut BytesMut, e: &anyhow::Error) {
1733    use std::fmt::Write;
1734    s.clear();
1735    let _ = write!(s, "{}", e);
1736}
1737
1738// this won't round trip to exactly the same object
1739impl Pack for anyhow::Error {
1740    fn encoded_len(&self) -> usize {
1741        ERR.with(|s| {
1742            let mut s = s.borrow_mut();
1743            write_anyhow(&mut *s, self);
1744            let len = s.len();
1745            varint_len(len as u64) + len
1746        })
1747    }
1748
1749    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
1750        ERR.with(|s| {
1751            let mut s = s.borrow_mut();
1752            write_anyhow(&mut *s, self);
1753            let len = s.len();
1754            encode_varint(len as u64, buf);
1755            Ok(buf.put_slice(&*s))
1756        })
1757    }
1758
1759    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
1760        let len = decode_varint(buf)? as usize;
1761        if len > buf.remaining() {
1762            Err(PackError::BufferShort)
1763        } else {
1764            let b = buf.copy_to_bytes(len);
1765            let s = match str::from_utf8(&b[..]) {
1766                Ok(s) => s,
1767                Err(_) => return Err(PackError::InvalidFormat),
1768            };
1769            Ok(anyhow::anyhow!(ArcStr::from(s)))
1770        }
1771    }
1772}
1773
1774impl<T: Pack> Pack for Box<T> {
1775    fn encoded_len(&self) -> usize {
1776        Pack::encoded_len(&**self)
1777    }
1778
1779    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
1780        Pack::encode(&**self, buf)
1781    }
1782
1783    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
1784        Ok(Box::new(Pack::decode(buf)?))
1785    }
1786}
1787
1788use smallvec::{Array, SmallVec};
1789
1790impl<A> Pack for SmallVec<A>
1791where
1792    A: Array,
1793    <A as Array>::Item: Pack,
1794{
1795    fn encoded_len(&self) -> usize {
1796        self.iter().fold(varint_len(SmallVec::len(self) as u64), |len, t| {
1797            len + Pack::encoded_len(t)
1798        })
1799    }
1800
1801    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
1802        let len = SmallVec::len(self);
1803        if len * mem::size_of::<<A as Array>::Item>() > MAX_VEC {
1804            return Err(PackError::TooBig);
1805        }
1806        encode_varint(len as u64, buf);
1807        for t in self {
1808            Pack::encode(t, buf)?
1809        }
1810        Ok(())
1811    }
1812
1813    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
1814        let elts = decode_varint(buf)? as usize;
1815        check_sz!(elts, buf.remaining(), <A as Array>::Item);
1816        let mut data = SmallVec::new();
1817        for _ in 0..elts {
1818            data.push(Pack::decode(buf)?);
1819        }
1820        Ok(data)
1821    }
1822}
1823
1824use enumflags2::{BitFlag, BitFlags, _internal::RawBitFlags};
1825
1826impl<T> Pack for BitFlags<T>
1827where
1828    T: BitFlag,
1829    <T as RawBitFlags>::Numeric: Pack,
1830{
1831    fn encoded_len(&self) -> usize {
1832        <<T as RawBitFlags>::Numeric as Pack>::encoded_len(&self.bits())
1833    }
1834
1835    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
1836        <<T as RawBitFlags>::Numeric as Pack>::encode(&self.bits(), buf)
1837    }
1838
1839    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
1840        let bits = <<T as RawBitFlags>::Numeric as Pack>::decode(buf)?;
1841        Self::from_bits(bits).map_err(|_| PackError::InvalidFormat)
1842    }
1843}
1844
1845use std::ops::Bound;
1846
1847impl<T: Pack> Pack for Bound<T> {
1848    fn encoded_len(&self) -> usize {
1849        1 + match self {
1850            Bound::Excluded(t) | Bound::Included(t) => Pack::encoded_len(t),
1851            Bound::Unbounded => 0,
1852        }
1853    }
1854
1855    fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
1856        match self {
1857            Bound::Unbounded => <u8 as Pack>::encode(&0, buf),
1858            Bound::Excluded(t) => {
1859                <u8 as Pack>::encode(&1, buf)?;
1860                Pack::encode(t, buf)
1861            }
1862            Bound::Included(t) => {
1863                <u8 as Pack>::encode(&2, buf)?;
1864                Pack::encode(t, buf)
1865            }
1866        }
1867    }
1868
1869    fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
1870        match <u8 as Pack>::decode(buf)? {
1871            0 => Ok(Bound::Unbounded),
1872            1 => Ok(Bound::Excluded(Pack::decode(buf)?)),
1873            2 => Ok(Bound::Included(Pack::decode(buf)?)),
1874            _ => Err(PackError::UnknownTag),
1875        }
1876    }
1877}