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 default deserializers implementations #233

Merged
merged 4 commits into from
Dec 6, 2023
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
250 changes: 232 additions & 18 deletions utils/core/src/serde/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.

use super::{flatten_slice_elements, DeserializationError, Vec};
use super::{DeserializationError, Vec};

mod byte_reader;
pub use byte_reader::{ByteReader, SliceReader};
Expand Down Expand Up @@ -53,6 +53,90 @@ impl Serializable for () {
fn write_into<W: ByteWriter>(&self, _target: &mut W) {}
}

impl<T1> Serializable for (T1,)
irakliyk marked this conversation as resolved.
Show resolved Hide resolved
where
T1: Serializable,
{
fn write_into<W: ByteWriter>(&self, target: &mut W) {
self.0.write_into(target);
}
}

impl<T1, T2> Serializable for (T1, T2)
where
T1: Serializable,
T2: Serializable,
{
fn write_into<W: ByteWriter>(&self, target: &mut W) {
self.0.write_into(target);
self.1.write_into(target);
}
}

impl<T1, T2, T3> Serializable for (T1, T2, T3)
where
T1: Serializable,
T2: Serializable,
T3: Serializable,
{
fn write_into<W: ByteWriter>(&self, target: &mut W) {
self.0.write_into(target);
self.1.write_into(target);
self.2.write_into(target);
}
}

impl<T1, T2, T3, T4> Serializable for (T1, T2, T3, T4)
where
T1: Serializable,
T2: Serializable,
T3: Serializable,
T4: Serializable,
{
fn write_into<W: ByteWriter>(&self, target: &mut W) {
self.0.write_into(target);
self.1.write_into(target);
self.2.write_into(target);
self.3.write_into(target);
}
}

impl<T1, T2, T3, T4, T5> Serializable for (T1, T2, T3, T4, T5)
where
T1: Serializable,
T2: Serializable,
T3: Serializable,
T4: Serializable,
T5: Serializable,
{
fn write_into<W: ByteWriter>(&self, target: &mut W) {
self.0.write_into(target);
self.1.write_into(target);
self.2.write_into(target);
self.3.write_into(target);
self.4.write_into(target);
}
}

impl<T1, T2, T3, T4, T5, T6> Serializable for (T1, T2, T3, T4, T5, T6)
where
T1: Serializable,
T2: Serializable,
T3: Serializable,
T4: Serializable,
T5: Serializable,
T6: Serializable,
{
fn write_into<W: ByteWriter>(&self, target: &mut W) {
self.0.write_into(target);
self.1.write_into(target);
self.2.write_into(target);
self.3.write_into(target);
self.4.write_into(target);
self.5.write_into(target);
}
}

impl Serializable for u8 {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
target.write_u8(*self);
Expand Down Expand Up @@ -113,30 +197,15 @@ impl<T: Serializable> Serializable for &Vec<T> {
}
}

impl<T: Serializable, const N: usize> Serializable for Vec<[T; N]> {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a blanket implementation for impl<T, C> Serializable for [T; C]. Together with the existing blanked implementation for Vec<T>, &Vec<T>, and &[T], it made these 3 implementations obsolete.

fn write_into<W: ByteWriter>(&self, target: &mut W) {
let source = flatten_slice_elements(self);
T::write_batch_into(source, target);
}
}

impl<T: Serializable, const N: usize> Serializable for &Vec<[T; N]> {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
let source = flatten_slice_elements(self);
T::write_batch_into(source, target);
}
}

impl<T: Serializable> Serializable for &[T] {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
T::write_batch_into(self, target);
}
}

impl<T: Serializable, const N: usize> Serializable for &[[T; N]] {
impl<T: Serializable, const C: usize> Serializable for [T; C] {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
let source = flatten_slice_elements(self);
T::write_batch_into(source, target);
T::write_batch_into(self, target);
}
}

Expand Down Expand Up @@ -197,3 +266,148 @@ pub trait Deserializable: Sized {
Ok(result)
}
}

impl Deserializable for () {
fn read_from<R: ByteReader>(_source: &mut R) -> Result<Self, DeserializationError> {
Ok(())
}
}

impl<T1> Deserializable for (T1,)
irakliyk marked this conversation as resolved.
Show resolved Hide resolved
where
T1: Deserializable,
{
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let v1 = T1::read_from(source)?;
Ok((v1,))
}
}

impl<T1, T2> Deserializable for (T1, T2)
where
T1: Deserializable,
T2: Deserializable,
{
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let v1 = T1::read_from(source)?;
let v2 = T2::read_from(source)?;
Ok((v1, v2))
}
}

impl<T1, T2, T3> Deserializable for (T1, T2, T3)
where
T1: Deserializable,
T2: Deserializable,
T3: Deserializable,
{
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let v1 = T1::read_from(source)?;
let v2 = T2::read_from(source)?;
let v3 = T3::read_from(source)?;
Ok((v1, v2, v3))
}
}

impl<T1, T2, T3, T4> Deserializable for (T1, T2, T3, T4)
where
T1: Deserializable,
T2: Deserializable,
T3: Deserializable,
T4: Deserializable,
{
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let v1 = T1::read_from(source)?;
let v2 = T2::read_from(source)?;
let v3 = T3::read_from(source)?;
let v4 = T4::read_from(source)?;
Ok((v1, v2, v3, v4))
}
}

impl<T1, T2, T3, T4, T5> Deserializable for (T1, T2, T3, T4, T5)
where
T1: Deserializable,
T2: Deserializable,
T3: Deserializable,
T4: Deserializable,
T5: Deserializable,
{
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let v1 = T1::read_from(source)?;
let v2 = T2::read_from(source)?;
let v3 = T3::read_from(source)?;
let v4 = T4::read_from(source)?;
let v5 = T5::read_from(source)?;
Ok((v1, v2, v3, v4, v5))
}
}

impl<T1, T2, T3, T4, T5, T6> Deserializable for (T1, T2, T3, T4, T5, T6)
where
T1: Deserializable,
T2: Deserializable,
T3: Deserializable,
T4: Deserializable,
T5: Deserializable,
T6: Deserializable,
{
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let v1 = T1::read_from(source)?;
let v2 = T2::read_from(source)?;
let v3 = T3::read_from(source)?;
let v4 = T4::read_from(source)?;
let v5 = T5::read_from(source)?;
let v6 = T6::read_from(source)?;
Ok((v1, v2, v3, v4, v5, v6))
}
}

impl Deserializable for u8 {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
source.read_u8()
}
}

impl Deserializable for u16 {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
source.read_u16()
}
}

impl Deserializable for u32 {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
source.read_u32()
}
}

impl Deserializable for u64 {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
source.read_u64()
}
}

impl<T: Deserializable> Deserializable for Option<T> {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let contains = source.read_bool()?;

match contains {
true => Ok(Some(T::read_from(source)?)),
false => Ok(None),
}
}
}

impl<T: Deserializable, const C: usize> Deserializable for [T; C] {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let data: Vec<T> = T::read_batch_from(source, C)?;

// SAFETY: the call above only returns a Vec if there are `C` elements, this conversion
// always succeeds
let res = data.try_into().unwrap_or_else(|v: Vec<T>| {
panic!("Expected a Vec of length {} but it was {}", C, v.len())
});

Ok(res)
Comment on lines +403 to +411
Copy link
Collaborator

Choose a reason for hiding this comment

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

It is a bit unfortunate that we have to allocate a vector here to deserialize the data - but I wonder if the compiler is smart enough to figure out that this is not needed.

}
}
Loading