Coverage Report

Created: 2026-09-14 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/minicbor-0.18.0/src/decode.rs
Line
Count
Source
1
//! Traits and types for decoding CBOR.
2
//!
3
//! This module defines the trait [`Decode`] and the actual [`Decoder`].
4
5
mod decoder;
6
mod error;
7
8
pub use decoder::{Decoder, Probe};
9
pub use decoder::{ArrayIter, ArrayIterWithCtx, BytesIter, MapIter, MapIterWithCtx, StrIter};
10
pub use error::Error;
11
12
#[cfg(feature = "half")]
13
mod tokens;
14
15
#[cfg(feature = "half")]
16
pub use tokens::{Token, Tokenizer};
17
18
/// A type that can be decoded from CBOR.
19
pub trait Decode<'b, C>: Sized {
20
    /// Decode a value using the given `Decoder`.
21
    ///
22
    /// In addition to the decoder a user provided decoding context is given
23
    /// as another parameter. Most implementations of this trait do not need
24
    /// a decoding context and should be completely generic in the context
25
    /// type. In cases where a context is needed and the `Decode` impl type is
26
    /// meant to be combined with other types that require a different context
27
    /// type, it is preferrable to constrain the context type variable `C` with
28
    /// a trait bound instead of fixing the type.
29
    fn decode(d: &mut Decoder<'b>, ctx: &mut C) -> Result<Self, Error>;
30
31
    /// If possible, return a nil value of `Self`.
32
    ///
33
    /// This method is primarily used by `minicbor-derive` and allows
34
    /// creating a special value denoting the absence of a "real" value if
35
    /// no CBOR value is present. The canonical example of a type where
36
    /// this is sensible is the `Option` type, whose `Decode::nil` method
37
    /// would return `Some(None)`.
38
    ///
39
    /// With the exception of `Option<_>` all types `T` are considered
40
    /// mandatory by default, i.e. `T::nil()` returns `None`. Missing values
41
    /// of `T` therefore cause decoding errors in derived `Decode`
42
    /// implementations.
43
    ///
44
    /// NB: A type implementing `Decode` with an overriden `Decode::nil`
45
    /// method should also override `Encode::is_nil` if it implements `Encode`
46
    /// at all.
47
0
    fn nil() -> Option<Self> {
48
0
        None
49
0
    }
50
}
51
52
#[cfg(feature = "alloc")]
53
impl<'b, C, T: Decode<'b, C>> Decode<'b, C> for alloc::boxed::Box<T> {
54
0
    fn decode(d: &mut Decoder<'b>, ctx: &mut C) -> Result<Self, Error> {
55
0
        T::decode(d, ctx).map(alloc::boxed::Box::new)
56
0
    }
57
}
58
59
impl<'a, 'b: 'a, C> Decode<'b, C> for &'a str {
60
0
    fn decode(d: &mut Decoder<'b>, _: &mut C) -> Result<Self, Error> {
61
0
        d.str()
62
0
    }
63
}
64
65
#[cfg(feature = "alloc")]
66
impl<'b, C, T> Decode<'b, C> for alloc::borrow::Cow<'_, T>
67
where
68
    T: alloc::borrow::ToOwned + ?Sized,
69
    T::Owned: Decode<'b, C>
70
{
71
0
    fn decode(d: &mut Decoder<'b>, ctx: &mut C) -> Result<Self, Error> {
72
0
        d.decode_with(ctx).map(alloc::borrow::Cow::Owned)
73
0
    }
74
}
75
76
#[cfg(feature = "alloc")]
77
impl<'b, C> Decode<'b, C> for alloc::string::String {
78
0
    fn decode(d: &mut Decoder<'b>, _: &mut C) -> Result<Self, Error> {
79
0
        d.str().map(alloc::string::String::from)
80
0
    }
Unexecuted instantiation: <alloc::string::String as minicbor::decode::Decode<()>>::decode
Unexecuted instantiation: <alloc::string::String as minicbor::decode::Decode<_>>::decode
81
}
82
83
#[cfg(feature = "alloc")]
84
impl<'b, C> Decode<'b, C> for alloc::boxed::Box<str> {
85
0
    fn decode(d: &mut Decoder<'b>, _: &mut C) -> Result<Self, Error> {
86
0
        d.str().map(Into::into)
87
0
    }
88
}
89
90
impl<'b, C, T: Decode<'b, C>> Decode<'b, C> for Option<T> {
91
0
    fn decode(d: &mut Decoder<'b>, ctx: &mut C) -> Result<Self, Error> {
92
0
        if crate::data::Type::Null == d.datatype()? {
93
0
            d.skip()?;
94
0
            return Ok(None)
95
0
        }
96
0
        T::decode(d, ctx).map(Some)
97
0
    }
