netidx_core/
pack.rs

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