From 271c19dddafbfff386e04d6d49825f4e523b9189 Mon Sep 17 00:00:00 2001 From: Augusto Hack Date: Wed, 6 Dec 2023 22:28:31 +0100 Subject: [PATCH] serde: add default deserializers implementations (#233) * serde: add default deserializers implementations * serde: add support for tuples * serde: add support for arrays --- utils/core/src/serde/mod.rs | 250 +++++++++++++++++++++++++++++++++--- 1 file changed, 232 insertions(+), 18 deletions(-) diff --git a/utils/core/src/serde/mod.rs b/utils/core/src/serde/mod.rs index de5a39672..ed84ea3d2 100644 --- a/utils/core/src/serde/mod.rs +++ b/utils/core/src/serde/mod.rs @@ -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}; @@ -53,6 +53,90 @@ impl Serializable for () { fn write_into(&self, _target: &mut W) {} } +impl Serializable for (T1,) +where + T1: Serializable, +{ + fn write_into(&self, target: &mut W) { + self.0.write_into(target); + } +} + +impl Serializable for (T1, T2) +where + T1: Serializable, + T2: Serializable, +{ + fn write_into(&self, target: &mut W) { + self.0.write_into(target); + self.1.write_into(target); + } +} + +impl Serializable for (T1, T2, T3) +where + T1: Serializable, + T2: Serializable, + T3: Serializable, +{ + fn write_into(&self, target: &mut W) { + self.0.write_into(target); + self.1.write_into(target); + self.2.write_into(target); + } +} + +impl Serializable for (T1, T2, T3, T4) +where + T1: Serializable, + T2: Serializable, + T3: Serializable, + T4: Serializable, +{ + fn write_into(&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 Serializable for (T1, T2, T3, T4, T5) +where + T1: Serializable, + T2: Serializable, + T3: Serializable, + T4: Serializable, + T5: Serializable, +{ + fn write_into(&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 Serializable for (T1, T2, T3, T4, T5, T6) +where + T1: Serializable, + T2: Serializable, + T3: Serializable, + T4: Serializable, + T5: Serializable, + T6: Serializable, +{ + fn write_into(&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(&self, target: &mut W) { target.write_u8(*self); @@ -113,30 +197,15 @@ impl Serializable for &Vec { } } -impl Serializable for Vec<[T; N]> { - fn write_into(&self, target: &mut W) { - let source = flatten_slice_elements(self); - T::write_batch_into(source, target); - } -} - -impl Serializable for &Vec<[T; N]> { - fn write_into(&self, target: &mut W) { - let source = flatten_slice_elements(self); - T::write_batch_into(source, target); - } -} - impl Serializable for &[T] { fn write_into(&self, target: &mut W) { T::write_batch_into(self, target); } } -impl Serializable for &[[T; N]] { +impl Serializable for [T; C] { fn write_into(&self, target: &mut W) { - let source = flatten_slice_elements(self); - T::write_batch_into(source, target); + T::write_batch_into(self, target); } } @@ -197,3 +266,148 @@ pub trait Deserializable: Sized { Ok(result) } } + +impl Deserializable for () { + fn read_from(_source: &mut R) -> Result { + Ok(()) + } +} + +impl Deserializable for (T1,) +where + T1: Deserializable, +{ + fn read_from(source: &mut R) -> Result { + let v1 = T1::read_from(source)?; + Ok((v1,)) + } +} + +impl Deserializable for (T1, T2) +where + T1: Deserializable, + T2: Deserializable, +{ + fn read_from(source: &mut R) -> Result { + let v1 = T1::read_from(source)?; + let v2 = T2::read_from(source)?; + Ok((v1, v2)) + } +} + +impl Deserializable for (T1, T2, T3) +where + T1: Deserializable, + T2: Deserializable, + T3: Deserializable, +{ + fn read_from(source: &mut R) -> Result { + let v1 = T1::read_from(source)?; + let v2 = T2::read_from(source)?; + let v3 = T3::read_from(source)?; + Ok((v1, v2, v3)) + } +} + +impl Deserializable for (T1, T2, T3, T4) +where + T1: Deserializable, + T2: Deserializable, + T3: Deserializable, + T4: Deserializable, +{ + fn read_from(source: &mut R) -> Result { + 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 Deserializable for (T1, T2, T3, T4, T5) +where + T1: Deserializable, + T2: Deserializable, + T3: Deserializable, + T4: Deserializable, + T5: Deserializable, +{ + fn read_from(source: &mut R) -> Result { + 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 Deserializable for (T1, T2, T3, T4, T5, T6) +where + T1: Deserializable, + T2: Deserializable, + T3: Deserializable, + T4: Deserializable, + T5: Deserializable, + T6: Deserializable, +{ + fn read_from(source: &mut R) -> Result { + 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(source: &mut R) -> Result { + source.read_u8() + } +} + +impl Deserializable for u16 { + fn read_from(source: &mut R) -> Result { + source.read_u16() + } +} + +impl Deserializable for u32 { + fn read_from(source: &mut R) -> Result { + source.read_u32() + } +} + +impl Deserializable for u64 { + fn read_from(source: &mut R) -> Result { + source.read_u64() + } +} + +impl Deserializable for Option { + fn read_from(source: &mut R) -> Result { + let contains = source.read_bool()?; + + match contains { + true => Ok(Some(T::read_from(source)?)), + false => Ok(None), + } + } +} + +impl Deserializable for [T; C] { + fn read_from(source: &mut R) -> Result { + let data: Vec = 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| { + panic!("Expected a Vec of length {} but it was {}", C, v.len()) + }); + + Ok(res) + } +}