From 641e20763fb9ecacf936053435e60f42b7c8f32e Mon Sep 17 00:00:00 2001 From: Valentine Wallace Date: Sun, 12 Sep 2021 20:46:11 -0400 Subject: [PATCH] Implement Readable/Writeable for HashSet To be used in upcoming commits for MPP ID storage --- lightning/src/util/ser.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) 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] -- 2.39.5