98
99
0
    fn nil() -> Option<Self> {
100
0
        Some(None)
101
0
    }
102
}
103
104
impl<'b, C, T, E> Decode<'b, C> for Result<T, E>
105
where
106
    T: Decode<'b, C>,
107
    E: Decode<'b, C>
108
{
109
0
    fn decode(d: &mut Decoder<'b>, ctx: &mut C) -> Result<Self, Error> {
110
0
        let p = d.position();
111
0
        if Some(2) != d.array()? {
112
0
            return Err(Error::message("expected enum (2-element array)").at(p))
113
0
        }
114
0
        let p = d.position();
115
0
        match d.u32()? {
116
0
            0 => T::decode(d, ctx).map(Ok),
117
0
            1 => E::decode(d, ctx).map(Err),
118
0
            n => Err(Error::unknown_variant(n).at(p))
119
        }
120
0
    }
121
}
122
123
#[cfg(feature = "alloc")]
124
impl<'b, C, T> Decode<'b, C> for alloc::collections::BinaryHeap<T>
125
where
126
    T: Decode<'b, C> + Ord
127
{
128
0
    fn decode(d: &mut Decoder<'b>, ctx: &mut C) -> Result<Self, Error> {
129
0
        let iter: ArrayIterWithCtx<C, T> = d.array_iter_with(ctx)?;
130
0
        let mut v = alloc::collections::BinaryHeap::new();
131
0
        for x in iter {
132
0
            v.push(x?)
133
        }
134
0
        Ok(v)
135
0
    }
136
}
137
138
#[cfg(feature = "std")]
139
impl<'b, C, T, S> Decode<'b, C> for std::collections::HashSet<T, S>
140
where
141
    T: Decode<'b, C> + Eq + std::hash::Hash,
142
    S: std::hash::BuildHasher + std::default::Default
143
{
144
    fn decode(d: &mut Decoder<'b>, ctx: &mut C) -> Result<Self, Error> {
145
        let iter: ArrayIterWithCtx<C, T> = d.array_iter_with(ctx)?;
146
        let mut v = std::collections::HashSet::default();
147
        for x in iter {
148
            v.insert(x?);
149
        }
150
        Ok(v)
151
    }
152
}
153
154
#[cfg(feature = "alloc")]
155
impl<'b, C, T> Decode<'b, C> for alloc::collections::BTreeSet<T>
156
where
157
    T: Decode<'b, C> + Ord
158
{
159
0
    fn decode(d: &mut Decoder<'b>, ctx: &mut C) -> Result<Self, Error> {
160
0
        let iter: ArrayIterWithCtx<C, T> = d.array_iter_with(ctx)?;
161
0
        let mut v = alloc::collections::BTreeSet::new();
162
0
        for x in iter {
163
0
            v.insert(x?);
164
        }
165
0
        Ok(v)
166
0
    }
167
}
168
169
#[cfg(feature = "std")]
170
impl<'b, C, K, V, S> Decode<'b, C> for std::collections::HashMap<K, V, S>
171
where
172
    K: Decode<'b, C> + Eq + std::hash::Hash,
173
    V: Decode<'b, C>,
174
    S: std::hash::BuildHasher + std::default::Default
