/rust/registry/src/index.crates.io-1949cf8c6b5b557f/minicbor-0.18.0/src/encode.rs
Line | Count | Source |
1 | | //! Traits and types for encoding CBOR. |
2 | | //! |
3 | | //! This module defines the trait [`Encode`] and the actual [`Encoder`]. |
4 | | //! It also defines a [`Write`] trait to store the encoded bytes. |
5 | | |
6 | | mod encoder; |
7 | | mod error; |
8 | | pub mod write; |
9 | | |
10 | | pub use encoder::Encoder; |
11 | | pub use error::Error; |
12 | | pub use write::Write; |
13 | | |
14 | | /// A type that can be encoded to CBOR. |
15 | | /// |
16 | | /// If this type's CBOR encoding is meant to be decoded by `Decode` impls |
17 | | /// derived with [`minicbor_derive`] *it is advisable to only produce a |
18 | | /// single CBOR data item*. Tagging, maps or arrays can and should be used |
19 | | /// for multiple values. |
20 | | pub trait Encode<C> { |
21 | | /// Encode a value of this type using the given `Encoder`. |
22 | | /// |
23 | | /// In addition to the encoder a user provided encoding context is given |
24 | | /// as another parameter. Most implementations of this trait do not need an |
25 | | /// encoding context and should be completely generic in the context |
26 | | /// type. In cases where a context is needed and the `Encode` impl type is |
27 | | /// meant to be combined with other types that require a different context |
28 | | /// type, it is preferrable to constrain the context type variable `C` with |
29 | | /// a trait bound instead of fixing the type. |
30 | | fn encode<W: Write>(&self, e: &mut Encoder<W>, ctx: &mut C) -> Result<(), Error<W::Error>>; |
31 | | |
32 | | /// Is this value of `Self` a nil value? |
33 | | /// |
34 | | /// This method is primarily used by `minicbor-derive`. |
35 | | /// |
36 | | /// Some types have a special value to denote the concept of "nothing", aka |
37 | | /// nil. An example is the `Option` type with its `None` value. This |
38 | | /// method--if overriden--allows checking if a value is such a special nil |
39 | | /// value. |
40 | | /// |
41 | | /// NB: A type implementing `Encode` with an overriden `Encode::is_nil` |
42 | | /// method should also override `Decode::nil` if it implements `Decode` |
43 | | /// at all. |
44 | 0 | fn is_nil(&self) -> bool { |
45 | 0 | false |
46 | 0 | } |
47 | | } |
48 | | |
49 | | impl<C, T: Encode<C> + ?Sized> Encode<C> for &T { |
50 | 0 | fn encode<W: Write>(&self, e: &mut Encoder<W>, ctx: &mut C) -> Result<(), Error<W::Error>> { |
51 | 0 | (**self).encode(e, ctx) |
52 | 0 | } Unexecuted instantiation: <&alloc::vec::Vec<dhall::syntax::ast::label::Label> as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <&&alloc::vec::Vec<dhall::syntax::ast::label::Label> as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <&dhall::syntax::ast::expr::Expr as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <&&dhall::syntax::ast::expr::Expr as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <&(&dhall::syntax::ast::expr::Expr,) as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <&dhall::operations::kind::BinOp as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <&&dhall::operations::kind::BinOp as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <&&i64 as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <&&u64 as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <&i64 as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <&u64 as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <&core::option::Option<()> as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <&alloc::collections::btree::map::BTreeMap<dhall::syntax::ast::label::Label, core::option::Option<dhall::syntax::ast::expr::Expr>> as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <&alloc::collections::btree::map::BTreeMap<dhall::syntax::ast::label::Label, dhall::syntax::ast::expr::Expr> as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <&&alloc::collections::btree::map::BTreeMap<dhall::syntax::ast::label::Label, core::option::Option<dhall::syntax::ast::expr::Expr>> as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <&&alloc::collections::btree::map::BTreeMap<dhall::syntax::ast::label::Label, dhall::syntax::ast::expr::Expr> as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <&dhall::syntax::ast::label::Label as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <&&dhall::syntax::ast::label::Label as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <&_ as minicbor::encode::Encode<_>>::encode::<_> |
53 | | } |
54 | | |
55 | | impl<C, T: Encode<C> + ?Sized> Encode<C> for &mut T { |
56 | 0 | fn encode<W: Write>(&self, e: &mut Encoder<W>, ctx: &mut C) -> Result<(), Error<W::Error>> { |
57 | 0 | (**self).encode(e, ctx) |
58 | 0 | } |
59 | | } |
60 | | |
61 | | #[cfg(feature = "alloc")] |
62 | | impl<C, T: Encode<C> + ?Sized> Encode<C> for alloc::boxed::Box<T> { |
63 | 0 | fn encode<W: Write>(&self, e: &mut Encoder<W>, ctx: &mut C) -> Result<(), Error<W::Error>> { |
64 | 0 | (**self).encode(e, ctx) |
65 | 0 | } |
66 | | } |
67 | | |
68 | | impl<C> Encode<C> for str { |
69 | 0 | fn encode<W: Write>(&self, e: &mut Encoder<W>, _: &mut C) -> Result<(), Error<W::Error>> { |
70 | 0 | e.str(self)?.ok() |
71 | 0 | } Unexecuted instantiation: <str as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <str as minicbor::encode::Encode<_>>::encode::<_> |
72 | | } |
73 | | |
74 | | impl<C, T: Encode<C>> Encode<C> for Option<T> { |
75 | 0 | fn encode<W: Write>(&self, e: &mut Encoder<W>, ctx: &mut C) -> Result<(), Error<W::Error>> { |
76 | 0 | if let Some(x) = self { |
77 | 0 | x.encode(e, ctx)?; |
78 | | } else { |
79 | 0 | e.null()?; |
80 | | } |
81 | 0 | Ok(()) |
82 | 0 | } Unexecuted instantiation: <core::option::Option<alloc::string::String> as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <core::option::Option<dhall::syntax::ast::expr::Expr> as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <core::option::Option<dhall::syntax::ast::import::Hash> as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <core::option::Option<()> as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <core::option::Option<_> as minicbor::encode::Encode<_>>::encode::<_> |
83 | | |
84 | 0 | fn is_nil(&self) -> bool { |
85 | 0 | self.is_none() |
86 | 0 | } |
87 | | } |
88 | | |
89 | | impl<C, T: Encode<C>, E: Encode<C>> Encode<C> for Result<T, E> { |
90 | 0 | fn encode<W: Write>(&self, e: &mut Encoder<W>, ctx: &mut C) -> Result<(), Error<W::Error>> { |
91 | 0 | e.array(2)?; |
92 | 0 | match self { |
93 | 0 | Ok(v) => e.u32(0)?.encode_with(v, ctx)?.ok(), |
94 | 0 | Err(v) => e.u32(1)?.encode_with(v, ctx)?.ok() |
95 | | } |
96 | 0 | } |
97 | | } |
98 | | |
99 | | #[cfg(feature = "alloc")] |
100 | | impl<C> Encode<C> for alloc::string::String { |
101 | 0 | fn encode<W: Write>(&self, e: &mut Encoder<W>, _: &mut C) -> Result<(), Error<W::Error>> { |
102 | 0 | e.str(self)?.ok() |
103 | 0 | } Unexecuted instantiation: <alloc::string::String as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <alloc::string::String as minicbor::encode::Encode<_>>::encode::<_> |
104 | | } |
105 | | |
106 | | #[cfg(feature = "alloc")] |
107 | | impl<C, T> Encode<C> for alloc::borrow::Cow<'_, T> |
108 | | where |
109 | | T: Encode<C> + alloc::borrow::ToOwned + ?Sized |
110 | | { |
111 | 0 | fn encode<W: Write>(&self, e: &mut Encoder<W>, ctx: &mut C) -> Result<(), Error<W::Error>> { |
112 | 0 | self.as_ref().encode(e, ctx) |
113 | 0 | } |
114 | | } |
115 | | |
116 | | #[cfg(feature = "std")] |
117 | | impl<C, T, S> Encode<C> for std::collections::HashSet<T, S> |
118 | | where |
119 | | T: Encode<C>, |
120 | | S: std::hash::BuildHasher |
121 | | { |
122 | | fn encode<W: Write>(&self, e: &mut Encoder<W>, ctx: &mut C) -> Result<(), Error<W::Error>> { |
123 | | e.array(self.len() as u64)?; |
124 | | for x in self { |
125 | | x.encode(e, ctx)? |
126 | | } |
127 | | Ok(()) |
128 | | } |
129 | | } |
130 | | |
131 | | #[cfg(feature = "std")] |
132 | | impl<C, K, V, S> Encode<C> for std::collections::HashMap<K, V, S> |
133 | | where |
134 | | K: Encode<C> + Eq + std::hash::Hash, |
135 | | V: Encode<C>, |
136 | | S: std::hash::BuildHasher |
137 | | { |
138 | | fn encode<W: Write>(&self, e: &mut Encoder<W>, ctx: &mut C) -> Result<(), Error<W::Error>> { |
139 | | e.map(self.len() as u64)?; |
140 | | for (k, v) in self { |
141 | | k.encode(e, ctx)?; |
142 | | v.encode(e, ctx)?; |
143 | | } |
144 | | Ok(()) |
145 | | } |
146 | | } |
147 | | |
148 | | #[cfg(feature = "alloc")] |
149 | | impl<C, K, V> Encode<C> for alloc::collections::BTreeMap<K, V> |
150 | | where |
151 | | K: Encode<C> + Eq + Ord, |
152 | | V: Encode<C> |
153 | | { |
154 | 0 | fn encode<W: Write>(&self, e: &mut Encoder<W>, ctx: &mut C) -> Result<(), Error<W::Error>> { |
155 | 0 | e.map(self.len() as u64)?; |
156 | 0 | for (k, v) in self { |
157 | 0 | k.encode(e, ctx)?; |
158 | 0 | v.encode(e, ctx)?; |
159 | | } |
160 | 0 | Ok(()) |
161 | 0 | } Unexecuted instantiation: <alloc::collections::btree::map::BTreeMap<dhall::syntax::ast::label::Label, core::option::Option<dhall::syntax::ast::expr::Expr>> as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <alloc::collections::btree::map::BTreeMap<dhall::syntax::ast::label::Label, dhall::syntax::ast::expr::Expr> as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <alloc::collections::btree::map::BTreeMap<_, _> as minicbor::encode::Encode<_>>::encode::<_> |
162 | | } |
163 | | |
164 | | impl<C, T> Encode<C> for core::marker::PhantomData<T> { |
165 | 0 | fn encode<W: Write>(&self, e: &mut Encoder<W>, _: &mut C) -> Result<(), Error<W::Error>> { |
166 | 0 | e.array(0)?.ok() |
167 | 0 | } |
168 | | } |
169 | | |
170 | | impl<C> Encode<C> for () { |
171 | 0 | fn encode<W: Write>(&self, e: &mut Encoder<W>, _: &mut C) -> Result<(), Error<W::Error>> { |
172 | 0 | e.array(0)?.ok() |
173 | 0 | } Unexecuted instantiation: <() as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <() as minicbor::encode::Encode<_>>::encode::<_> |
174 | | } |
175 | | |
176 | | impl<C, T: Encode<C>> Encode<C> for core::num::Wrapping<T> { |
177 | 0 | fn encode<W: Write>(&self, e: &mut Encoder<W>, ctx: &mut C) -> Result<(), Error<W::Error>> { |
178 | 0 | self.0.encode(e, ctx) |
179 | 0 | } |
180 | | } |
181 | | |
182 | | #[cfg(target_pointer_width = "32")] |
183 | | impl<C> Encode<C> for usize { |
184 | | fn encode<W: Write>(&self, e: &mut Encoder<W>, _: &mut C) -> Result<(), Error<W::Error>> { |
185 | | e.u32(*self as u32)?.ok() |
186 | | } |
187 | | } |
188 | | |
189 | | #[cfg(target_pointer_width = "64")] |
190 | | impl<C> Encode<C> for usize { |
191 | 0 | fn encode<W: Write>(&self, e: &mut Encoder<W>, _: &mut C) -> Result<(), Error<W::Error>> { |
192 | 0 | e.u64(*self as u64)?.ok() |
193 | 0 | } |
194 | | } |
195 | | |
196 | | #[cfg(target_pointer_width = "32")] |
197 | | impl<C> Encode<C> for isize { |
198 | | fn encode<W: Write>(&self, e: &mut Encoder<W>, _: &mut C) -> Result<(), Error<W::Error>> { |
199 | | e.i32(*self as i32)?.ok() |
200 | | } |
201 | | } |
202 | | |
203 | | #[cfg(target_pointer_width = "64")] |
204 | | impl<C> Encode<C> for isize { |
205 | 0 | fn encode<W: Write>(&self, e: &mut Encoder<W>, _: &mut C) -> Result<(), Error<W::Error>> { |
206 | 0 | e.i64(*self as i64)?.ok() |
207 | 0 | } |
208 | | } |
209 | | |
210 | | impl<C> Encode<C> for crate::data::Int { |
211 | 0 | fn encode<W: Write>(&self, e: &mut Encoder<W>, _: &mut C) -> Result<(), Error<W::Error>> { |
212 | 0 | e.int(*self)?.ok() |
213 | 0 | } |
214 | | } |
215 | | |
216 | | macro_rules! encode_basic { |
217 | | ($($t:ident)*) => { |
218 | | $( |
219 | | impl<C> Encode<C> for $t { |
220 | 0 | fn encode<W: Write>(&self, e: &mut Encoder<W>, _: &mut C) -> Result<(), Error<W::Error>> { |
221 | 0 | e.$t(*self)?; |
222 | 0 | Ok(()) |
223 | 0 | } Unexecuted instantiation: <u64 as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <i64 as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <bool as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <f64 as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <char as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <u8 as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <i8 as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <u16 as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <i16 as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <u32 as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <i32 as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <u64 as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <i64 as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <bool as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <f32 as minicbor::encode::Encode<_>>::encode::<_> |
224 | | } |
225 | | )* |
226 | | } |
227 | | } |
228 | | |
229 | | encode_basic!(u8 i8 u16 i16 u32 i32 u64 i64 bool f32 f64 char); |
230 | | |
231 | | macro_rules! encode_nonzero { |
232 | | ($($t:ty)*) => { |
233 | | $( |
234 | | impl<C> Encode<C> for $t { |
235 | 0 | fn encode<W: Write>(&self, e: &mut Encoder<W>, ctx: &mut C) -> Result<(), Error<W::Error>> { |
236 | 0 | self.get().encode(e, ctx) |
237 | 0 | } Unexecuted instantiation: <core::num::nonzero::NonZero<u8> as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <core::num::nonzero::NonZero<u16> as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <core::num::nonzero::NonZero<u32> as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <core::num::nonzero::NonZero<u64> as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <core::num::nonzero::NonZero<i8> as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <core::num::nonzero::NonZero<i16> as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <core::num::nonzero::NonZero<i32> as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <core::num::nonzero::NonZero<i64> as minicbor::encode::Encode<_>>::encode::<_> |
238 | | } |
239 | | )* |
240 | | } |
241 | | } |
242 | | |
243 | | encode_nonzero! { |
244 | | core::num::NonZeroU8 |
245 | | core::num::NonZeroU16 |
246 | | core::num::NonZeroU32 |
247 | | core::num::NonZeroU64 |
248 | | core::num::NonZeroI8 |
249 | | core::num::NonZeroI16 |
250 | | core::num::NonZeroI32 |
251 | | core::num::NonZeroI64 |
252 | | } |
253 | | |
254 | | #[cfg(any(atomic32, atomic64))] |
255 | | macro_rules! encode_atomic { |
256 | | ($($t:ty)*) => { |
257 | | $( |
258 | | impl<C> Encode<C> for $t { |
259 | 0 | fn encode<W: Write>(&self, e: &mut Encoder<W>, ctx: &mut C) -> Result<(), Error<W::Error>> { |
260 | 0 | self.load(core::sync::atomic::Ordering::SeqCst).encode(e, ctx)?; |
261 | 0 | Ok(()) |
262 | 0 | } Unexecuted instantiation: <core::sync::atomic::AtomicBool as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <core::sync::atomic::AtomicU8 as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <core::sync::atomic::AtomicU16 as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <core::sync::atomic::AtomicU32 as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <core::sync::atomic::AtomicU64 as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <core::sync::atomic::AtomicUsize as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <core::sync::atomic::AtomicI8 as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <core::sync::atomic::AtomicI16 as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <core::sync::atomic::AtomicI32 as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <core::sync::atomic::AtomicI64 as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <core::sync::atomic::AtomicIsize as minicbor::encode::Encode<_>>::encode::<_> |
263 | | } |
264 | | )* |
265 | | } |
266 | | } |
267 | | |
268 | | #[cfg(atomic32)] |
269 | | encode_atomic! { |
270 | | core::sync::atomic::AtomicBool |
271 | | core::sync::atomic::AtomicU8 |
272 | | core::sync::atomic::AtomicU16 |
273 | | core::sync::atomic::AtomicU32 |
274 | | core::sync::atomic::AtomicUsize |
275 | | core::sync::atomic::AtomicI8 |
276 | | core::sync::atomic::AtomicI16 |
277 | | core::sync::atomic::AtomicI32 |
278 | | core::sync::atomic::AtomicIsize |
279 | | } |
280 | | |
281 | | #[cfg(atomic64)] |
282 | | encode_atomic! { |
283 | | core::sync::atomic::AtomicBool |
284 | | core::sync::atomic::AtomicU8 |
285 | | core::sync::atomic::AtomicU16 |
286 | | core::sync::atomic::AtomicU32 |
287 | | core::sync::atomic::AtomicU64 |
288 | | core::sync::atomic::AtomicUsize |
289 | | core::sync::atomic::AtomicI8 |
290 | | core::sync::atomic::AtomicI16 |
291 | | core::sync::atomic::AtomicI32 |
292 | | core::sync::atomic::AtomicI64 |
293 | | core::sync::atomic::AtomicIsize |
294 | | } |
295 | | |
296 | | macro_rules! encode_sequential { |
297 | | ($($t:ty)*) => { |
298 | | $( |
299 | | impl<C, T: Encode<C>> Encode<C> for $t { |
300 | 0 | fn encode<W: Write>(&self, e: &mut Encoder<W>, ctx: &mut C) -> Result<(), Error<W::Error>> { |
301 | 0 | e.array(self.len() as u64)?; |
302 | 0 | for x in self { |
303 | 0 | x.encode(e, ctx)? |
304 | | } |
305 | 0 | Ok(()) |
306 | 0 | } Unexecuted instantiation: <alloc::vec::Vec<dhall::syntax::ast::label::Label> as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <alloc::collections::btree::set::BTreeSet<_> as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <[_] as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <alloc::vec::Vec<_> as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <alloc::collections::vec_deque::VecDeque<_> as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <alloc::collections::linked_list::LinkedList<_> as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <alloc::collections::binary_heap::BinaryHeap<_> as minicbor::encode::Encode<_>>::encode::<_> |
307 | | } |
308 | | )* |
309 | | } |
310 | | } |
311 | | |
312 | | encode_sequential!([T]); |
313 | | |
314 | | #[cfg(feature = "alloc")] |
315 | | encode_sequential! { |
316 | | alloc::vec::Vec<T> |
317 | | alloc::collections::VecDeque<T> |
318 | | alloc::collections::LinkedList<T> |
319 | | alloc::collections::BinaryHeap<T> |
320 | | alloc::collections::BTreeSet<T> |
321 | | } |
322 | | |
323 | | macro_rules! encode_arrays { |
324 | | ($($n:expr)*) => { |
325 | | $( |
326 | | impl<C, T: Encode<C>> Encode<C> for [T; $n] { |
327 | 0 | fn encode<W: Write>(&self, e: &mut Encoder<W>, ctx: &mut C) -> Result<(), Error<W::Error>> { |
328 | 0 | e.array($n)?; |
329 | 0 | for x in self { |
330 | 0 | x.encode(e, ctx)? |
331 | | } |
332 | 0 | Ok(()) |
333 | 0 | } Unexecuted instantiation: <[_; 0] as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <[_; 1] as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <[_; 2] as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <[_; 3] as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <[_; 4] as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <[_; 5] as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <[_; 6] as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <[_; 7] as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <[_; 8] as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <[_; 9] as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <[_; 10] as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <[_; 11] as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <[_; 12] as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <[_; 13] as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <[_; 14] as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <[_; 15] as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <[_; 16] as minicbor::encode::Encode<_>>::encode::<_> |
334 | | } |
335 | | )* |
336 | | } |
337 | | } |
338 | | |
339 | | encode_arrays!(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16); |
340 | | |
341 | | macro_rules! encode_tuples { |
342 | | ($( $len:expr => { $($T:ident ($idx:tt))+ } )+) => { |
343 | | $( |
344 | | impl<Ctx, $($T: Encode<Ctx>),+> Encode<Ctx> for ($($T,)+) { |
345 | 0 | fn encode<W: Write>(&self, e: &mut Encoder<W>, ctx: &mut Ctx) -> Result<(), Error<W::Error>> { |
346 | 0 | e.array($len)? |
347 | 0 | $(.encode_with(&self.$idx, ctx)?)+ |
348 | 0 | .ok() |
349 | 0 | } Unexecuted instantiation: <(&dhall::syntax::ast::expr::Expr,) as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <(u64, &dhall::syntax::ast::expr::Expr) as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <(u64, &dhall::syntax::ast::expr::Expr, &dhall::syntax::ast::expr::Expr) as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <(u64, &dhall::syntax::ast::expr::Expr, &dhall::syntax::ast::label::Label) as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <(u64, &dhall::syntax::ast::expr::Expr, (&dhall::syntax::ast::expr::Expr,)) as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <(u64, &dhall::syntax::ast::expr::Expr, &dhall::syntax::ast::expr::Expr, &dhall::syntax::ast::expr::Expr) as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <(u64, &dhall::syntax::ast::expr::Expr, &alloc::vec::Vec<dhall::syntax::ast::label::Label>, &dhall::syntax::ast::expr::Expr) as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <(u64, u64, &dhall::syntax::ast::expr::Expr, &dhall::syntax::ast::expr::Expr) as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <(u64, &dhall::operations::kind::BinOp, &dhall::syntax::ast::expr::Expr, &dhall::syntax::ast::expr::Expr) as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <(u64, &i64) as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <(u64, &u64) as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <(u64, &alloc::collections::btree::map::BTreeMap<dhall::syntax::ast::label::Label, core::option::Option<dhall::syntax::ast::expr::Expr>>) as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <(u64, &alloc::collections::btree::map::BTreeMap<dhall::syntax::ast::label::Label, dhall::syntax::ast::expr::Expr>) as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <(u64, core::option::Option<()>, &dhall::syntax::ast::expr::Expr) as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <(&dhall::syntax::ast::label::Label, u64) as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <(u64, &dhall::syntax::ast::label::Label, &dhall::syntax::ast::expr::Expr, &dhall::syntax::ast::expr::Expr) as minicbor::encode::Encode<()>>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <(_,) as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <(_, _) as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <(_, _, _) as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <(_, _, _, _) as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <(_, _, _, _, _) as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <(_, _, _, _, _, _) as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <(_, _, _, _, _, _, _) as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <(_, _, _, _, _, _, _, _) as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _) as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _) as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _) as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _) as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _) as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _) as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as minicbor::encode::Encode<_>>::encode::<_> Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as minicbor::encode::Encode<_>>::encode::<_> |
350 | | } |
351 | | )+ |
352 | | } |
353 | | } |
354 | | |
355 | | encode_tuples! { |
356 | | 1 => { A(0) } |
357 | | 2 => { A(0) B(1) } |
358 | | 3 => { A(0) B(1) C(2) } |
359 | | 4 => { A(0) B(1) C(2) D(3) } |
360 | | 5 => { A(0) B(1) C(2) D(3) E(4) } |
361 | | 6 => { A(0) B(1) C(2) D(3) E(4) F(5) } |
362 | | 7 => { A(0) B(1) C(2) D(3) E(4) F(5) G(6) } |
363 | | 8 => { A(0) B(1) C(2) D(3) E(4) F(5) G(6) H(7) } |
364 | | 9 => { A(0) B(1) C(2) D(3) E(4) F(5) G(6) H(7) I(8) } |
365 | | 10 => { A(0) B(1) C(2) D(3) E(4) F(5) G(6) H(7) I(8) J(9) } |
366 | | 11 => { A(0) B(1) C(2) D(3) E(4) F(5) G(6) H(7) I(8) J(9) K(10) } |
367 | | 12 => { A(0) B(1) C(2) D(3) E(4) F(5) G(6) H(7) I(8) J(9) K(10) L(11) } |
368 | | 13 => { A(0) B(1) C(2) D(3) E(4) F(5) G(6) H(7) I(8) J(9) K(10) L(11) M(12) } |
369 | | 14 => { A(0) B(1) C(2) D(3) E(4) F(5) G(6) H(7) I(8) J(9) K(10) L(11) M(12) N(13) } |
370 | | 15 => { A(0) B(1) C(2) D(3) E(4) F(5) G(6) H(7) I(8) J(9) K(10) L(11) M(12) N(13) O(14) } |
371 | | 16 => { A(0) B(1) C(2) D(3) E(4) F(5) G(6) H(7) I(8) J(9) K(10) L(11) M(12) N(13) O(14) P(15) } |
372 | | } |
373 | | |
374 | | impl<C> Encode<C> for core::time::Duration { |
375 | 0 | fn encode<W: Write>(&self, e: &mut Encoder<W>, ctx: &mut C) -> Result<(), Error<W::Error>> { |
376 | 0 | e.array(2)? |
377 | 0 | .encode_with(self.as_secs(), ctx)? |
378 | 0 | .encode_with(self.subsec_nanos(), ctx)? |
379 | 0 | .ok() |
380 | 0 | } |
381 | | } |
382 | | |
383 | | #[cfg(feature = "std")] |
384 | | impl<C> Encode<C> for std::time::SystemTime { |
385 | | fn encode<W: Write>(&self, e: &mut Encoder<W>, ctx: &mut C) -> Result<(), Error<W::Error>> { |
386 | | match self.duration_since(std::time::UNIX_EPOCH) { |
387 | | Ok(d) => d.encode(e, ctx), |
388 | | Err(e) => Err(Error::custom(e).with_message("when encoding system time")) |
389 | | } |
390 | | } |
391 | | } |
392 | | |
393 | | impl<C, T: Encode<C> + Copy> Encode<C> for core::cell::Cell<T> { |
394 | 0 | fn encode<W: Write>(&self, e: &mut Encoder<W>, ctx: &mut C) -> Result<(), Error<W::Error>> { |
395 | 0 | self.get().encode(e, ctx) |
396 | 0 | } |
397 | | } |
398 | | |
399 | | impl<C, T: Encode<C>> Encode<C> for core::cell::RefCell<T> { |
400 | 0 | fn encode<W: Write>(&self, e: &mut Encoder<W>, ctx: &mut C) -> Result<(), Error<W::Error>> { |
401 | 0 | if let Ok(v) = self.try_borrow() { |
402 | 0 | v.encode(e, ctx) |
403 | | } else { |
404 | 0 | Err(Error::message("could not borrow ref cell value")) |
405 | | } |
406 | 0 | } |
407 | | } |
408 | | |
409 | | #[cfg(feature = "std")] |
410 | | impl<C> Encode<C> for std::path::Path { |
411 | | fn encode<W: Write>(&self, e: &mut Encoder<W>, _: &mut C) -> Result<(), Error<W::Error>> { |
412 | | if let Some(s) = self.to_str() { |
413 | | e.str(s)?.ok() |
414 | | } else { |
415 | | Err(Error::message("non-utf-8 path values are not supported")) |
416 | | } |
417 | | } |
418 | | } |
419 | | |
420 | | #[cfg(feature = "std")] |
421 | | impl<C> Encode<C> for std::path::PathBuf { |
422 | | fn encode<W: Write>(&self, e: &mut Encoder<W>, ctx: &mut C) -> Result<(), Error<W::Error>> { |
423 | | self.as_path().encode(e, ctx) |
424 | | } |
425 | | } |
426 | | |
427 | | #[cfg(feature = "std")] |
428 | | impl<C> Encode<C> for std::net::IpAddr { |
429 | | fn encode<W: Write>(&self, e: &mut Encoder<W>, ctx: &mut C) -> Result<(), Error<W::Error>> { |
430 | | e.array(2)?; |
431 | | match self { |
432 | | std::net::IpAddr::V4(a) => e.u32(0)?.encode_with(a, ctx)?.ok(), |
433 | | std::net::IpAddr::V6(a) => e.u32(1)?.encode_with(a, ctx)?.ok() |
434 | | } |
435 | | } |
436 | | } |
437 | | |
438 | | #[cfg(feature = "std")] |
439 | | impl<C> Encode<C> for std::net::Ipv4Addr { |
440 | | fn encode<W: Write>(&self, e: &mut Encoder<W>, _: &mut C) -> Result<(), Error<W::Error>> { |
441 | | e.bytes(&self.octets())?.ok() |
442 | | } |
443 | | } |
444 | | |
445 | | #[cfg(feature = "std")] |
446 | | impl<C> Encode<C> for std::net::Ipv6Addr { |
447 | | fn encode<W: Write>(&self, e: &mut Encoder<W>, _: &mut C) -> Result<(), Error<W::Error>> { |
448 | | e.bytes(&self.octets())?.ok() |
449 | | } |
450 | | } |
451 | | |
452 | | #[cfg(feature = "std")] |
453 | | impl<C> Encode<C> for std::net::SocketAddr { |
454 | | fn encode<W: Write>(&self, e: &mut Encoder<W>, ctx: &mut C) -> Result<(), Error<W::Error>> { |
455 | | e.array(2)?; |
456 | | match self { |
457 | | std::net::SocketAddr::V4(a) => e.u32(0)?.encode_with(a, ctx)?.ok(), |
458 | | std::net::SocketAddr::V6(a) => e.u32(1)?.encode_with(a, ctx)?.ok() |
459 | | } |
460 | | } |
461 | | } |
462 | | |
463 | | #[cfg(feature = "std")] |
464 | | impl<C> Encode<C> for std::net::SocketAddrV4 { |
465 | | fn encode<W: Write>(&self, e: &mut Encoder<W>, ctx: &mut C) -> Result<(), Error<W::Error>> { |
466 | | e.array(2)? |
467 | | .encode_with(self.ip(), ctx)? |
468 | | .encode_with(self.port(), ctx)? |
469 | | .ok() |
470 | | } |
471 | | } |
472 | | |
473 | | #[cfg(feature = "std")] |
474 | | impl<C> Encode<C> for std::net::SocketAddrV6 { |
475 | | fn encode<W: Write>(&self, e: &mut Encoder<W>, ctx: &mut C) -> Result<(), Error<W::Error>> { |
476 | | e.array(2)? |
477 | | .encode_with(self.ip(), ctx)? |
478 | | .encode_with(self.port(), ctx)? |
479 | | .ok() |
480 | | } |
481 | | } |
482 | | |
483 | | impl<C, T: Encode<C>> Encode<C> for core::ops::Range<T> { |
484 | 0 | fn encode<W: Write>(&self, e: &mut Encoder<W>, ctx: &mut C) -> Result<(), Error<W::Error>> { |
485 | 0 | e.array(2)? |
486 | 0 | .encode_with(&self.start, ctx)? |
487 | 0 | .encode_with(&self.end, ctx)? |
488 | 0 | .ok() |
489 | 0 | } |
490 | | } |
491 | | |
492 | | impl<C, T: Encode<C>> Encode<C> for core::ops::RangeFrom<T> { |
493 | 0 | fn encode<W: Write>(&self, e: &mut Encoder<W>, ctx: &mut C) -> Result<(), Error<W::Error>> { |
494 | 0 | e.array(1)? |
495 | 0 | .encode_with(&self.start, ctx)? |
496 | 0 | .ok() |
497 | 0 | } |
498 | | } |
499 | | |
500 | | impl<C, T: Encode<C>> Encode<C> for core::ops::RangeTo<T> { |
501 | 0 | fn encode<W: Write>(&self, e: &mut Encoder<W>, ctx: &mut C) -> Result<(), Error<W::Error>> { |
502 | 0 | e.array(1)? |
503 | 0 | .encode_with(&self.end, ctx)? |
504 | 0 | .ok() |
505 | 0 | } |
506 | | } |
507 | | |
508 | | impl<C, T: Encode<C>> Encode<C> for core::ops::RangeToInclusive<T> { |
509 | 0 | fn encode<W: Write>(&self, e: &mut Encoder<W>, ctx: &mut C) -> Result<(), Error<W::Error>> { |
510 | 0 | e.array(1)? |
511 | 0 | .encode_with(&self.end, ctx)? |
512 | 0 | .ok() |
513 | 0 | } |
514 | | } |
515 | | |
516 | | impl<C, T: Encode<C>> Encode<C> for core::ops::RangeInclusive<T> { |
517 | 0 | fn encode<W: Write>(&self, e: &mut Encoder<W>, ctx: &mut C) -> Result<(), Error<W::Error>> { |
518 | 0 | e.array(2)? |
519 | 0 | .encode_with(self.start(), ctx)? |
520 | 0 | .encode_with(self.end(), ctx)? |
521 | 0 | .ok() |
522 | 0 | } |
523 | | } |
524 | | |
525 | | impl<C, T: Encode<C>> Encode<C> for core::ops::Bound<T> { |
526 | 0 | fn encode<W: Write>(&self, e: &mut Encoder<W>, ctx: &mut C) -> Result<(), Error<W::Error>> { |
527 | 0 | e.array(2)?; |
528 | 0 | match self { |
529 | 0 | core::ops::Bound::Included(v) => e.u32(0)?.encode_with(v, ctx)?.ok(), |
530 | 0 | core::ops::Bound::Excluded(v) => e.u32(1)?.encode_with(v, ctx)?.ok(), |
531 | 0 | core::ops::Bound::Unbounded => e.u32(2)?.array(0)?.ok() |
532 | | } |
533 | 0 | } |
534 | | } |
535 | | |
536 | | /// An encodable iterator writing its items as a CBOR array. |
537 | | /// |
538 | | /// This type wraps any type implementing [`Iterator`] + [`Clone`] and encodes |
539 | | /// the items produced by the iterator as a CBOR array. |
540 | | #[derive(Debug)] |
541 | | pub struct ArrayIter<I>(I); |
542 | | |
543 | | impl<I> ArrayIter<I> { |
544 | 0 | pub fn new(it: I) -> Self { |
545 | 0 | ArrayIter(it) |
546 | 0 | } |
547 | | } |
548 | | |
549 | | impl<C, I, T> Encode<C> for ArrayIter<I> |
550 | | where |
551 | | I: Iterator<Item = T> + Clone, |
552 | | T: Encode<C> |
553 | | { |
554 | 0 | fn encode<W: Write>(&self, e: &mut Encoder<W>, ctx: &mut C) -> Result<(), Error<W::Error>> { |
555 | 0 | let iter = self.0.clone(); |
556 | 0 | let (low, up) = iter.size_hint(); |
557 | 0 | let exact = Some(low) == up; |
558 | 0 | if exact { |
559 | 0 | e.array(low as u64)?; |
560 | | } else { |
561 | 0 | e.begin_array()?; |
562 | | } |
563 | 0 | for item in iter { |
564 | 0 | item.encode(e, ctx)?; |
565 | | } |
566 | 0 | if !exact { |
567 | 0 | e.end()?; |
568 | 0 | } |
569 | 0 | Ok(()) |
570 | 0 | } |
571 | | } |
572 | | |
573 | | /// An encodable iterator writing its items as a CBOR map. |
574 | | /// |
575 | | /// This type wraps any type implementing [`Iterator`] + [`Clone`] and encodes |
576 | | /// the items produced by the iterator as a CBOR map. |
577 | | #[derive(Debug)] |
578 | | pub struct MapIter<I>(I); |
579 | | |
580 | | impl<I> MapIter<I> { |
581 | 0 | pub fn new(it: I) -> Self { |
582 | 0 | MapIter(it) |
583 | 0 | } |
584 | | } |
585 | | |
586 | | impl<C, I, K, V> Encode<C> for MapIter<I> |
587 | | where |
588 | | I: Iterator<Item = (K, V)> + Clone, |
589 | | K: Encode<C>, |
590 | | V: Encode<C> |
591 | | { |
592 | 0 | fn encode<W: Write>(&self, e: &mut Encoder<W>, ctx: &mut C) -> Result<(), Error<W::Error>> { |
593 | 0 | let iter = self.0.clone(); |
594 | 0 | let (low, up) = iter.size_hint(); |
595 | 0 | let exact = Some(low) == up; |
596 | 0 | if exact { |
597 | 0 | e.map(low as u64)?; |
598 | | } else { |
599 | 0 | e.begin_map()?; |
600 | | } |
601 | 0 | for (k, v) in iter { |
602 | 0 | k.encode(e, ctx)?; |
603 | 0 | v.encode(e, ctx)?; |
604 | | } |
605 | 0 | if !exact { |
606 | 0 | e.end()?; |
607 | 0 | } |
608 | 0 | Ok(()) |
609 | 0 | } |
610 | | } |
611 | | |