Skip to main content

musli_core/internal/
macros.rs

1macro_rules! __slice_sequence {
2    (
3        $(#[$($meta:meta)*])*
4        $cx:ident,
5        $ty:ident <T $(, $alloc:ident)?>,
6        || $new:expr,
7        |$vec:ident, $value:ident| $insert:expr,
8        |$reserve_vec:ident, $reserve_capacity:ident| $reserve:expr,
9        |$capacity:ident| $with_capacity:expr,
10    ) => {
11        $(#[$($meta)*])*
12        impl<M, T $(, $alloc)*> $crate::en::Encode<M> for $ty<T $(, $alloc)*>
13        where
14            T: $crate::en::Encode<M>,
15            $($alloc: Allocator,)*
16        {
17            const IS_BITWISE_ENCODE: bool = false;
18
19            type Encode = Self;
20
21            #[inline]
22            fn encode<E>(&self, encoder: E) -> Result<(), E::Error>
23            where
24                E: $crate::en::Encoder<Mode = M>,
25            {
26                encoder.encode_slice(self)
27            }
28
29            #[inline]
30            fn as_encode(&self) -> &Self::Encode {
31                self
32            }
33        }
34
35        $(#[$($meta)*])*
36        impl<'de, M, A, T $(, $alloc)*> $crate::de::Decode<'de, M, A> for $ty<T $(, $alloc)*>
37        where
38            A: $crate::alloc::Allocator,
39            T: $crate::de::Decode<'de, M, A>,
40            $($alloc: $crate::alloc::Allocator + $crate::alloc::FromAllocator<A>,)*
41        {
42            const IS_BITWISE_DECODE: bool = false;
43
44            #[inline]
45            fn decode<D>(decoder: D) -> Result<Self, D::Error>
46            where
47                D: Decoder<'de, Mode = M, Allocator = A>,
48            {
49                struct Builder<'de, M, A, T $(, $alloc)*>
50                where
51                    $($alloc: $crate::alloc::Allocator,)*
52                {
53                    vec: $ty<T $(, $alloc)*>,
54                    _marker: core::marker::PhantomData<(M, A, &'de ())>
55                }
56
57                #[allow(unused_variables)]
58                impl<'de, M, A, T $(, $alloc)*> $crate::de::DecodeSliceBuilder<T, A> for Builder<'de, M, A, T $(, $alloc)*>
59                where
60                    T: $crate::de::Decode<'de, M, A>,
61                    A: $crate::alloc::Allocator,
62                    $($alloc: $crate::alloc::Allocator + $crate::alloc::FromAllocator<A>,)*
63                {
64                    #[inline]
65                    fn new<C>($cx: C) -> Result<Self, C::Error>
66                    where
67                        C: $crate::Context<Allocator = A>,
68                    {
69                        Ok(Builder {
70                            vec: $new,
71                            _marker: core::marker::PhantomData
72                        })
73                    }
74
75                    #[inline]
76                    fn with_capacity<C>($cx: C, $capacity: usize) -> Result<Self, C::Error>
77                    where
78                        C: $crate::Context<Allocator = A>,
79                    {
80                        Ok(Builder {
81                            vec: $with_capacity,
82                            _marker: core::marker::PhantomData
83                        })
84                    }
85
86                    #[inline]
87                    fn push<C>(&mut self, $cx: C, $value: T) -> Result<(), C::Error>
88                    where
89                        C: $crate::Context<Allocator = A>,
90                    {
91                        let $vec = &mut self.vec;
92                        $insert;
93                        Ok(())
94                    }
95
96                    #[inline]
97                    fn reserve<C>( &mut self, $cx: C, $reserve_capacity: usize) -> Result<(), C::Error>
98                    where
99                        C: $crate::Context<Allocator = A>,
100                    {
101                        let $reserve_vec = &mut self.vec;
102                        $reserve;
103                        Ok(())
104                    }
105
106                    #[inline]
107                    unsafe fn set_len(&mut self, len: usize) {
108                        unsafe {
109                            self.vec.set_len(len);
110                        }
111                    }
112
113                    #[inline]
114                    fn as_mut_ptr(&mut self) -> *mut T {
115                        self.vec.as_mut_ptr()
116                    }
117                }
118
119                let Builder { vec, _marker: core::marker::PhantomData } = decoder.decode_slice()?;
120                Ok(vec)
121            }
122        }
123
124        $(#[$($meta)*])*
125        impl<M, T $(, $alloc)*> $crate::en::EncodePacked<M> for $ty<T $(, $alloc)*>
126        where
127            T: $crate::en::Encode<M>,
128            $($alloc: $crate::alloc::Allocator,)*
129        {
130            #[inline]
131            fn encode_packed<E>(&self, encoder: E) -> Result<(), E::Error>
132            where
133                E: $crate::en::Encoder<Mode = M>,
134            {
135                encoder.encode_pack_fn(|pack| {
136                    $crate::en::SequenceEncoder::encode_slice(pack, self)
137                })
138            }
139        }
140    }
141}
142
143pub(crate) use __slice_sequence as slice_sequence;