Macro impl_writeable_tlv_based

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

Implements Readable/Writeable for a struct storing it as a set of TLVs. Each TLV is read/written in the order they appear and contains a type number, a field name, and a de/serialization method, from the following:

If $fieldty is required, then $field is a required field that is not an Option nor a Vec. If $fieldty is (default_value, $default), then $field will be set to $default if not present. If $fieldty is (static_value, $static), then $field will be set to $static. If $fieldty is option, then $field is optional field. If $fieldty is upgradable_option, then $field is optional and read via MaybeReadable. If $fieldty is upgradable_required, then $field is stored as an Option and read via MaybeReadable, requiring the TLV to be present. If $fieldty is optional_vec, then $field is a Vec, which needs to have its individual elements serialized. Note that for optional_vec no bytes are written if the vec is empty If $fieldty is (legacy, $ty, $write) then, when writing, the function $write will be called with the object being serialized and a returned Option and is written as a TLV if Some. When reading, an optional field of type $ty is read (which can be used in later default_value or static_value fields by referring to the value by name).

For example,

struct LightningMessage {
	tlv_integer: u32,
	tlv_default_integer: u32,
	tlv_optional_integer: Option<u32>,
	tlv_vec_type_integer: Vec<u32>,
	tlv_upgraded_integer: u32,
}

impl_writeable_tlv_based!(LightningMessage, {
	(0, tlv_integer, required),
	(1, tlv_default_integer, (default_value, 7)),
	(2, tlv_optional_integer, option),
	(3, tlv_vec_type_integer, optional_vec),
	(4, unwritten_type, (legacy, u32, |us: &LightningMessage| Some(us.tlv_integer))),
	(_unused, tlv_upgraded_integer, (static_value, unwritten_type.unwrap_or(0) * 2))
});