Skip to content

Commit

Permalink
serde: add support for some of the alloc collections (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
hackaugusto authored Feb 29, 2024
1 parent 069e517 commit 2e324e2
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions utils/core/src/serde/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,27 @@ impl<T: Serializable, const C: usize> Serializable for [T; C] {
}
}

impl<T: Serializable> Serializable for Vec<T> {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
target.write_usize(self.len());
target.write_many(self);
}
}

impl<K: Serializable, V: Serializable> Serializable for BTreeMap<K, V> {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
target.write_usize(self.len());
target.write_many(self);
}
}

impl<T: Serializable> Serializable for BTreeSet<T> {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
target.write_usize(self.len());
target.write_many(self);
}
}

// DESERIALIZABLE
// ================================================================================================

Expand Down Expand Up @@ -378,3 +399,26 @@ impl<T: Deserializable, const C: usize> Deserializable for [T; C] {
Ok(res)
}
}

impl<T: Deserializable> Deserializable for Vec<T> {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let len = source.read_usize()?;
source.read_many(len)
}
}

impl<K: Deserializable + Ord, V: Deserializable> Deserializable for BTreeMap<K, V> {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let len = source.read_usize()?;
let data = source.read_many(len)?;
Ok(BTreeMap::from_iter(data))
}
}

impl<T: Deserializable + Ord> Deserializable for BTreeSet<T> {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let len = source.read_usize()?;
let data = source.read_many(len)?;
Ok(BTreeSet::from_iter(data))
}
}

0 comments on commit 2e324e2

Please sign in to comment.