Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

serde: add support for some of the alloc collections #253

Merged
merged 1 commit into from
Feb 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
Comment on lines +421 to +422
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the moment the copy is required, since there is no iterator API for theByteReader

}
}
Loading