Skip to content

Commit

Permalink
serde: add default deserializers implementations (#233)
Browse files Browse the repository at this point in the history
* serde: add default deserializers implementations
* serde: add support for tuples
* serde: add support for arrays
  • Loading branch information
hackaugusto authored Dec 6, 2023
1 parent 2cef735 commit 271c19d
Showing 1 changed file with 232 additions and 18 deletions.
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,)
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]> {
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,)
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)
}
}

0 comments on commit 271c19d

Please sign in to comment.