175
{
176
    fn decode(d: &mut Decoder<'b>, ctx: &mut C) -> Result<Self, Error> {
177
        let mut m = std::collections::HashMap::default();
178
        let iter: MapIterWithCtx<C, K, V> = d.map_iter_with(ctx)?;
179
        for x in iter {
180
            let (k, v) = x?;
181
            m.insert(k, v);
182
        }
183
        Ok(m)
184
    }
185
}
186
187
#[cfg(feature = "alloc")]
188
impl<'b, C, K, V> Decode<'b, C> for alloc::collections::BTreeMap<K, V>
189
where
190
    K: Decode<'b, C> + Eq + Ord,
191
    V: Decode<'b, C>
192
{
193
0
    fn decode(d: &mut Decoder<'b>, ctx: &mut C) -> Result<Self, Error> {
194
0
        let mut m = alloc::collections::BTreeMap::new();
195
0
        let iter: MapIterWithCtx<C, K, V> = d.map_iter_with(ctx)?;
196
0
        for x in iter {
197
0
            let (k, v) = x?;
198
0
            m.insert(k, v);
199
        }
200
0
        Ok(m)
201
0
    }
202
}
203
204
impl<'b, C, T> Decode<'b, C> for core::marker::PhantomData<T> {
205
0
    fn decode(d: &mut Decoder<'b>, _: &mut C) -> Result<Self, Error> {
206
0
        let p = d.position();
207
0
        if Some(0) != d.array()? {
208
0
            return Err(Error::message("expected phantom data, i.e. an empty array").at(p))
209
0
        }
210
0
        Ok(core::marker::PhantomData)
211
0
    }
212
}
213
214
impl<'b, C> Decode<'b, C> for () {
215
0
    fn decode(d: &mut Decoder<'b>, _: &mut C) -> Result<Self, Error> {
216
0
        let p = d.position();
217
0
        if Some(0) != d.array()? {
218
0
            return Err(Error::message("expected unit, i.e. an empty array").at(p))
219
0
        }
220
0
        Ok(())
221
0
    }
222
}
223
224
impl<'b, C, T: Decode<'b, C>> Decode<'b, C> for core::num::Wrapping<T> {
225
0
    fn decode(d: &mut Decoder<'b>, ctx: &mut C) -> Result<Self, Error> {
226
0
        d.decode_with(ctx).map(core::num::Wrapping)
227
0
    }
