tlbits/as/
same.rs

1use crate::{
2    de::{
3        BitReader, BitUnpack,
4        args::{BitUnpackWithArgs, r#as::BitUnpackAsWithArgs},
5        r#as::BitUnpackAs,
6    },
7    ser::{
8        BitPack, BitWriter,
9        args::{BitPackWithArgs, r#as::BitPackAsWithArgs},
10        r#as::BitPackAs,
11    },
12};
13
14/// Adapter to convert from `*As` to regular **de**/**ser**ialization traits.
15#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
16pub struct Same;
17
18impl<T> BitPackAs<T> for Same
19where
20    T: BitPack,
21{
22    #[inline]
23    fn pack_as<W>(source: &T, writer: W) -> Result<(), W::Error>
24    where
25        W: BitWriter,
26    {
27        source.pack(writer)
28    }
29}
30
31impl<T> BitPackAsWithArgs<T> for Same
32where
33    T: BitPackWithArgs,
34{
35    type Args = T::Args;
36
37    #[inline]
38    fn pack_as_with<W>(source: &T, writer: W, args: Self::Args) -> Result<(), W::Error>
39    where
40        W: BitWriter,
41    {
42        T::pack_with(source, writer, args)
43    }
44}
45
46impl<'de, T> BitUnpackAs<'de, T> for Same
47where
48    T: BitUnpack<'de>,
49{
50    #[inline]
51    fn unpack_as<R>(reader: R) -> Result<T, R::Error>
52    where
53        R: BitReader<'de>,
54    {
55        T::unpack(reader)
56    }
57}
58
59impl<'de, T> BitUnpackAsWithArgs<'de, T> for Same
60where
61    T: BitUnpackWithArgs<'de>,
62{
63    type Args = T::Args;
64
65    #[inline]
66    fn unpack_as_with<R>(reader: R, args: Self::Args) -> Result<T, R::Error>
67    where
68        R: BitReader<'de>,
69    {
70        T::unpack_with(reader, args)
71    }
72}