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

Support into iterator #251

Merged
merged 2 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion utils/core/src/serde/byte_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ pub trait ByteWriter: Sized {
/// Serializes all `elements` and writes the resulting bytes into `self`.
///
/// This method does not write any metadata (e.g. number of serialized elements) into `self`.
fn write_many<S: Serializable>(&mut self, elements: &[S]) {
fn write_many<S, T>(&mut self, elements: T)
where
T: IntoIterator<Item = S>,
S: Serializable,
{
for element in elements {
element.write_into(self);
}
Expand Down
20 changes: 7 additions & 13 deletions utils/core/src/serde/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub use byte_writer::ByteWriter;
// ================================================================================================

/// Defines how to serialize `Self` into bytes.
pub trait Serializable: Sized {
pub trait Serializable {
// REQUIRED METHODS
// --------------------------------------------------------------------------------------------
/// Serializes `self` into bytes and writes these bytes into the `target`.
Expand All @@ -40,6 +40,12 @@ pub trait Serializable: Sized {
}
}

impl<T: Serializable> Serializable for &T {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
(*self).write_into(target)
}
}

impl Serializable for () {
fn write_into<W: ByteWriter>(&self, _target: &mut W) {}
}
Expand Down Expand Up @@ -176,18 +182,6 @@ impl<T: Serializable> Serializable for Option<T> {
}
}

impl<T: Serializable> Serializable for &Option<T> {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
match self {
Some(v) => {
target.write_bool(true);
v.write_into(target);
}
None => target.write_bool(false),
}
}
}

impl<T: Serializable, const C: usize> Serializable for [T; C] {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
target.write_many(self)
Expand Down
Loading