228
}
229
230
#[cfg(target_pointer_width = "32")]
231
impl<'b, C> Decode<'b, C> for usize {
232
    fn decode(d: &mut Decoder<'b>, _: &mut C) -> Result<Self, Error> {
233
        d.u32().map(|n| n as usize)
234
    }
235
}
236
237
#[cfg(target_pointer_width = "64")]
238
impl<'b, C> Decode<'b, C> for usize {
239
0
    fn decode(d: &mut Decoder<'b>, _: &mut C) -> Result<Self, Error> {
240
0
        d.u64().map(|n| n as usize)
241
0
    }
242
}
243
244
#[cfg(target_pointer_width = "32")]
245
impl<'b, C> Decode<'b, C> for isize {
246
    fn decode(d: &mut Decoder<'b>, _: &mut C) -> Result<Self, Error> {
247
        d.i32().map(|n| n as isize)
248
    }
249
}
250
251
#[cfg(target_pointer_width = "64")]
252
impl<'b, C> Decode<'b, C> for isize {
253
0
    fn decode(d: &mut Decoder<'b>, _: &mut C) -> Result<Self, Error> {
254
0
        d.i64().map(|n| n as isize)
255
0
    }
256
}
257
258
impl<'b, C> Decode<'b, C> for crate::data::Int {
259
0
    fn decode(d: &mut Decoder<'b>, _: &mut C) -> Result<Self, Error> {
260
0
        d.int()
261
0
    }
262
}
263
264
macro_rules! decode_basic {
265
    ($($t:ident)*) => {
266
        $(
267
            impl<'b, C> Decode<'b, C> for $t {
268
0
                fn decode(d: &mut Decoder<'b>, _: &mut C) -> Result<Self, Error> {
269
0
                    d.$t()
270
0
                }
Unexecuted instantiation: <u8 as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <i8 as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <u16 as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <i16 as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <u32 as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <i32 as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <u64 as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <i64 as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <bool as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <f32 as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <f64 as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <char as minicbor::decode::Decode<_>>::decode
271
            }
272
        )*
273
    }
274
}
275
276
decode_basic!(u8 i8 u16 i16 u32 i32 u64 i64 bool f32 f64 char);
277
278
macro_rules! decode_nonzero {
279
    ($($t:ty, $msg:expr)*) => {
280
        $(
281
            impl<'b, C> Decode<'b, C> for $t {
282
0
                fn decode(d: &mut Decoder<'b>, ctx: &mut C) -> Result<Self, Error> {
283
0
                    let p = d.position();
284
0
                    <$t>::new(Decode::decode(d, ctx)?).ok_or_else(|| Error::message($msg).at(p))
Unexecuted instantiation: <core::num::nonzero::NonZero<u16> as minicbor::decode::Decode<_>>::decode::{closure#0}
Unexecuted instantiation: <core::num::nonzero::NonZero<u32> as minicbor::decode::Decode<_>>::decode::{closure#0}
Unexecuted instantiation: <core::num::nonzero::NonZero<u64> as minicbor::decode::Decode<_>>::decode::{closure#0}
Unexecuted instantiation: <core::num::nonzero::NonZero<i8> as minicbor::decode::Decode<_>>::decode::{closure#0}
Unexecuted instantiation: <core::num::nonzero::NonZero<i16> as minicbor::decode::Decode<_>>::decode::{closure#0}
Unexecuted instantiation: <core::num::nonzero::NonZero<i32> as minicbor::decode::Decode<_>>::decode::{closure#0}
Unexecuted instantiation: <core::num::nonzero::NonZero<i64> as minicbor::decode::Decode<_>>::decode::{closure#0}
Unexecuted instantiation: <core::num::nonzero::NonZero<u8> as minicbor::decode::Decode<_>>::decode::{closure#0}
285
0
                }
Unexecuted instantiation: <core::num::nonzero::NonZero<u16> as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <core::num::nonzero::NonZero<u32> as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <core::num::nonzero::NonZero<u64> as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <core::num::nonzero::NonZero<i8> as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <core::num::nonzero::NonZero<i16> as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <core::num::nonzero::NonZero<i32> as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <core::num::nonzero::NonZero<i64> as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <core::num::nonzero::NonZero<u8> as minicbor::decode::Decode<_>>::decode
286
            }
287
        )*
288
    }
289
}
290
291
decode_nonzero! {
292
    core::num::NonZeroU8,  "unexpected 0 when decoding a `NonZeroU8`"
293
    core::num::NonZeroU16, "unexpected 0 when decoding a `NonZeroU16`"
294
    core::num::NonZeroU32, "unexpected 0 when decoding a `NonZeroU32`"
295
    core::num::NonZeroU64, "unexpected 0 when decoding a `NonZeroU64`"
296
    core::num::NonZeroI8,  "unexpected 0 when decoding a `NonZeroI8`"
297
    core::num::NonZeroI16, "unexpected 0 when decoding a `NonZeroI16`"
298
    core::num::NonZeroI32, "unexpected 0 when decoding a `NonZeroI32`"
299
    core::num::NonZeroI64, "unexpected 0 when decoding a `NonZeroI64`"
300
}
301
302
#[cfg(any(atomic32, atomic64))]
303
macro_rules! decode_atomic {
304
    ($($t:ty)*) => {
305
        $(
306
            impl<'b, C> Decode<'b, C> for $t {
307
0
                fn decode(d: &mut Decoder<'b>, ctx: &mut C) -> Result<Self, Error> {
308
0
                    d.decode_with(ctx).map(<$t>::new)
309
0
                }
Unexecuted instantiation: <core::sync::atomic::AtomicBool as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <core::sync::atomic::AtomicU8 as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <core::sync::atomic::AtomicU16 as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <core::sync::atomic::AtomicU32 as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <core::sync::atomic::AtomicU64 as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <core::sync::atomic::AtomicUsize as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <core::sync::atomic::AtomicI8 as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <core::sync::atomic::AtomicI16 as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <core::sync::atomic::AtomicI32 as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <core::sync::atomic::AtomicI64 as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <core::sync::atomic::AtomicIsize as minicbor::decode::Decode<_>>::decode
310
            }
311
        )*
312
    }
313
}
314
315
#[cfg(atomic32)]
316
decode_atomic! {
317
    core::sync::atomic::AtomicBool
318
    core::sync::atomic::AtomicU8
319
    core::sync::atomic::AtomicU16
320
    core::sync::atomic::AtomicU32
321
    core::sync::atomic::AtomicUsize
322
    core::sync::atomic::AtomicI8
323
    core::sync::atomic::AtomicI16
324
    core::sync::atomic::AtomicI32
325
    core::sync::atomic::AtomicIsize
326
}
327
328
#[cfg(atomic64)]
329
decode_atomic! {
330
    core::sync::atomic::AtomicBool
331
    core::sync::atomic::AtomicU8
332
    core::sync::atomic::AtomicU16
333
    core::sync::atomic::AtomicU32
334
    core::sync::atomic::AtomicU64
335
    core::sync::atomic::AtomicUsize
336
    core::sync::atomic::AtomicI8
337
    core::sync::atomic::AtomicI16
338
    core::sync::atomic::AtomicI32
339
    core::sync::atomic::AtomicI64
340
    core::sync::atomic::AtomicIsize
341
}
342
343
#[cfg(feature = "alloc")]
344
macro_rules! decode_sequential {
345
    ($($t:ty, $push:ident)*) => {
346
        $(
347
            impl<'b, C, T: Decode<'b, C>> Decode<'b, C> for $t {
348
0
                fn decode(d: &mut Decoder<'b>, ctx: &mut C) -> Result<Self, Error> {
349
0
                    let iter: ArrayIterWithCtx<C, T> = d.array_iter_with(ctx)?;
350
0
                    let mut v = <$t>::new();
351
0
                    for x in iter {
352
0
                        v.$push(x?)
353
                    }
354
0
                    Ok(v)
355
0
                }
Unexecuted instantiation: <alloc::vec::Vec<_> as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <alloc::collections::vec_deque::VecDeque<_> as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <alloc::collections::linked_list::LinkedList<_> as minicbor::decode::Decode<_>>::decode
356
            }
357
        )*
358
    }
359
}
360
361
#[cfg(feature = "alloc")]
362
decode_sequential! {
363
    alloc::vec::Vec<T>, push
364
    alloc::collections::VecDeque<T>, push_back
365
    alloc::collections::LinkedList<T>, push_back
366
}
367
368
macro_rules! decode_arrays {
369
    ($($n:expr)*) => {
370
        $(
371
            impl<'b, C, T: Decode<'b, C> + Default> Decode<'b, C> for [T; $n] {
372
0
                fn decode(d: &mut Decoder<'b>, ctx: &mut C) -> Result<Self, Error> {
373
0
                    let p = d.position();
374
0
                    let iter: ArrayIterWithCtx<C, T> = d.array_iter_with(ctx)?;
375
0
                    let mut a: [T; $n] = Default::default();
376
0
                    let mut i = 0;
377
0
                    for x in iter {
378
0
                        if i >= a.len() {
379
0
                            let msg = concat!("array has more than ", $n, " elements");
380
0
                            return Err(Error::message(msg).at(p))
381
0
                        }
382
0
                        a[i] = x?;
383
0
                        i += 1;
384
                    }
385
0
                    if i < a.len() {
386
0
                        let msg = concat!("array has less than ", $n, " elements");
387
0
                        return Err(Error::message(msg).at(p))
388
0
                    }
389
0
                    Ok(a)
390
0
                }
Unexecuted instantiation: <[_; 5] as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <[_; 6] as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <[_; 7] as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <[_; 8] as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <[_; 9] as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <[_; 10] as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <[_; 11] as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <[_; 12] as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <[_; 13] as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <[_; 14] as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <[_; 15] as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <[_; 16] as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <[_; 0] as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <[_; 1] as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <[_; 2] as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <[_; 3] as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <[_; 4] as minicbor::decode::Decode<_>>::decode
391
            }
392
        )*
393
    }
394
}
395
396
decode_arrays!(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16);
397
398
macro_rules! decode_tuples {
399
    ($( $len:expr => { $($T:ident)+ } )+) => {
400
        $(
401
            impl<'b, Ctx, $($T: Decode<'b, Ctx>),+> Decode<'b, Ctx> for ($($T,)+) {
402
0
                fn decode(d: &mut Decoder<'b>, ctx: &mut Ctx) -> Result<Self, Error> {
403
0
                    let p = d.position();
404
0
                    let n = d.array()?;
405
0
                    if n != Some($len) {
406
0
                        return Err(Error::message(concat!("invalid ", $len, "-tuple length")).at(p))
407
0
                    }
408
0
                    Ok(($($T::decode(d, ctx)?,)+))
409
0
                }
Unexecuted instantiation: <(_,) as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <(_, _) as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <(_, _, _) as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <(_, _, _, _) as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <(_, _, _, _, _) as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <(_, _, _, _, _, _) as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <(_, _, _, _, _, _, _) as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <(_, _, _, _, _, _, _, _) as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _) as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _) as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _) as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _) as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _) as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _) as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as minicbor::decode::Decode<_>>::decode
Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as minicbor::decode::Decode<_>>::decode
410
            }
411
        )+
