From: Wilmer Paulino Date: Mon, 11 Mar 2024 17:21:43 +0000 (-0700) Subject: Support Vec serialization that include element length prefix X-Git-Tag: v0.0.123-beta~21^2~13 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=c096a2416116b8249c3d45deeff03b154742b1c7;p=rust-lightning Support Vec serialization that include element length prefix We add new macro alternatives to impl_writeable_for_vec/impl_readable_for_vec that add a length prefix to each element in the `Vec`. This is intended to be used over the existing macros when attempting to serialize a `Vec` with elements of variable lengths. --- diff --git a/lightning/src/util/ser.rs b/lightning/src/util/ser.rs index f333fa2ba..f6af5ce53 100644 --- a/lightning/src/util/ser.rs +++ b/lightning/src/util/ser.rs @@ -820,6 +820,49 @@ macro_rules! impl_for_vec { } } +// Alternatives to impl_writeable_for_vec/impl_readable_for_vec that add a length prefix to each +// element in the Vec. Intended to be used when elements have variable lengths. +macro_rules! impl_writeable_for_vec_with_element_length_prefix { + ($ty: ty $(, $name: ident)*) => { + impl<$($name : Writeable),*> Writeable for Vec<$ty> { + #[inline] + fn write(&self, w: &mut W) -> Result<(), io::Error> { + CollectionLength(self.len() as u64).write(w)?; + for elem in self.iter() { + CollectionLength(elem.serialized_length() as u64).write(w)?; + elem.write(w)?; + } + Ok(()) + } + } + } +} +macro_rules! impl_readable_for_vec_with_element_length_prefix { + ($ty: ty $(, $name: ident)*) => { + impl<$($name : Readable),*> Readable for Vec<$ty> { + #[inline] + fn read(r: &mut R) -> Result { + let len: CollectionLength = Readable::read(r)?; + let mut ret = Vec::with_capacity(cmp::min(len.0 as usize, MAX_BUF_SIZE / core::mem::size_of::<$ty>())); + for _ in 0..len.0 { + let elem_len: CollectionLength = Readable::read(r)?; + let mut elem_reader = FixedLengthReader::new(r, elem_len.0); + if let Some(val) = MaybeReadable::read(&mut elem_reader)? { + ret.push(val); + } + } + Ok(ret) + } + } + } +} +macro_rules! impl_for_vec_with_element_length_prefix { + ($ty: ty $(, $name: ident)*) => { + impl_writeable_for_vec_with_element_length_prefix!($ty $(, $name)*); + impl_readable_for_vec_with_element_length_prefix!($ty $(, $name)*); + } +} + impl Writeable for Vec { #[inline] fn write(&self, w: &mut W) -> Result<(), io::Error> {