pub trait Encode<C> {
// Required method
fn encode<W: Write>(
&self,
e: &mut Encoder<W>,
ctx: &mut C,
) -> Result<(), Error<W::Error>>;
// Provided method
fn is_nil(&self) -> bool { ... }
}
Expand description
A type that can be encoded to CBOR.
If this type’s CBOR encoding is meant to be decoded by Decode
impls
derived with minicbor_derive
it is advisable to only produce a
single CBOR data item. Tagging, maps or arrays can and should be used
for multiple values.
Required Methods§
sourcefn encode<W: Write>(
&self,
e: &mut Encoder<W>,
ctx: &mut C,
) -> Result<(), Error<W::Error>>
fn encode<W: Write>( &self, e: &mut Encoder<W>, ctx: &mut C, ) -> Result<(), Error<W::Error>>
Encode a value of this type using the given Encoder
.
In addition to the encoder a user provided encoding context is given
as another parameter. Most implementations of this trait do not need an
encoding context and should be completely generic in the context
type. In cases where a context is needed and the Encode
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 is_nil(&self) -> bool
fn is_nil(&self) -> bool
Is this value of Self
a nil value?
This method is primarily used by minicbor-derive
.
Some types have a special value to denote the concept of “nothing”, aka
nil. An example is the Option
type with its None
value. This
method–if overriden–allows checking if a value is such a special nil
value.
NB: A type implementing Encode
with an overriden Encode::is_nil
method should also override Decode::nil
if it implements Decode
at all.