412
    }
413
}
414
415
decode_tuples! {
416
    1  => { A }
417
    2  => { A B }
418
    3  => { A B C }
419
    4  => { A B C D }
420
    5  => { A B C D E }
421
    6  => { A B C D E F }
422
    7  => { A B C D E F G }
423
    8  => { A B C D E F G H }
424
    9  => { A B C D E F G H I }
425
    10 => { A B C D E F G H I J }
426
    11 => { A B C D E F G H I J K }
427
    12 => { A B C D E F G H I J K L }
428
    13 => { A B C D E F G H I J K L M }
429
    14 => { A B C D E F G H I J K L M N }
430
    15 => { A B C D E F G H I J K L M N O }
431
    16 => { A B C D E F G H I J K L M N O P }
432
}
433
434
macro_rules! decode_fields {
435
    ($d:ident $c:ident | $($n:literal $x:ident => $t:ty ; $msg:literal)*) => {
436
        $(let mut $x : core::option::Option<$t> = None;)*
437
438
        let p = $d.position();
439
440
        match $d.array()? {
441
            Some(n) => for i in 0 .. n {
442
                match i {
443
                    $($n => $x = Some(Decode::decode($d, $c)?),)*
444
                    _    => $d.skip()?
445
                }
446
            }
447
            None => {
448
                let mut i = 0;
449
                while $d.datatype()? != crate::data::Type::Break {
450
                    match i {
451
                        $($n => $x = Some(Decode::decode($d, $c)?),)*
452
                        _    => $d.skip()?
453
                    }
454
                    i += 1
455
                }
456
                $d.skip()?
457
            }
458
        }
459
460
        $(let $x = if let Some(x) = $x {
461
            x
462
        } else {
463
            return Err(Error::missing_value($n).at(p).with_message($msg))
464
        };)*
465
    }
466
}
467
468
impl<'b, C> Decode<'b, C> for core::time::Duration {
469
0
    fn decode(d: &mut Decoder<'b>, ctx: &mut C) -> Result<Self, Error> {
470
0
        decode_fields! { d ctx |
471
            0 secs  => u64 ; "Duration::secs"
472
            1 nanos => u32 ; "Duration::nanos"
473
        }
474
0
        Ok(core::time::Duration::new(secs, nanos))
475
0
    }
476
}
477
478
#[cfg(feature = "std")]
479
impl<'b, C> Decode<'b, C> for std::time::SystemTime {
480
    fn decode(d: &mut Decoder<'b>, ctx: &mut C) -> Result<Self, Error> {
481
        let p = d.position();
482
        std::time::UNIX_EPOCH
483
            .checked_add(d.decode_with(ctx)?)
484
            .ok_or_else(|| Error::message("duration value can not represent system time").at(p))
485
    }
486
}
487
488
impl<'b, C, T: Decode<'b, C>> Decode<'b, C> for core::cell::Cell<T> {
489
0
    fn decode(d: &mut Decoder<'b>, ctx: &mut C) -> Result<Self, Error> {
490
0
        d.decode_with(ctx).map(core::cell::Cell::new)
491
0
    }
