pub trait Decode<'b, C>: Sized {
// Required method
fn decode(d: &mut Decoder<'b>, ctx: &mut C) -> Result<Self, Error>;
// Provided method
fn nil() -> Option<Self> { ... }
}
Expand description
A type that can be decoded from CBOR.
Required Methods§
sourcefn decode(d: &mut Decoder<'b>, ctx: &mut C) -> Result<Self, Error>
fn decode(d: &mut Decoder<'b>, ctx: &mut C) -> Result<Self, Error>
Decode a value using the given Decoder
.
In addition to the decoder a user provided decoding context is given
as another parameter. Most implementations of this trait do not need
a decoding context and should be completely generic in the context
type. In cases where a context is needed and the Decode
impl type is
meant to be combined with other types that require a different context
type, it is preferrable to constrain the context type variable C
with
a trait bound instead of fixing the type.
Provided Methods§
sourcefn nil() -> Option<Self>
fn nil() -> Option<Self>
If possible, return a nil value of Self
.
This method is primarily used by minicbor-derive
and allows
creating a special value denoting the absence of a “real” value if
no CBOR value is present. The canonical example of a type where
this is sensible is the Option
type, whose Decode::nil
method
would return Some(None)
.
With the exception of Option<_>
all types T
are considered
mandatory by default, i.e. T::nil()
returns None
. Missing values
of T
therefore cause decoding errors in derived Decode
implementations.
NB: A type implementing Decode
with an overriden Decode::nil
method should also override Encode::is_nil
if it implements Encode
at all.