Macro encode_tlv_stream
macro_rules! encode_tlv_stream { ($stream: expr, {$(($type: expr, $field: expr, $fieldty: tt)),* $(,)*}) => { ... }; }
Expand description
Implements the TLVs serialization part in a Writeable
implementation of a struct.
This should be called inside a method which returns Result<_,
io::Error
>
, such as
Writeable::write
. It will only return an Err
if the stream Err
s or Writeable::write
on one of the fields Err
s.
$stream
must be a &mut
Writer
which will receive the bytes for each TLV in the stream.
Fields MUST be sorted in $type
-order.
Note that the lightning TLV requirements require that a single type not appear more than once, that TLVs are sorted in type-ascending order, and that any even types be understood by the decoder.
Any option
fields which have a value of None
will not be serialized at all.
For example,
let mut required_value = 0u64;
let mut optional_value: Option<u64> = None;
encode_tlv_stream!(stream, {
(0, required_value, required),
(1, Some(42u64), option),
(2, optional_value, option),
});
// At this point `required_value` has been written as a TLV of type 0, `42u64` has been written
// as a TLV of type 1 (indicating the reader may ignore it if it is not understood), and *no*
// TLV is written with type 2.