492
}
493
494
impl<'b, C, T: Decode<'b, C>> Decode<'b, C> for core::cell::RefCell<T> {
495
0
    fn decode(d: &mut Decoder<'b>, ctx: &mut C) -> Result<Self, Error> {
496
0
        d.decode_with(ctx).map(core::cell::RefCell::new)
497
0
    }
498
}
499
500
#[cfg(feature = "std")]
501
impl<'a, 'b: 'a, C> Decode<'b, C> for &'a std::path::Path {
502
    fn decode(d: &mut Decoder<'b>, _: &mut C) -> Result<Self, Error> {
503
        d.str().map(std::path::Path::new)
504
    }
505
}
506
507
#[cfg(feature = "std")]
508
impl<'b, C> Decode<'b, C> for Box<std::path::Path> {
509
    fn decode(d: &mut Decoder<'b>, ctx: &mut C) -> Result<Self, Error> {
510
        d.decode_with(ctx).map(std::path::PathBuf::into_boxed_path)
511
    }
512
}
513
514
#[cfg(feature = "std")]
515
impl<'b, C> Decode<'b, C> for std::path::PathBuf {
516
    fn decode(d: &mut Decoder<'b>, ctx: &mut C) -> Result<Self, Error> {
517
        d.decode_with(ctx).map(std::path::Path::to_path_buf)
518
    }
519
}
520
521
#[cfg(feature = "std")]
522
impl<'b, C> Decode<'b, C> for std::net::IpAddr {
523
    fn decode(d: &mut Decoder<'b>, ctx: &mut C) -> Result<Self, Error> {
524
        let p = d.position();
525
        if Some(2) != d.array()? {
526
            return Err(Error::message("expected enum (2-element array)").at(p))
527
        }
528
        let p = d.position();
529
        match d.u32()? {
530
            0 => Ok(std::net::Ipv4Addr::decode(d, ctx)?.into()),
531
            1 => Ok(std::net::Ipv6Addr::decode(d, ctx)?.into()),
532
            n => Err(Error::unknown_variant(n).at(p))
533
        }
534
    }
535
}
536
537
#[cfg(feature = "std")]
538
impl<'b, C> Decode<'b, C> for std::net::Ipv4Addr {
539
    fn decode(d: &mut Decoder<'b>, ctx: &mut C) -> Result<Self, Error> {
540
        let octets: crate::bytes::ByteArray<4> = Decode::decode(d, ctx)?;
541
        Ok(<[u8; 4]>::from(octets).into())
542
    }
543
}
544
545
#[cfg(feature = "std")]
546
impl<'b, C> Decode<'b, C> for std::net::Ipv6Addr {
547
    fn decode(d: &mut Decoder<'b>, ctx: &mut C) -> Result<Self, Error> {
548
        let octets: crate::bytes::ByteArray<16> = Decode::decode(d, ctx)?;
549
        Ok(<[u8; 16]>::from(octets).into())
550
    }
551
}
552
553
#[cfg(feature = "std")]
554
impl<'b, C> Decode<'b, C> for std::net::SocketAddr {
555
    fn decode(d: &mut Decoder<'b>, ctx: &mut C) -> Result<Self, Error> {
556
        let p = d.position();
557
        if Some(2) != d.array()? {
558
            return Err(Error::message("expected enum (2-element array)").at(p))
559
        }
560
        let p = d.position();
561
        match d.u32()? {
562
            0 => Ok(std::net::SocketAddrV4::decode(d, ctx)?.into()),
563
            1 => Ok(std::net::SocketAddrV6::decode(d, ctx)?.into()),
564
            n => Err(Error::unknown_variant(n).at(p))
565
        }
566
    }
567
}
568
569
#[cfg(feature = "std")]
570
impl<'b, C> Decode<'b, C> for std::net::SocketAddrV4 {
571
    fn decode(d: &mut Decoder<'b>, ctx: &mut C) -> Result<Self, Error> {
572
        decode_fields! { d ctx |
573
            0 ip   => std::net::Ipv4Addr ; "SocketAddrV4::ip"
574
            1 port => u16                ; "SocketAddrV4::port"
575
        }
576
        Ok(std::net::SocketAddrV4::new(ip, port))
577
    }
578
}
579
580
#[cfg(feature = "std")]
581
impl<'b, C> Decode<'b, C> for std::net::SocketAddrV6 {
582
    fn decode(d: &mut Decoder<'b>, ctx: &mut C) -> Result<Self, Error> {
583
        decode_fields! { d ctx |
584
            0 ip   => std::net::Ipv6Addr ; "SocketAddrV6::ip"
585
            1 port => u16                ; "SocketAddrV6::port"
586
        }
587
        Ok(std::net::SocketAddrV6::new(ip, port, 0, 0))
588
    }
589
}
590
591
impl<'b, C, T: Decode<'b, C>> Decode<'b,C > for core::ops::Range<T> {
592
0
    fn decode(d: &mut Decoder<'b>, ctx: &mut C) -> Result<Self, Error> {
593
0
        decode_fields! { d ctx |
594
            0 start => T ; "Range::start"
595
            1 end   => T ; "Range::end"
596
        }
597
0
        Ok(core::ops::Range { start, end })
598
0
    }
