From: Valentine Wallace Date: Mon, 13 Sep 2021 00:46:11 +0000 (-0400) Subject: Implement Readable/Writeable for HashSet X-Git-Tag: v0.0.101~15^2~3 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=641e20763fb9ecacf936053435e60f42b7c8f32e;p=rust-lightning Implement Readable/Writeable for HashSet To be used in upcoming commits for MPP ID storage --- diff --git a/lightning/src/util/ser.rs b/lightning/src/util/ser.rs index 0b5036bd7..c76b70181 100644 --- a/lightning/src/util/ser.rs +++ b/lightning/src/util/ser.rs @@ -528,6 +528,36 @@ impl Readable for HashMap } } +// HashSet +impl Writeable for HashSet +where T: Writeable + Eq + Hash +{ + #[inline] + fn write(&self, w: &mut W) -> Result<(), io::Error> { + (self.len() as u16).write(w)?; + for item in self.iter() { + item.write(w)?; + } + Ok(()) + } +} + +impl Readable for HashSet +where T: Readable + Eq + Hash +{ + #[inline] + fn read(r: &mut R) -> Result { + let len: u16 = Readable::read(r)?; + let mut ret = HashSet::with_capacity(len as usize); + for _ in 0..len { + if !ret.insert(T::read(r)?) { + return Err(DecodeError::InvalidValue) + } + } + Ok(ret) + } +} + // Vectors impl Writeable for Vec { #[inline]