foundationdb_tuple/
pack.rs

1use super::*;
2use memchr::memchr_iter;
3use std::convert::TryFrom;
4use std::io;
5use std::mem;
6
7#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
8pub enum VersionstampOffset {
9    None { size: u32 },
10    OneIncomplete { offset: u32 },
11    MultipleIncomplete,
12}
13impl VersionstampOffset {
14    pub(crate) fn append_key_offset(self, output: &mut Vec<u8>, api_version: i32) {
15        match self {
16            Self::OneIncomplete { offset } if api_version < 520 => {
17                let offset = u16::try_from(offset)
18                    .expect("versionstamp key offset does not fit in u16 before API 520");
19                output.extend_from_slice(&offset.to_le_bytes());
20            }
21            Self::OneIncomplete { offset } => output.extend_from_slice(&offset.to_le_bytes()),
22            Self::MultipleIncomplete => {
23                panic!("versionstamp key cannot contain multiple incomplete versionstamps");
24            }
25            Self::None { .. } => {}
26        }
27    }
28}
29
30impl std::ops::AddAssign<u32> for VersionstampOffset {
31    fn add_assign(&mut self, r: u32) {
32        if let VersionstampOffset::None { size } = self {
33            *size += r;
34        }
35    }
36}
37impl std::ops::AddAssign for VersionstampOffset {
38    fn add_assign(&mut self, rhs: Self) {
39        match (&mut *self, rhs) {
40            (VersionstampOffset::None { size }, VersionstampOffset::None { size: r }) => {
41                *size += r;
42            }
43            (VersionstampOffset::None { size }, VersionstampOffset::OneIncomplete { offset }) => {
44                *self = VersionstampOffset::OneIncomplete {
45                    offset: *size + offset,
46                };
47            }
48            (
49                VersionstampOffset::OneIncomplete { .. },
50                VersionstampOffset::OneIncomplete { .. },
51            )
52            | (VersionstampOffset::None { .. }, VersionstampOffset::MultipleIncomplete)
53            | (VersionstampOffset::OneIncomplete { .. }, VersionstampOffset::MultipleIncomplete) => {
54                *self = VersionstampOffset::MultipleIncomplete;
55            }
56            _ => {}
57        }
58    }
59}
60
61const PACK_ERR_MSG: &str = "pack io error on Vec, data size didn't fit in `u32`?";
62
63/// A type that can be packed
64pub trait TuplePack {
65    fn pack<W: io::Write>(
66        &self,
67        w: &mut W,
68        tuple_depth: TupleDepth,
69    ) -> io::Result<VersionstampOffset>;
70
71    fn pack_root<W: io::Write>(&self, w: &mut W) -> io::Result<VersionstampOffset> {
72        self.pack(w, TupleDepth::new())
73    }
74
75    /// Pack value and returns the packed buffer
76    ///
77    /// # Panics
78    ///
79    /// Panics if the encoded data size doesn't fit in `u32`.
80    fn pack_to_vec(&self) -> Vec<u8> {
81        let mut vec = Vec::new();
82        self.pack_into_vec(&mut vec);
83        vec
84    }
85
86    /// Pack a value with a four-byte versionstamp offset for API 520 and later.
87    /// For keys under an earlier runtime API, use [`pack_with_versionstamp_for_key`].
88    ///
89    /// # Panics
90    ///
91    /// Panics if there is multiple versionstamp present or if the encoded data size doesn't fit in `u32`.
92    fn pack_to_vec_with_versionstamp(&self) -> Vec<u8> {
93        let mut vec = Vec::new();
94        let offset = self.pack_into_vec_with_versionstamp(&mut vec);
95        if let VersionstampOffset::MultipleIncomplete = offset {
96            panic!("pack_to_vec_with_versionstamp does not allow multiple versionstamps");
97        }
98        vec
99    }
100
101    /// Pack value into the given buffer
102    ///
103    /// # Panics
104    ///
105    /// Panics if the encoded data size doesn't fit in `u32`.
106    fn pack_into_vec(&self, output: &mut Vec<u8>) -> VersionstampOffset {
107        self.pack_root(output).expect(PACK_ERR_MSG)
108    }
109
110    /// Pack a value into the buffer with a four-byte versionstamp offset for API
111    /// 520 and later. For legacy keys, use [`pack_with_versionstamp_for_key`] or
112    /// [`Subspace::pack_with_versionstamp_for_key`] with the buffer as a prefix.
113    ///
114    /// # Panics
115    ///
116    /// Panics if the encoded data size doesn't fit in `u32`.
117    fn pack_into_vec_with_versionstamp(&self, output: &mut Vec<u8>) -> VersionstampOffset {
118        let mut offset = VersionstampOffset::None {
119            size: u32::try_from(output.len()).expect(PACK_ERR_MSG),
120        };
121        offset += self.pack_root(output).expect(PACK_ERR_MSG);
122        if let VersionstampOffset::OneIncomplete { offset } = offset {
123            output.extend_from_slice(&offset.to_le_bytes());
124        }
125        offset
126    }
127}
128
129/// A type that can be unpacked
130pub trait TupleUnpack<'de>: Sized {
131    fn unpack(input: &'de [u8], tuple_depth: TupleDepth) -> PackResult<(&'de [u8], Self)>;
132
133    fn unpack_root(input: &'de [u8]) -> PackResult<Self> {
134        let (input, this) = Self::unpack(input, TupleDepth::new())?;
135        if !input.is_empty() {
136            return Err(PackError::TrailingBytes);
137        }
138        Ok(this)
139    }
140}
141
142impl<T> TuplePack for &T
143where
144    T: TuplePack,
145{
146    fn pack<W: io::Write>(
147        &self,
148        w: &mut W,
149        tuple_depth: TupleDepth,
150    ) -> io::Result<VersionstampOffset> {
151        (*self).pack(w, tuple_depth)
152    }
153}
154
155#[inline]
156fn parse_bytes(input: &[u8], num: usize) -> PackResult<(&[u8], &[u8])> {
157    if input.len() < num {
158        Err(PackError::MissingBytes)
159    } else {
160        Ok((&input[num..], &input[..num]))
161    }
162}
163
164#[inline]
165fn parse_byte(input: &[u8]) -> PackResult<(&[u8], u8)> {
166    if input.is_empty() {
167        Err(PackError::MissingBytes)
168    } else {
169        Ok((&input[1..], input[0]))
170    }
171}
172
173fn parse_code(input: &[u8], expected: u8) -> PackResult<&[u8]> {
174    let (input, found) = parse_byte(input)?;
175    if found == expected {
176        Ok(input)
177    } else {
178        Err(PackError::BadCode {
179            found,
180            expected: Some(expected),
181        })
182    }
183}
184
185fn write_bytes<W: io::Write>(w: &mut W, v: &[u8]) -> io::Result<VersionstampOffset> {
186    let mut size =
187        u32::try_from(v.len()).map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?;
188    let mut pos = 0;
189    for idx in memchr_iter(NIL, v) {
190        let next_idx = idx + 1;
191        size += 1;
192        w.write_all(&v[pos..next_idx])?;
193        w.write_all(&[ESCAPE])?;
194        pos = next_idx;
195    }
196    w.write_all(&v[pos..])?;
197    w.write_all(&[NIL])?;
198    size += 2;
199    Ok(VersionstampOffset::None { size })
200}
201
202fn parse_slice(input: &[u8]) -> PackResult<(&[u8], Cow<'_, [u8]>)> {
203    let mut bytes = Vec::new();
204    let mut pos = 0;
205    for idx in memchr_iter(NIL, input) {
206        let next_idx = idx + 1;
207        if input.get(next_idx) == Some(&ESCAPE) {
208            bytes.extend_from_slice(&input[pos..next_idx]);
209            pos = next_idx + 1;
210        } else {
211            let slice = &input[pos..idx];
212            return Ok((
213                &input[next_idx..],
214                (if pos == 0 {
215                    Cow::Borrowed(slice)
216                } else {
217                    bytes.extend_from_slice(slice);
218                    Cow::Owned(bytes)
219                }),
220            ));
221        }
222    }
223    Err(PackError::MissingBytes)
224}
225
226fn parse_string(input: &[u8]) -> PackResult<(&[u8], Cow<'_, str>)> {
227    let (input, slice) = parse_slice(input)?;
228    Ok((
229        input,
230        match slice {
231            Cow::Borrowed(slice) => {
232                Cow::Borrowed(std::str::from_utf8(slice).map_err(|_| PackError::BadStringFormat)?)
233            }
234            Cow::Owned(vec) => {
235                Cow::Owned(String::from_utf8(vec).map_err(|_| PackError::BadStringFormat)?)
236            }
237        },
238    ))
239}
240
241impl TuplePack for () {
242    fn pack<W: io::Write>(
243        &self,
244        w: &mut W,
245        tuple_depth: TupleDepth,
246    ) -> io::Result<VersionstampOffset> {
247        if tuple_depth.depth() > 0 {
248            w.write_all(&[NESTED, NIL])?;
249            Ok(VersionstampOffset::None { size: 2 })
250        } else {
251            Ok(VersionstampOffset::None { size: 0 })
252        }
253    }
254}
255
256impl TupleUnpack<'_> for () {
257    fn unpack(mut input: &[u8], tuple_depth: TupleDepth) -> PackResult<(&[u8], Self)> {
258        if tuple_depth.depth() > 0 {
259            input = parse_code(input, NESTED)?;
260            input = parse_code(input, NIL)?;
261        }
262        Ok((input, ()))
263    }
264}
265
266macro_rules! tuple_impls {
267    ($(($($n:tt $name:ident $v:ident)+))+) => {
268        $(
269            impl<$($name),+> TuplePack for ($($name,)+)
270            where
271                $($name: TuplePack,)+
272            {
273                fn pack<W: io::Write>(&self, w: &mut W, tuple_depth: TupleDepth) -> io::Result<VersionstampOffset> {
274                    let mut offset = VersionstampOffset::None { size: 0 };
275                    if tuple_depth.depth() > 0 {
276                        w.write_all(&[NESTED])?;
277                        offset += 1;
278                    }
279
280                    $(
281                        offset += self.$n.pack(w, tuple_depth.increment())?;
282                    )*
283
284                    if tuple_depth.depth() > 0 {
285                        w.write_all(&[NIL])?;
286                        offset += 1;
287                    }
288                    Ok(offset)
289                }
290            }
291
292            impl<'de, $($name),+> TupleUnpack<'de> for ($($name,)+)
293            where
294                $($name: TupleUnpack<'de>,)+
295            {
296                fn unpack(input: &'de [u8], tuple_depth: TupleDepth) -> PackResult<(&'de [u8], Self)> {
297                    let input = if tuple_depth.depth() > 0 { parse_code(input, NESTED)? } else { input };
298
299                    $(
300                        let (input, $v) = $name::unpack(input, tuple_depth.increment())?;
301                    )*
302
303                    let input = if tuple_depth.depth() > 0 { parse_code(input, NIL)? } else { input };
304
305                    let tuple = ( $($v,)* );
306                    Ok((input, tuple))
307                }
308            }
309        )+
310    }
311}
312
313tuple_impls! {
314    (0 T0 t0)
315    (0 T0 t0 1 T1 t1)
316    (0 T0 t0 1 T1 t1 2 T2 t2)
317    (0 T0 t0 1 T1 t1 2 T2 t2 3 T3 t3)
318    (0 T0 t0 1 T1 t1 2 T2 t2 3 T3 t3 4 T4 t4)
319    (0 T0 t0 1 T1 t1 2 T2 t2 3 T3 t3 4 T4 t4 5 T5 t5)
320    (0 T0 t0 1 T1 t1 2 T2 t2 3 T3 t3 4 T4 t4 5 T5 t5 6 T6 t6)
321    (0 T0 t0 1 T1 t1 2 T2 t2 3 T3 t3 4 T4 t4 5 T5 t5 6 T6 t6 7 T7 t7)
322    (0 T0 t0 1 T1 t1 2 T2 t2 3 T3 t3 4 T4 t4 5 T5 t5 6 T6 t6 7 T7 t7 8 T8 t8)
323    (0 T0 t0 1 T1 t1 2 T2 t2 3 T3 t3 4 T4 t4 5 T5 t5 6 T6 t6 7 T7 t7 8 T8 t8 9 T9 t9)
324    (0 T0 t0 1 T1 t1 2 T2 t2 3 T3 t3 4 T4 t4 5 T5 t5 6 T6 t6 7 T7 t7 8 T8 t8 9 T9 t9 10 T10 t10)
325    (0 T0 t0 1 T1 t1 2 T2 t2 3 T3 t3 4 T4 t4 5 T5 t5 6 T6 t6 7 T7 t7 8 T8 t8 9 T9 t9 10 T10 t10 11 T11 t11)
326}
327
328const MAX_SZ: usize = 8;
329
330macro_rules! sign_bit {
331    ($type:ident) => {
332        (1 << (mem::size_of::<$type>() * 8 - 1))
333    };
334}
335
336macro_rules! unpack_ux {
337    ($ux: ident, $input: expr_2021, $n: expr_2021) => {{
338        let (input, bytes) = parse_bytes($input, $n)?;
339        let mut arr = [0u8; ::std::mem::size_of::<$ux>()];
340        (&mut arr[(::std::mem::size_of::<$ux>() - $n)..]).copy_from_slice(bytes);
341        (input, $ux::from_be_bytes(arr))
342    }};
343}
344
345macro_rules! unpack_px {
346    ($ix: ident, $ux: ident, $input: expr_2021, $n: expr_2021) => {{
347        let (input, bytes) = parse_bytes($input, $n)?;
348        let mut arr = [0u8; ::std::mem::size_of::<$ux>()];
349        (&mut arr[(::std::mem::size_of::<$ux>() - $n)..]).copy_from_slice(bytes);
350        let x = $ix::from_be_bytes(arr);
351        if x < 0 {
352            Err(PackError::UnsupportedIntLength)
353        } else {
354            Ok((input, x))
355        }
356    }};
357}
358macro_rules! unpack_nx {
359    ($ix: ident, $ux: ident, $input: expr_2021, $n: expr_2021) => {{
360        let (input, bytes) = parse_bytes($input, $n)?;
361        let mut arr = [0xffu8; ::std::mem::size_of::<$ix>()];
362        (&mut arr[(::std::mem::size_of::<$ix>() - $n)..]).copy_from_slice(bytes);
363        let x = $ix::from_be_bytes(arr).wrapping_add(1);
364        if x > 0 {
365            Err(PackError::UnsupportedIntLength)
366        } else {
367            Ok((input, x))
368        }
369    }};
370}
371
372macro_rules! impl_ux {
373    ($ux: ident) => {
374        impl_ux!($ux, mem::size_of::<$ux>());
375    };
376    ($ux: ident, $max_sz:expr_2021) => {
377        impl TuplePack for $ux {
378            fn pack<W: io::Write>(
379                &self,
380                w: &mut W,
381                _tuple_depth: TupleDepth,
382            ) -> io::Result<VersionstampOffset> {
383                const SZ: usize = mem::size_of::<$ux>();
384                let u = *self;
385                let n = SZ - (u.leading_zeros() as usize) / 8;
386                let mut offset = VersionstampOffset::None { size: n as u32 + 1 };
387                if SZ <= MAX_SZ || n <= MAX_SZ {
388                    w.write_all(&[INTZERO + n as u8])?;
389                } else {
390                    w.write_all(&[POSINTEND, n as u8])?;
391                    offset += 1;
392                };
393                w.write_all(&u.to_be_bytes()[SZ - n..])?;
394
395                Ok(offset)
396            }
397        }
398
399        impl<'de> TupleUnpack<'de> for $ux {
400            fn unpack(input: &[u8], _tuple_depth: TupleDepth) -> PackResult<(&[u8], Self)> {
401                const SZ: usize = mem::size_of::<$ux>();
402                let (input, found) = parse_byte(input)?;
403                if INTZERO <= found && found <= INTZERO + $max_sz as u8 {
404                    let n = (found - INTZERO) as usize;
405                    Ok(unpack_ux!($ux, input, n))
406                } else if found == POSINTEND {
407                    let (input, raw_length) = parse_byte(input)?;
408                    let n: usize = usize::from(raw_length);
409                    if n > SZ {
410                        return Err(PackError::UnsupportedIntLength);
411                    }
412                    Ok(unpack_ux!($ux, input, n))
413                } else {
414                    Err(PackError::BadCode {
415                        found,
416                        expected: None,
417                    })
418                }
419            }
420        }
421    };
422}
423
424macro_rules! impl_ix {
425    ($ix: ident, $ux: ident) => {
426        impl_ix!($ix, $ux, mem::size_of::<$ix>());
427    };
428    ($ix: ident, $ux: ident, $max_sz:expr_2021) => {
429        impl TuplePack for $ix {
430            fn pack<W: io::Write>(
431                &self,
432                w: &mut W,
433                _tuple_depth: TupleDepth,
434            ) -> io::Result<VersionstampOffset> {
435                const SZ: usize = mem::size_of::<$ix>();
436                let i = *self;
437                let u = self.wrapping_abs() as $ux;
438                let n = SZ - (u.leading_zeros() as usize) / 8;
439                let mut offset = VersionstampOffset::None { size: n as u32 + 1 };
440                let arr = if i >= 0 {
441                    if SZ <= MAX_SZ || n <= MAX_SZ {
442                        w.write_all(&[INTZERO + n as u8])?;
443                    } else {
444                        w.write_all(&[POSINTEND, n as u8])?;
445                        offset += 1;
446                    }
447                    (u.to_be_bytes())
448                } else {
449                    if SZ <= MAX_SZ || n <= MAX_SZ {
450                        w.write_all(&[INTZERO - n as u8])?;
451                    } else {
452                        w.write_all(&[NEGINTSTART, n as u8 ^ 0xff])?;
453                        offset += 1;
454                    }
455                    (i.wrapping_sub(1).to_be_bytes())
456                };
457                w.write_all(&arr[SZ - n..])?;
458
459                Ok(offset)
460            }
461        }
462
463        impl<'de> TupleUnpack<'de> for $ix {
464            fn unpack(input: &[u8], _tuple_depth: TupleDepth) -> PackResult<(&[u8], Self)> {
465                const SZ: usize = mem::size_of::<$ix>();
466                let (input, found) = parse_byte(input)?;
467                if INTZERO <= found && found <= INTZERO + $max_sz as u8 {
468                    let n = (found - INTZERO) as usize;
469                    unpack_px!($ix, $ux, input, n)
470                } else if INTZERO - $max_sz as u8 <= found && found < INTZERO {
471                    let n = (INTZERO - found) as usize;
472                    unpack_nx!($ix, $ux, input, n)
473                } else if found == NEGINTSTART {
474                    let (input, raw_length) = parse_byte(input)?;
475                    let n = usize::from(raw_length ^ 0xff);
476                    if n > SZ {
477                        return Err(PackError::UnsupportedIntLength);
478                    }
479                    unpack_nx!($ix, $ux, input, n)
480                } else if found == POSINTEND {
481                    let (input, raw_length) = parse_byte(input)?;
482                    let n: usize = usize::from(raw_length);
483                    if n > SZ {
484                        return Err(PackError::UnsupportedIntLength);
485                    }
486                    unpack_px!($ix, $ux, input, n)
487                } else {
488                    Err(PackError::BadCode {
489                        found,
490                        expected: None,
491                    })
492                }
493            }
494        }
495    };
496}
497
498macro_rules! impl_fx {
499    ( $fx: ident, $fx_to_ux_be_bytes: ident, $ux_width: tt, $parse_ux: ident, $ux: ident, $code: ident) => {
500        #[inline]
501        pub(super) fn $fx_to_ux_be_bytes(f: $fx) -> [u8; $ux_width] {
502            let u = if f.is_sign_negative() {
503                f.to_bits() ^ $ux::MAX
504            } else {
505                f.to_bits() ^ sign_bit!($ux)
506            };
507            u.to_be_bytes()
508        }
509        impl TuplePack for $fx {
510            fn pack<W: io::Write>(
511                &self,
512                w: &mut W,
513                _tuple_depth: TupleDepth,
514            ) -> io::Result<VersionstampOffset> {
515                let bytes = $fx_to_ux_be_bytes(*self);
516                w.write_all(&[$code])?;
517                w.write_all(&bytes)?;
518                Ok(VersionstampOffset::None {
519                    size: std::mem::size_of::<$fx>() as u32 + 1,
520                })
521            }
522        }
523
524        fn $parse_ux(input: &[u8]) -> PackResult<(&[u8], $ux)> {
525            let (input, bytes) = parse_bytes(input, mem::size_of::<$ux>())?;
526            let mut arr = [0u8; mem::size_of::<$ux>()];
527            arr.copy_from_slice(bytes);
528            Ok((input, $ux::from_be_bytes(arr)))
529        }
530        impl<'de> TupleUnpack<'de> for $fx {
531            fn unpack(input: &[u8], _tuple_depth: TupleDepth) -> PackResult<(&[u8], Self)> {
532                let input = parse_code(input, $code)?;
533                let (input, u) = $parse_ux(input)?;
534                Ok((
535                    input,
536                    $fx::from_bits(if (u & sign_bit!($ux)) == 0 {
537                        u ^ $ux::MAX
538                    } else {
539                        u ^ sign_bit!($ux)
540                    }),
541                ))
542            }
543        }
544    };
545}
546
547//impl_ux!(u8);
548impl_ux!(u16);
549impl_ux!(u32);
550impl_ux!(u64);
551impl_ux!(u128, MAX_SZ);
552impl_ux!(usize);
553
554//impl_ix!(i8, u8);
555impl_ix!(i16, u16);
556impl_ix!(i32, u32);
557impl_ix!(i64, u64);
558impl_ix!(i128, u128, MAX_SZ);
559impl_ix!(isize, usize);
560
561impl_fx!(f32, f32_to_u32_be_bytes, 4, parse_u32, u32, FLOAT);
562impl_fx!(f64, f64_to_u64_be_bytes, 8, parse_u64, u64, DOUBLE);
563
564#[cfg(feature = "num-bigint")]
565mod bigint {
566    use super::*;
567    use num_bigint::{BigInt, BigUint, Sign};
568    use std::convert::TryFrom;
569
570    fn invert(bytes: &mut [u8]) {
571        // The ones' complement of a binary number is defined as the value
572        // obtained by inverting all the bits in the binary representation
573        // of the number (swapping 0s for 1s and vice versa).
574        for byte in bytes.iter_mut() {
575            *byte = !*byte;
576        }
577    }
578
579    fn inverted(bytes: &[u8]) -> Vec<u8> {
580        // The ones' complement of a binary number is defined as the value
581        // obtained by inverting all the bits in the binary representation
582        // of the number (swapping 0s for 1s and vice versa).
583        bytes.iter().map(|byte| !*byte).collect()
584    }
585
586    fn bigint_n(n: usize) -> io::Result<u8> {
587        u8::try_from(n).map_err(|_| {
588            std::io::Error::new(
589                std::io::ErrorKind::InvalidInput,
590                "BigUint requires more than 255 bytes to be represented",
591            )
592        })
593    }
594
595    impl TuplePack for BigInt {
596        fn pack<W: io::Write>(
597            &self,
598            w: &mut W,
599            _tuple_depth: TupleDepth,
600        ) -> io::Result<VersionstampOffset> {
601            if self.sign() == Sign::NoSign {
602                w.write_all(&[INTZERO])?;
603                return Ok(VersionstampOffset::None { size: 1 });
604            }
605            let (sign, mut bytes) = self.to_bytes_be();
606            let n = bytes.len();
607            let mut offset = VersionstampOffset::None { size: n as u32 + 1 };
608            match sign {
609                Sign::Minus => {
610                    if n <= MAX_SZ {
611                        w.write_all(&[INTZERO - n as u8])?;
612                    } else {
613                        w.write_all(&[NEGINTSTART, bigint_n(n)? ^ 0xff])?;
614                        offset += 1;
615                    }
616                    invert(&mut bytes);
617                    w.write_all(&bytes)?;
618                }
619                Sign::NoSign => unreachable!(),
620                Sign::Plus => {
621                    if n <= MAX_SZ {
622                        w.write_all(&[INTZERO + n as u8])?;
623                    } else {
624                        w.write_all(&[POSINTEND, bigint_n(n)?])?;
625                        offset += 1;
626                    }
627                    w.write_all(&bytes)?;
628                }
629            }
630
631            Ok(offset)
632        }
633    }
634
635    impl TupleUnpack<'_> for BigInt {
636        fn unpack(input: &[u8], _tuple_depth: TupleDepth) -> PackResult<(&[u8], Self)> {
637            let (input, found) = parse_byte(input)?;
638            if INTZERO <= found && found <= INTZERO + MAX_SZ as u8 {
639                let n = (found - INTZERO) as usize;
640                let (input, bytes) = parse_bytes(input, n)?;
641                Ok((input, Self::from_bytes_be(Sign::Plus, bytes)))
642            } else if INTZERO - MAX_SZ as u8 <= found && found < INTZERO {
643                let n = (INTZERO - found) as usize;
644                let (input, bytes) = parse_bytes(input, n)?;
645                Ok((input, Self::from_bytes_be(Sign::Minus, &inverted(bytes))))
646            } else if found == NEGINTSTART {
647                let (input, raw_length) = parse_byte(input)?;
648                let n = usize::from(raw_length ^ 0xff);
649                let (input, bytes) = parse_bytes(input, n)?;
650                Ok((input, Self::from_bytes_be(Sign::Minus, &inverted(bytes))))
651            } else if found == POSINTEND {
652                let (input, raw_length) = parse_byte(input)?;
653                let n: usize = usize::from(raw_length);
654                let (input, bytes) = parse_bytes(input, n)?;
655                Ok((input, Self::from_bytes_be(Sign::Plus, bytes)))
656            } else {
657                Err(PackError::BadCode {
658                    found,
659                    expected: None,
660                })
661            }
662        }
663    }
664
665    impl TuplePack for BigUint {
666        fn pack<W: io::Write>(
667            &self,
668            w: &mut W,
669            _tuple_depth: TupleDepth,
670        ) -> io::Result<VersionstampOffset> {
671            let n = self.bits();
672            if n == 0 {
673                w.write_all(&[INTZERO])?;
674                return Ok(VersionstampOffset::None { size: 1 });
675            }
676            let bytes = self.to_bytes_be();
677            let n = bytes.len();
678            let mut offset = VersionstampOffset::None { size: n as u32 + 1 };
679            if n <= MAX_SZ {
680                w.write_all(&[INTZERO + n as u8])?;
681            } else {
682                w.write_all(&[POSINTEND, bigint_n(n)?])?;
683                offset += 1;
684            }
685            w.write_all(&bytes)?;
686
687            Ok(offset)
688        }
689    }
690
691    impl TupleUnpack<'_> for BigUint {
692        fn unpack(input: &[u8], _tuple_depth: TupleDepth) -> PackResult<(&[u8], Self)> {
693            let (input, found) = parse_byte(input)?;
694            if INTZERO <= found && found <= INTZERO + MAX_SZ as u8 {
695                let n = (found - INTZERO) as usize;
696                let (input, bytes) = parse_bytes(input, n)?;
697                Ok((input, Self::from_bytes_be(bytes)))
698            } else if found == POSINTEND {
699                let (input, raw_length) = parse_byte(input)?;
700                let n: usize = usize::from(raw_length);
701                let (input, bytes) = parse_bytes(input, n)?;
702                Ok((input, Self::from_bytes_be(bytes)))
703            } else {
704                Err(PackError::BadCode {
705                    found,
706                    expected: None,
707                })
708            }
709        }
710    }
711}
712
713impl TuplePack for bool {
714    fn pack<W: io::Write>(
715        &self,
716        w: &mut W,
717        _tuple_depth: TupleDepth,
718    ) -> io::Result<VersionstampOffset> {
719        w.write_all(&[if *self { TRUE } else { FALSE }])?;
720        Ok(VersionstampOffset::None { size: 1 })
721    }
722}
723
724impl TupleUnpack<'_> for bool {
725    fn unpack(input: &[u8], _tuple_depth: TupleDepth) -> PackResult<(&[u8], Self)> {
726        let (input, v) = parse_byte(input)?;
727        match v {
728            FALSE => Ok((input, false)),
729            TRUE => Ok((input, true)),
730            _ => Err(PackError::Message(
731                format!("{v} is not a valid bool value").into_boxed_str(),
732            )),
733        }
734    }
735}
736
737impl<T> TuplePack for &[T]
738where
739    T: TuplePack,
740{
741    fn pack<W: io::Write>(
742        &self,
743        w: &mut W,
744        tuple_depth: TupleDepth,
745    ) -> io::Result<VersionstampOffset> {
746        let mut offset = VersionstampOffset::None { size: 0 };
747
748        if tuple_depth.depth() > 0 {
749            w.write_all(&[NESTED])?;
750            offset += 1;
751        }
752
753        for v in self.iter() {
754            offset += v.pack(w, tuple_depth.increment())?;
755        }
756
757        if tuple_depth.depth() > 0 {
758            w.write_all(&[NIL])?;
759            offset += 1;
760        }
761        Ok(offset)
762    }
763}
764
765impl<T> TuplePack for Vec<T>
766where
767    T: TuplePack,
768{
769    fn pack<W: io::Write>(
770        &self,
771        w: &mut W,
772        tuple_depth: TupleDepth,
773    ) -> io::Result<VersionstampOffset> {
774        self.as_slice().pack(w, tuple_depth)
775    }
776}
777
778fn is_end_of_tuple(input: &[u8], nested: bool) -> bool {
779    match input.first() {
780        None => true,
781        _ if !nested => false,
782        Some(&NIL) => Some(&ESCAPE) != input.get(1),
783        _ => false,
784    }
785}
786
787impl<'de, T> TupleUnpack<'de> for Vec<T>
788where
789    T: TupleUnpack<'de>,
790{
791    fn unpack(mut input: &'de [u8], tuple_depth: TupleDepth) -> PackResult<(&'de [u8], Self)> {
792        let nested = tuple_depth.depth() > 0;
793        if nested {
794            input = parse_code(input, NESTED)?;
795        }
796
797        let mut vec = Vec::new();
798
799        while !is_end_of_tuple(input, nested) {
800            let (rem, v) = T::unpack(input, tuple_depth.increment())?;
801            input = rem;
802            vec.push(v);
803        }
804
805        if nested {
806            input = parse_code(input, NIL)?;
807        }
808
809        Ok((input, vec))
810    }
811}
812
813impl TuplePack for Bytes<'_> {
814    fn pack<W: io::Write>(
815        &self,
816        w: &mut W,
817        _tuple_depth: TupleDepth,
818    ) -> io::Result<VersionstampOffset> {
819        w.write_all(&[BYTES])?;
820        write_bytes(w, self.as_ref())
821    }
822}
823
824impl<'de> TupleUnpack<'de> for Bytes<'de> {
825    fn unpack(input: &'de [u8], _tuple_depth: TupleDepth) -> PackResult<(&'de [u8], Self)> {
826        let input = parse_code(input, BYTES)?;
827        let (input, v) = parse_slice(input)?;
828        Ok((input, Bytes(v)))
829    }
830}
831
832impl TuplePack for &[u8] {
833    fn pack<W: io::Write>(
834        &self,
835        w: &mut W,
836        tuple_depth: TupleDepth,
837    ) -> io::Result<VersionstampOffset> {
838        Bytes::from(*self).pack(w, tuple_depth)
839    }
840}
841
842impl TuplePack for Vec<u8> {
843    fn pack<W: io::Write>(
844        &self,
845        w: &mut W,
846        tuple_depth: TupleDepth,
847    ) -> io::Result<VersionstampOffset> {
848        Bytes::from(self.as_slice()).pack(w, tuple_depth)
849    }
850}
851
852impl<'de> TupleUnpack<'de> for Vec<u8> {
853    fn unpack(input: &'de [u8], tuple_depth: TupleDepth) -> PackResult<(&'de [u8], Self)> {
854        let (input, bytes) = Bytes::unpack(input, tuple_depth)?;
855        Ok((input, bytes.into_owned()))
856    }
857}
858
859impl TuplePack for &str {
860    fn pack<W: io::Write>(
861        &self,
862        w: &mut W,
863        _tuple_depth: TupleDepth,
864    ) -> io::Result<VersionstampOffset> {
865        w.write_all(&[STRING])?;
866        write_bytes(w, self.as_bytes())
867    }
868}
869
870impl TuplePack for String {
871    fn pack<W: io::Write>(
872        &self,
873        w: &mut W,
874        tuple_depth: TupleDepth,
875    ) -> io::Result<VersionstampOffset> {
876        self.as_str().pack(w, tuple_depth)
877    }
878}
879
880impl TuplePack for Cow<'_, str> {
881    fn pack<W: io::Write>(
882        &self,
883        w: &mut W,
884        tuple_depth: TupleDepth,
885    ) -> io::Result<VersionstampOffset> {
886        self.as_ref().pack(w, tuple_depth)
887    }
888}
889
890impl<'de> TupleUnpack<'de> for Cow<'de, str> {
891    fn unpack(input: &'de [u8], _tuple_depth: TupleDepth) -> PackResult<(&'de [u8], Self)> {
892        let input = parse_code(input, STRING)?;
893        let (input, v) = parse_string(input)?;
894        Ok((input, v))
895    }
896}
897
898impl TupleUnpack<'_> for String {
899    fn unpack(input: &[u8], _tuple_depth: TupleDepth) -> PackResult<(&[u8], Self)> {
900        let input = parse_code(input, STRING)?;
901        let (input, v) = parse_string(input)?;
902        Ok((input, v.into_owned()))
903    }
904}
905
906impl<T> TuplePack for Option<T>
907where
908    T: TuplePack,
909{
910    fn pack<W: io::Write>(
911        &self,
912        w: &mut W,
913        tuple_depth: TupleDepth,
914    ) -> io::Result<VersionstampOffset> {
915        match self {
916            None => {
917                let size = if tuple_depth.depth() > 1 {
918                    // Empty value in nested tuple is encoded with [NIL, ESCAPE] to
919                    // disambiguate itself with end-of-tuple marker.
920                    w.write_all(&[NIL, ESCAPE])?;
921                    2
922                } else {
923                    w.write_all(&[NIL])?;
924                    1
925                };
926                Ok(VersionstampOffset::None { size })
927            }
928            Some(v) => v.pack(w, tuple_depth),
929        }
930    }
931}
932
933impl<'de, T> TupleUnpack<'de> for Option<T>
934where
935    T: TupleUnpack<'de>,
936{
937    fn unpack(mut input: &'de [u8], tuple_depth: TupleDepth) -> PackResult<(&'de [u8], Self)> {
938        if let Some(&NIL) = input.first() {
939            input = parse_code(input, NIL)?;
940            if tuple_depth.depth() > 1 {
941                input = parse_code(input, ESCAPE)?;
942            }
943            Ok((input, None))
944        } else {
945            let (input, v) = T::unpack(input, tuple_depth)?;
946            Ok((input, Some(v)))
947        }
948    }
949}
950
951impl TuplePack for Element<'_> {
952    fn pack<W: io::Write>(
953        &self,
954        w: &mut W,
955        tuple_depth: TupleDepth,
956    ) -> io::Result<VersionstampOffset> {
957        match self {
958            Element::Nil => Option::<()>::None.pack(w, tuple_depth),
959            Element::Bool(b) => b.pack(w, tuple_depth),
960            Element::Int(i) => i.pack(w, tuple_depth),
961            Element::Float(f) => f.pack(w, tuple_depth),
962            Element::Double(f) => f.pack(w, tuple_depth),
963            Element::String(c) => c.pack(w, tuple_depth),
964            Element::Bytes(b) => b.pack(w, tuple_depth),
965            Element::Versionstamp(b) => b.pack(w, tuple_depth),
966            Element::Tuple(v) => v.pack(w, tuple_depth),
967            #[cfg(feature = "uuid")]
968            Element::Uuid(v) => v.pack(w, tuple_depth),
969            #[cfg(feature = "num-bigint")]
970            Element::BigInt(v) => v.pack(w, tuple_depth),
971        }
972    }
973}
974
975impl<'de> TupleUnpack<'de> for Element<'de> {
976    fn unpack(input: &'de [u8], tuple_depth: TupleDepth) -> PackResult<(&'de [u8], Self)> {
977        const INTMIN: u8 = INTZERO - 8;
978        const INTMAX: u8 = INTZERO + 8;
979
980        let first = match input.first() {
981            None if tuple_depth.depth() == 0 => return Ok((input, Element::Tuple(Vec::new()))),
982            None => return Err(PackError::MissingBytes),
983            Some(byte) => byte,
984        };
985
986        let (mut input, mut v) = match *first {
987            NIL => {
988                let (input, _) = Option::<()>::unpack(input, tuple_depth)?;
989                (input, Element::Nil)
990            }
991            BYTES => {
992                let (input, v) = Bytes::unpack(input, tuple_depth)?;
993                (input, Element::Bytes(v))
994            }
995            STRING => {
996                let (input, v) = Cow::<'de, str>::unpack(input, tuple_depth)?;
997                (input, Element::String(v))
998            }
999            NESTED => {
1000                let (input, v) = Vec::<Self>::unpack(input, tuple_depth)?;
1001                (input, Element::Tuple(v))
1002            }
1003            INTMIN..=INTMAX => match i64::unpack(input, tuple_depth) {
1004                Ok((input, v)) => (input, Element::Int(v)),
1005                #[cfg(feature = "num-bigint")]
1006                Err(PackError::UnsupportedIntLength) => {
1007                    let (input, v) = num_bigint::BigInt::unpack(input, tuple_depth)?;
1008                    (input, Element::BigInt(v))
1009                }
1010                Err(err) => return Err(err),
1011            },
1012            #[cfg(feature = "num-bigint")]
1013            NEGINTSTART => {
1014                let (input, v) = num_bigint::BigInt::unpack(input, tuple_depth)?;
1015                (input, Element::BigInt(v))
1016            }
1017            #[cfg(feature = "num-bigint")]
1018            POSINTEND => {
1019                let (input, v) = num_bigint::BigInt::unpack(input, tuple_depth)?;
1020                (input, Element::BigInt(v))
1021            }
1022            #[cfg(not(feature = "num-bigint"))]
1023            NEGINTSTART => {
1024                let (input, v) = i64::unpack(input, tuple_depth)?;
1025                (input, Element::Int(v))
1026            }
1027            #[cfg(not(feature = "num-bigint"))]
1028            POSINTEND => {
1029                let (input, v) = i64::unpack(input, tuple_depth)?;
1030                (input, Element::Int(v))
1031            }
1032            FLOAT => {
1033                let (input, v) = f32::unpack(input, tuple_depth)?;
1034                (input, Element::Float(v))
1035            }
1036            DOUBLE => {
1037                let (input, v) = f64::unpack(input, tuple_depth)?;
1038                (input, Element::Double(v))
1039            }
1040            FALSE | TRUE => {
1041                let (input, v) = bool::unpack(input, tuple_depth)?;
1042                (input, Element::Bool(v))
1043            }
1044            VERSIONSTAMP => {
1045                let (input, v) = Versionstamp::unpack(input, tuple_depth)?;
1046                (input, Element::Versionstamp(v))
1047            }
1048            #[cfg(feature = "uuid")]
1049            UUID => {
1050                let (input, v) = uuid::Uuid::unpack(input, tuple_depth)?;
1051                (input, Element::Uuid(v))
1052            }
1053            found => {
1054                return Err(PackError::BadCode {
1055                    found,
1056                    expected: None,
1057                });
1058            }
1059        };
1060
1061        if tuple_depth.depth() == 0 && !input.is_empty() {
1062            let mut tuple = vec![v];
1063            while !input.is_empty() {
1064                let (rem, v) = Self::unpack(input, tuple_depth.increment())?;
1065                tuple.push(v);
1066                input = rem;
1067            }
1068            v = Element::Tuple(tuple);
1069        }
1070
1071        Ok((input, v))
1072    }
1073}
1074
1075impl TuplePack for Versionstamp {
1076    fn pack<W: io::Write>(
1077        &self,
1078        w: &mut W,
1079        _tuple_depth: TupleDepth,
1080    ) -> io::Result<VersionstampOffset> {
1081        w.write_all(&[VERSIONSTAMP])?;
1082        w.write_all(self.as_bytes())?;
1083        if self.is_complete() {
1084            Ok(VersionstampOffset::None { size: 1 + 12 })
1085        } else {
1086            Ok(VersionstampOffset::OneIncomplete { offset: 1 })
1087        }
1088    }
1089}
1090
1091impl TupleUnpack<'_> for Versionstamp {
1092    fn unpack(input: &[u8], _tuple_depth: TupleDepth) -> PackResult<(&[u8], Self)> {
1093        let input = parse_code(input, VERSIONSTAMP)?;
1094        let (input, slice) = parse_bytes(input, 12)?;
1095        let mut bytes = [0xff; 12];
1096        bytes.copy_from_slice(slice);
1097        Ok((input, Versionstamp::from(bytes)))
1098    }
1099}
1100
1101#[cfg(feature = "uuid")]
1102mod pack_uuid {
1103    use super::*;
1104    use uuid::Uuid;
1105
1106    impl TuplePack for Uuid {
1107        fn pack<W: io::Write>(
1108            &self,
1109            w: &mut W,
1110            _tuple_depth: TupleDepth,
1111        ) -> io::Result<VersionstampOffset> {
1112            w.write_all(&[UUID])?;
1113            w.write_all(self.as_bytes())?;
1114            Ok(VersionstampOffset::None { size: 1 + 16 })
1115        }
1116    }
1117
1118    impl TupleUnpack<'_> for Uuid {
1119        fn unpack(input: &[u8], _tuple_depth: TupleDepth) -> PackResult<(&[u8], Self)> {
1120            let input = parse_code(input, UUID)?;
1121            let (input, slice) = parse_bytes(input, 16)?;
1122            let uuid = Self::from_slice(slice).map_err(|_| PackError::BadUuid)?;
1123            Ok((input, uuid))
1124        }
1125    }
1126}