599
}
600
601
impl<'b, C, T: Decode<'b, C>> Decode<'b, C> for core::ops::RangeFrom<T> {
602
0
    fn decode(d: &mut Decoder<'b>, ctx: &mut C) -> Result<Self, Error> {
603
0
        decode_fields! { d ctx |
604
            0 start => T ; "RangeFrom::start"
605
        }
606
0
        Ok(core::ops::RangeFrom { start })
607
0
    }
608
}
609
610
impl<'b, C, T: Decode<'b, C>> Decode<'b, C> for core::ops::RangeTo<T> {
611
0
    fn decode(d: &mut Decoder<'b>, ctx: &mut C) -> Result<Self, Error> {
612
0
        decode_fields! { d ctx |
613
            0 end => T ; "RangeTo::end"
614
        }
615
0
        Ok(core::ops::RangeTo { end })
616
0
    }
617
}
618
619
impl<'b, C, T: Decode<'b, C>> Decode<'b, C> for core::ops::RangeToInclusive<T> {
620
0
    fn decode(d: &mut Decoder<'b>, ctx: &mut C) -> Result<Self, Error> {
621
0
        decode_fields! { d ctx |
622
            0 end => T ; "RangeToInclusive::end"
623
        }
624
0
        Ok(core::ops::RangeToInclusive { end })
625
0
    }
