+// This file is Copyright its original authors, visible in version control
+// history.
+//
+// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
+// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
+// You may not use this file except in accordance with one or both of these
+// licenses.
+
+//! Some macros that implement [`Readable`]/[`Writeable`] traits for lightning messages.
+//! They also handle serialization and deserialization of TLVs.
+//!
+//! [`Readable`]: crate::util::ser::Readable
+//! [`Writeable`]: crate::util::ser::Writeable
+
+// There are quite a few TLV serialization "types" which behave differently. We currently only
+// publicly document the `optional` and `required` types, not supporting anything else publicly and
+// changing them at will.
+//
+// Some of the other types include:
+// * (default_value, $default) - reads optionally, reading $default if no TLV is present
+// * (static_value, $value) - ignores any TLVs, always using $value
+// * required_vec - reads into a Vec without a length prefix, failing if no TLV is present.
+// * optional_vec - reads into an Option<Vec> without a length prefix, continuing if no TLV is
+// present. Writes from a Vec directly, only if any elements are present. Note
+// that the struct deserialization macros return a Vec, not an Option.
+// * upgradable_option - reads via MaybeReadable.
+// * upgradable_required - reads via MaybeReadable, requiring a TLV be present but may return None
+// if MaybeReadable::read() returns None.
+
+/// Implements serialization for a single TLV record.
+/// This is exported for use by other exported macros, do not use directly.
+#[doc(hidden)]
+#[macro_export]
+macro_rules! _encode_tlv {
+ ($stream: expr, $type: expr, $field: expr, (default_value, $default: expr)) => {
+ $crate::_encode_tlv!($stream, $type, $field, required)
+ };
+ ($stream: expr, $type: expr, $field: expr, (static_value, $value: expr)) => {
+ let _ = &$field; // Ensure we "use" the $field
+ };
+ ($stream: expr, $type: expr, $field: expr, required) => {
+ BigSize($type).write($stream)?;
+ BigSize($field.serialized_length() as u64).write($stream)?;
+ $field.write($stream)?;
+ };
+ ($stream: expr, $type: expr, $field: expr, required_vec) => {
+ $crate::_encode_tlv!($stream, $type, $crate::util::ser::WithoutLength(&$field), required);
+ };
+ ($stream: expr, $optional_type: expr, $optional_field: expr, option) => {
+ if let Some(ref field) = $optional_field {
+ BigSize($optional_type).write($stream)?;
+ BigSize(field.serialized_length() as u64).write($stream)?;
+ field.write($stream)?;
+ }
+ };
+ ($stream: expr, $type: expr, $field: expr, optional_vec) => {
+ if !$field.is_empty() {
+ $crate::_encode_tlv!($stream, $type, $field, required_vec);
+ }
+ };
+ ($stream: expr, $type: expr, $field: expr, upgradable_required) => {
+ $crate::_encode_tlv!($stream, $type, $field, required);
+ };
+ ($stream: expr, $type: expr, $field: expr, upgradable_option) => {
+ $crate::_encode_tlv!($stream, $type, $field, option);
+ };
+ ($stream: expr, $type: expr, $field: expr, (option, encoding: ($fieldty: ty, $encoding: ident))) => {
+ $crate::_encode_tlv!($stream, $type, $field.map(|f| $encoding(f)), option);
+ };
+ ($stream: expr, $type: expr, $field: expr, (option, encoding: $fieldty: ty)) => {
+ $crate::_encode_tlv!($stream, $type, $field, option);
+ };
+ ($stream: expr, $type: expr, $field: expr, (option: $trait: ident $(, $read_arg: expr)?)) => {
+ // Just a read-mapped type
+ $crate::_encode_tlv!($stream, $type, $field, option);
+ };
+}
+
+/// Panics if the last seen TLV type is not numerically less than the TLV type currently being checked.
+/// This is exported for use by other exported macros, do not use directly.
+#[doc(hidden)]
+#[macro_export]
+macro_rules! _check_encoded_tlv_order {
+ ($last_type: expr, $type: expr, (static_value, $value: expr)) => { };
+ ($last_type: expr, $type: expr, $fieldty: tt) => {
+ if let Some(t) = $last_type {
+ #[allow(unused_comparisons)] // Note that $type may be 0 making the following comparison always false
+ (debug_assert!(t < $type))
+ }
+ $last_type = Some($type);
+ };
+}
+
+/// 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,
+/// ```
+/// # use lightning::encode_tlv_stream;
+/// # fn write<W: lightning::util::ser::Writer> (stream: &mut W) -> Result<(), lightning::io::Error> {
+/// 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.
+/// # Ok(())
+/// # }
+/// ```
+///
+/// [`Writeable`]: crate::util::ser::Writeable
+/// [`io::Error`]: crate::io::Error
+/// [`Writeable::write`]: crate::util::ser::Writeable::write
+/// [`Writer`]: crate::util::ser::Writer
+#[macro_export]
+macro_rules! encode_tlv_stream {
+ ($stream: expr, {$(($type: expr, $field: expr, $fieldty: tt)),* $(,)*}) => {
+ $crate::_encode_tlv_stream!($stream, {$(($type, $field, $fieldty)),*})
+ }
+}
+
+/// Implementation of [`encode_tlv_stream`].
+/// This is exported for use by other exported macros, do not use directly.
+#[doc(hidden)]
+#[macro_export]
+macro_rules! _encode_tlv_stream {
+ ($stream: expr, {$(($type: expr, $field: expr, $fieldty: tt)),* $(,)*}) => { {
+ $crate::_encode_tlv_stream!($stream, { $(($type, $field, $fieldty)),* }, &[])
+ } };
+ ($stream: expr, {$(($type: expr, $field: expr, $fieldty: tt)),* $(,)*}, $extra_tlvs: expr) => { {
+ #[allow(unused_imports)]
+ use $crate::{
+ ln::msgs::DecodeError,
+ util::ser,
+ util::ser::BigSize,
+ util::ser::Writeable,
+ };
+
+ $(
+ $crate::_encode_tlv!($stream, $type, $field, $fieldty);
+ )*
+ for tlv in $extra_tlvs {
+ let (typ, value): &(u64, Vec<u8>) = tlv;
+ $crate::_encode_tlv!($stream, *typ, *value, required_vec);
+ }
+
+ #[allow(unused_mut, unused_variables, unused_assignments)]
+ #[cfg(debug_assertions)]
+ {
+ let mut last_seen: Option<u64> = None;
+ $(
+ $crate::_check_encoded_tlv_order!(last_seen, $type, $fieldty);
+ )*
+ for tlv in $extra_tlvs {
+ let (typ, _): &(u64, Vec<u8>) = tlv;
+ $crate::_check_encoded_tlv_order!(last_seen, *typ, required_vec);
+ }
+ }
+ } };
+}
+
+/// Adds the length of the serialized field to a [`LengthCalculatingWriter`].
+/// This is exported for use by other exported macros, do not use directly.
+///
+/// [`LengthCalculatingWriter`]: crate::util::ser::LengthCalculatingWriter
+#[doc(hidden)]
+#[macro_export]
+macro_rules! _get_varint_length_prefixed_tlv_length {
+ ($len: expr, $type: expr, $field: expr, (default_value, $default: expr)) => {
+ $crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field, required)
+ };
+ ($len: expr, $type: expr, $field: expr, (static_value, $value: expr)) => {
+ };
+ ($len: expr, $type: expr, $field: expr, required) => {
+ BigSize($type).write(&mut $len).expect("No in-memory data may fail to serialize");
+ let field_len = $field.serialized_length();
+ BigSize(field_len as u64).write(&mut $len).expect("No in-memory data may fail to serialize");
+ $len.0 += field_len;
+ };
+ ($len: expr, $type: expr, $field: expr, required_vec) => {
+ $crate::_get_varint_length_prefixed_tlv_length!($len, $type, $crate::util::ser::WithoutLength(&$field), required);
+ };
+ ($len: expr, $optional_type: expr, $optional_field: expr, option) => {
+ if let Some(ref field) = $optional_field {
+ BigSize($optional_type).write(&mut $len).expect("No in-memory data may fail to serialize");
+ let field_len = field.serialized_length();
+ BigSize(field_len as u64).write(&mut $len).expect("No in-memory data may fail to serialize");
+ $len.0 += field_len;
+ }
+ };
+ ($len: expr, $type: expr, $field: expr, optional_vec) => {
+ if !$field.is_empty() {
+ $crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field, required_vec);
+ }
+ };
+ ($len: expr, $type: expr, $field: expr, (option: $trait: ident $(, $read_arg: expr)?)) => {
+ $crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field, option);
+ };
+ ($len: expr, $type: expr, $field: expr, (option, encoding: ($fieldty: ty, $encoding: ident))) => {
+ $crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field.map(|f| $encoding(f)), option);
+ };
+ ($len: expr, $type: expr, $field: expr, upgradable_required) => {
+ $crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field, required);
+ };
+ ($len: expr, $type: expr, $field: expr, upgradable_option) => {
+ $crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field, option);
+ };
+}
+
+/// See the documentation of [`write_tlv_fields`].
+/// This is exported for use by other exported macros, do not use directly.
+#[doc(hidden)]
+#[macro_export]
+macro_rules! _encode_varint_length_prefixed_tlv {
+ ($stream: expr, {$(($type: expr, $field: expr, $fieldty: tt)),*}) => { {
+ $crate::_encode_varint_length_prefixed_tlv!($stream, {$(($type, $field, $fieldty)),*}, &[])
+ } };
+ ($stream: expr, {$(($type: expr, $field: expr, $fieldty: tt)),*}, $extra_tlvs: expr) => { {
+ extern crate alloc;
+ use $crate::util::ser::BigSize;
+ use alloc::vec::Vec;
+ let len = {
+ #[allow(unused_mut)]
+ let mut len = $crate::util::ser::LengthCalculatingWriter(0);
+ $(
+ $crate::_get_varint_length_prefixed_tlv_length!(len, $type, $field, $fieldty);
+ )*
+ for tlv in $extra_tlvs {
+ let (typ, value): &(u64, Vec<u8>) = tlv;
+ $crate::_get_varint_length_prefixed_tlv_length!(len, *typ, *value, required_vec);
+ }
+ len.0
+ };
+ BigSize(len as u64).write($stream)?;
+ $crate::_encode_tlv_stream!($stream, { $(($type, $field, $fieldty)),* }, $extra_tlvs);
+ } };
+}
+
+/// Errors if there are missing required TLV types between the last seen type and the type currently being processed.
+/// This is exported for use by other exported macros, do not use directly.
+#[doc(hidden)]
+#[macro_export]
+macro_rules! _check_decoded_tlv_order {
+ ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, (default_value, $default: expr)) => {{
+ #[allow(unused_comparisons)] // Note that $type may be 0 making the second comparison always false
+ let invalid_order = ($last_seen_type.is_none() || $last_seen_type.unwrap() < $type) && $typ.0 > $type;
+ if invalid_order {
+ $field = $default.into();
+ }
+ }};
+ ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, (static_value, $value: expr)) => {
+ };
+ ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, required) => {{
+ #[allow(unused_comparisons)] // Note that $type may be 0 making the second comparison always false
+ let invalid_order = ($last_seen_type.is_none() || $last_seen_type.unwrap() < $type) && $typ.0 > $type;
+ if invalid_order {
+ return Err(DecodeError::InvalidValue);
+ }
+ }};
+ ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, (required: $trait: ident $(, $read_arg: expr)?)) => {{
+ $crate::_check_decoded_tlv_order!($last_seen_type, $typ, $type, $field, required);
+ }};
+ ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, option) => {{
+ // no-op
+ }};
+ ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, required_vec) => {{
+ $crate::_check_decoded_tlv_order!($last_seen_type, $typ, $type, $field, required);
+ }};
+ ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, optional_vec) => {{
+ // no-op
+ }};
+ ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, upgradable_required) => {{
+ _check_decoded_tlv_order!($last_seen_type, $typ, $type, $field, required)
+ }};
+ ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, upgradable_option) => {{
+ // no-op
+ }};
+ ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, (option: $trait: ident $(, $read_arg: expr)?)) => {{
+ // no-op
+ }};
+ ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, (option, encoding: $encoding: tt)) => {{
+ // no-op
+ }};
+}
+
+/// Errors if there are missing required TLV types after the last seen type.
+/// This is exported for use by other exported macros, do not use directly.
+#[doc(hidden)]
+#[macro_export]
+macro_rules! _check_missing_tlv {
+ ($last_seen_type: expr, $type: expr, $field: ident, (default_value, $default: expr)) => {{
+ #[allow(unused_comparisons)] // Note that $type may be 0 making the second comparison always false
+ let missing_req_type = $last_seen_type.is_none() || $last_seen_type.unwrap() < $type;
+ if missing_req_type {
+ $field = $default.into();
+ }
+ }};
+ ($last_seen_type: expr, $type: expr, $field: expr, (static_value, $value: expr)) => {
+ $field = $value;
+ };
+ ($last_seen_type: expr, $type: expr, $field: ident, required) => {{
+ #[allow(unused_comparisons)] // Note that $type may be 0 making the second comparison always false
+ let missing_req_type = $last_seen_type.is_none() || $last_seen_type.unwrap() < $type;
+ if missing_req_type {
+ return Err(DecodeError::InvalidValue);
+ }
+ }};
+ ($last_seen_type: expr, $type: expr, $field: ident, (required: $trait: ident $(, $read_arg: expr)?)) => {{
+ $crate::_check_missing_tlv!($last_seen_type, $type, $field, required);
+ }};
+ ($last_seen_type: expr, $type: expr, $field: ident, required_vec) => {{
+ $crate::_check_missing_tlv!($last_seen_type, $type, $field, required);
+ }};
+ ($last_seen_type: expr, $type: expr, $field: ident, option) => {{
+ // no-op
+ }};
+ ($last_seen_type: expr, $type: expr, $field: ident, optional_vec) => {{
+ // no-op
+ }};
+ ($last_seen_type: expr, $type: expr, $field: ident, upgradable_required) => {{
+ _check_missing_tlv!($last_seen_type, $type, $field, required)
+ }};
+ ($last_seen_type: expr, $type: expr, $field: ident, upgradable_option) => {{
+ // no-op
+ }};
+ ($last_seen_type: expr, $type: expr, $field: ident, (option: $trait: ident $(, $read_arg: expr)?)) => {{
+ // no-op
+ }};
+ ($last_seen_type: expr, $type: expr, $field: ident, (option, encoding: $encoding: tt)) => {{
+ // no-op
+ }};
+}
+
+/// Implements deserialization for a single TLV record.
+/// This is exported for use by other exported macros, do not use directly.
+#[doc(hidden)]
+#[macro_export]
+macro_rules! _decode_tlv {
+ ($reader: expr, $field: ident, (default_value, $default: expr)) => {{
+ $crate::_decode_tlv!($reader, $field, required)
+ }};
+ ($reader: expr, $field: ident, (static_value, $value: expr)) => {{
+ }};
+ ($reader: expr, $field: ident, required) => {{
+ $field = $crate::util::ser::Readable::read(&mut $reader)?;
+ }};
+ ($reader: expr, $field: ident, (required: $trait: ident $(, $read_arg: expr)?)) => {{
+ $field = $trait::read(&mut $reader $(, $read_arg)*)?;
+ }};
+ ($reader: expr, $field: ident, required_vec) => {{
+ let f: $crate::util::ser::WithoutLength<Vec<_>> = $crate::util::ser::Readable::read(&mut $reader)?;
+ $field = f.0;
+ }};
+ ($reader: expr, $field: ident, option) => {{
+ $field = Some($crate::util::ser::Readable::read(&mut $reader)?);
+ }};
+ ($reader: expr, $field: ident, optional_vec) => {{
+ let f: $crate::util::ser::WithoutLength<Vec<_>> = $crate::util::ser::Readable::read(&mut $reader)?;
+ $field = Some(f.0);
+ }};
+ // `upgradable_required` indicates we're reading a required TLV that may have been upgraded
+ // without backwards compat. We'll error if the field is missing, and return `Ok(None)` if the
+ // field is present but we can no longer understand it.
+ // Note that this variant can only be used within a `MaybeReadable` read.
+ ($reader: expr, $field: ident, upgradable_required) => {{
+ $field = match $crate::util::ser::MaybeReadable::read(&mut $reader)? {
+ Some(res) => res,
+ _ => return Ok(None)
+ };
+ }};
+ // `upgradable_option` indicates we're reading an Option-al TLV that may have been upgraded
+ // without backwards compat. $field will be None if the TLV is missing or if the field is present
+ // but we can no longer understand it.
+ ($reader: expr, $field: ident, upgradable_option) => {{
+ $field = $crate::util::ser::MaybeReadable::read(&mut $reader)?;
+ }};
+ ($reader: expr, $field: ident, (option: $trait: ident $(, $read_arg: expr)?)) => {{
+ $field = Some($trait::read(&mut $reader $(, $read_arg)*)?);
+ }};
+ ($reader: expr, $field: ident, (option, encoding: ($fieldty: ty, $encoding: ident, $encoder:ty))) => {{
+ $crate::_decode_tlv!($reader, $field, (option, encoding: ($fieldty, $encoding)));
+ }};
+ ($reader: expr, $field: ident, (option, encoding: ($fieldty: ty, $encoding: ident))) => {{
+ $field = {
+ let field: $encoding<$fieldty> = ser::Readable::read(&mut $reader)?;
+ Some(field.0)
+ };
+ }};
+ ($reader: expr, $field: ident, (option, encoding: $fieldty: ty)) => {{
+ $crate::_decode_tlv!($reader, $field, option);
+ }};
+}
+
+/// Checks if `$val` matches `$type`.
+/// This is exported for use by other exported macros, do not use directly.
+#[doc(hidden)]
+#[macro_export]
+macro_rules! _decode_tlv_stream_match_check {
+ ($val: ident, $type: expr, (static_value, $value: expr)) => { false };
+ ($val: ident, $type: expr, $fieldty: tt) => { $val == $type }
+}
+
+/// 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,
+/// ```
+/// # use lightning::decode_tlv_stream;
+/// # fn read<R: lightning::io::Read> (stream: R) -> Result<(), lightning::ln::msgs::DecodeError> {
+/// 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.
+/// # Ok(())
+/// # }
+/// ```
+///
+/// [`Readable`]: crate::util::ser::Readable
+/// [`DecodeError`]: crate::ln::msgs::DecodeError
+/// [`Readable::read`]: crate::util::ser::Readable::read
+/// [`Read`]: crate::io::Read
+/// [`DecodeError::ShortRead`]: crate::ln::msgs::DecodeError::ShortRead
+#[macro_export]
+macro_rules! decode_tlv_stream {
+ ($stream: expr, {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*}) => {
+ let rewind = |_, _| { unreachable!() };
+ $crate::_decode_tlv_stream_range!($stream, .., rewind, {$(($type, $field, $fieldty)),*});
+ }
+}
+
+/// Similar to [`decode_tlv_stream`] with a custom TLV decoding capabilities.
+///
+/// `$decode_custom_tlv` is a closure that may be optionally provided to handle custom message types.
+/// If it is provided, it will be called with the custom type and the [`FixedLengthReader`] containing
+/// the message contents. It should return `Ok(true)` if the custom message is successfully parsed,
+/// `Ok(false)` if the message type is unknown, and `Err(`[`DecodeError`]`)` if parsing fails.
+///
+/// [`FixedLengthReader`]: crate::util::ser::FixedLengthReader
+/// [`DecodeError`]: crate::ln::msgs::DecodeError
+macro_rules! decode_tlv_stream_with_custom_tlv_decode {
+ ($stream: expr, {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*}
+ $(, $decode_custom_tlv: expr)?) => { {
+ let rewind = |_, _| { unreachable!() };
+ _decode_tlv_stream_range!(
+ $stream, .., rewind, {$(($type, $field, $fieldty)),*} $(, $decode_custom_tlv)?
+ );
+ } }
+}
+
+#[doc(hidden)]
+#[macro_export]
+macro_rules! _decode_tlv_stream_range {
+ ($stream: expr, $range: expr, $rewind: ident, {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*}
+ $(, $decode_custom_tlv: expr)?) => { {
+ use $crate::ln::msgs::DecodeError;
+ let mut last_seen_type: Option<u64> = None;
+ let mut stream_ref = $stream;
+ 'tlv_read: loop {
+ use $crate::util::ser;
+
+ // First decode the type of this TLV:
+ let typ: ser::BigSize = {
+ // We track whether any bytes were read during the consensus_decode call to
+ // determine whether we should break or return ShortRead if we get an
+ // UnexpectedEof. This should in every case be largely cosmetic, but its nice to
+ // pass the TLV test vectors exactly, which require this distinction.
+ let mut tracking_reader = ser::ReadTrackingReader::new(&mut stream_ref);
+ match <$crate::util::ser::BigSize as $crate::util::ser::Readable>::read(&mut tracking_reader) {
+ Err(DecodeError::ShortRead) => {
+ if !tracking_reader.have_read {
+ break 'tlv_read;
+ } else {
+ return Err(DecodeError::ShortRead);
+ }
+ },
+ Err(e) => return Err(e),
+ Ok(t) => if core::ops::RangeBounds::contains(&$range, &t.0) { t } else {
+ drop(tracking_reader);
+
+ // Assumes the type id is minimally encoded, which is enforced on read.
+ use $crate::util::ser::Writeable;
+ let bytes_read = t.serialized_length();
+ $rewind(stream_ref, bytes_read);
+ break 'tlv_read;
+ },