From 36fa4742ac73c529337503a59161c71d41eb99cf Mon Sep 17 00:00:00 2001 From: "Augusto F. Hack" Date: Wed, 28 Feb 2024 17:58:28 +0100 Subject: [PATCH] serde: add support for some of the alloc collections --- utils/core/src/serde/mod.rs | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/utils/core/src/serde/mod.rs b/utils/core/src/serde/mod.rs index b79301ad4..8ee06482a 100644 --- a/utils/core/src/serde/mod.rs +++ b/utils/core/src/serde/mod.rs @@ -188,6 +188,27 @@ impl Serializable for [T; C] { } } +impl Serializable for Vec { + fn write_into(&self, target: &mut W) { + target.write_usize(self.len()); + target.write_many(self); + } +} + +impl Serializable for BTreeMap { + fn write_into(&self, target: &mut W) { + target.write_usize(self.len()); + target.write_many(self); + } +} + +impl Serializable for BTreeSet { + fn write_into(&self, target: &mut W) { + target.write_usize(self.len()); + target.write_many(self); + } +} + // DESERIALIZABLE // ================================================================================================ @@ -378,3 +399,26 @@ impl Deserializable for [T; C] { Ok(res) } } + +impl Deserializable for Vec { + fn read_from(source: &mut R) -> Result { + let len = source.read_usize()?; + source.read_many(len) + } +} + +impl Deserializable for BTreeMap { + fn read_from(source: &mut R) -> Result { + let len = source.read_usize()?; + let data = source.read_many(len)?; + Ok(BTreeMap::from_iter(data)) + } +} + +impl Deserializable for BTreeSet { + fn read_from(source: &mut R) -> Result { + let len = source.read_usize()?; + let data = source.read_many(len)?; + Ok(BTreeSet::from_iter(data)) + } +}