626
}
627
628
impl<'b, C, T: Decode<'b, C>> Decode<'b, C> for core::ops::RangeInclusive<T> {
629
0
    fn decode(d: &mut Decoder<'b>, ctx: &mut C) -> Result<Self, Error> {
630
0
        decode_fields! { d ctx |
631
            0 start => T ; "RangeInclusive::start"
632
            1 end   => T ; "RangeInclusive::end"
633
        }
634
0
        Ok(core::ops::RangeInclusive::new(start, end))
635
0
    }
636
}
637
638
impl<'b, C, T: Decode<'b, C>> Decode<'b, C> for core::ops::Bound<T> {
639
0
    fn decode(d: &mut Decoder<'b>, ctx: &mut C) -> Result<Self, Error> {
640
0
        let p = d.position();
641
0
        if Some(2) != d.array()? {
642
0
            return Err(Error::message("expected enum (2-element array)").at(p))
643
0
        }
644
0
        let p = d.position();
645
0
        match d.u32()? {
646
0
            0 => d.decode_with(ctx).map(core::ops::Bound::Included),
647
0
            1 => d.decode_with(ctx).map(core::ops::Bound::Excluded),
648
0
            2 => d.skip().map(|_| core::ops::Bound::Unbounded),
649
0
            n => Err(Error::unknown_variant(n).at(p))
650
        }
651
0
    }
652
}