mirror of
https://github.com/lightningdevkit/rust-lightning.git
synced 2025-02-24 23:08:36 +01:00
Implement Readable/Writeable for HashSet
To be used in upcoming commits for MPP ID storage
This commit is contained in:
parent
c986e52ce8
commit
641e20763f
1 changed files with 30 additions and 0 deletions
|
@ -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]
|
||||
|
|
Loading…
Add table
Reference in a new issue