Macro breez_sdk_core::lightning::decode_tlv_stream

macro_rules! decode_tlv_stream {
    ($stream: expr, {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*}) => { ... };
}
Expand description

Implements the TLVs deserialization part in a Readable implementation of a struct.

This should be called inside a method which returns Result<_, DecodeError>, such as Readable::read. It will either return an Err or ensure all required fields have been read and optionally read optional fields.

$stream must be a Read and will be fully consumed, reading until no more bytes remain (i.e. it returns DecodeError::ShortRead).

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.

For example,

let mut required_value = 0u64;
let mut optional_value: Option<u64> = None;
decode_tlv_stream!(stream, {
    (0, required_value, required),
    (2, optional_value, option),
});
// At this point, `required_value` has been overwritten with the TLV with type 0.
// `optional_value` may have been overwritten, setting it to `Some` if a TLV with type 2 was
// present.