Skip to content

Commit 641e207

Browse files
Implement Readable/Writeable for HashSet
To be used in upcoming commits for MPP ID storage
1 parent c986e52 commit 641e207

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

lightning/src/util/ser.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,36 @@ impl<K, V> Readable for HashMap<K, V>
528528
}
529529
}
530530

531+
// HashSet
532+
impl<T> Writeable for HashSet<T>
533+
where T: Writeable + Eq + Hash
534+
{
535+
#[inline]
536+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
537+
(self.len() as u16).write(w)?;
538+
for item in self.iter() {
539+
item.write(w)?;
540+
}
541+
Ok(())
542+
}
543+
}
544+
545+
impl<T> Readable for HashSet<T>
546+
where T: Readable + Eq + Hash
547+
{
548+
#[inline]
549+
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
550+
let len: u16 = Readable::read(r)?;
551+
let mut ret = HashSet::with_capacity(len as usize);
552+
for _ in 0..len {
553+
if !ret.insert(T::read(r)?) {
554+
return Err(DecodeError::InvalidValue)
555+
}
556+
}
557+
Ok(ret)
558+
}
559+
}
560+
531561
// Vectors
532562
impl Writeable for Vec<u8> {
533563
#[inline]

0 commit comments

Comments
 (0)