Implement Readable/Writeable for HashSet

To be used in upcoming commits for MPP ID storage
This commit is contained in:
Valentine Wallace 2021-09-12 20:46:11 -04:00
parent c986e52ce8
commit 641e20763f
No known key found for this signature in database
GPG key ID: F88EC43B95E601B8

View 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]