]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Implement Readable/Writeable for HashSet
authorValentine Wallace <vwallace@protonmail.com>
Mon, 13 Sep 2021 00:46:11 +0000 (20:46 -0400)
committerValentine Wallace <vwallace@protonmail.com>
Fri, 17 Sep 2021 19:23:45 +0000 (15:23 -0400)
To be used in upcoming commits for MPP ID storage

lightning/src/util/ser.rs

index 0b5036bd749424dd4529cb23481d5e01236d6974..c76b701817b5d9eb1a5c2645f705241fe86e3ad9 100644 (file)
@@ -528,6 +528,36 @@ impl<K, V> Readable for HashMap<K, V>
        }
 }
 
+// HashSet
+impl<T> Writeable for HashSet<T>
+where T: Writeable + Eq + Hash
+{
+       #[inline]
+       fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
+               (self.len() as u16).write(w)?;
+               for item in self.iter() {
+                       item.write(w)?;
+               }
+               Ok(())
+       }
+}
+
+impl<T> Readable for HashSet<T>
+where T: Readable + Eq + Hash
+{
+       #[inline]
+       fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
+               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<u8> {
        #[inline]