From b63becdc93697d4257cf0cffb9677bcb97ba5998 Mon Sep 17 00:00:00 2001 From: Patrick Roy Date: Thu, 8 Dec 2022 15:40:31 +0000 Subject: [PATCH 1/2] Implement versionize for arrays via const generics Signed-off-by: Patrick Roy --- src/primitives.rs | 84 +++++++++++++++++------------------------------ 1 file changed, 31 insertions(+), 53 deletions(-) diff --git a/src/primitives.rs b/src/primitives.rs index a6ea4bf..3d7b3fc 100644 --- a/src/primitives.rs +++ b/src/primitives.rs @@ -114,66 +114,44 @@ impl Versionize for String { 1 } } + +impl Versionize for [T; N] +where + T: Copy + Default + Versionize, +{ + #[inline] + fn serialize( + &self, + writer: &mut W, + version_map: &VersionMap, + app_version: u16, + ) -> VersionizeResult<()> { + for element in self { + element.serialize(writer, version_map, app_version)?; + } -macro_rules! impl_versionize_array_with_size { - ($ty:literal) => { - impl Versionize for [T; $ty] - where - T: Copy + Default + Versionize, - { - #[inline] - fn serialize( - &self, - writer: &mut W, - version_map: &VersionMap, - app_version: u16, - ) -> VersionizeResult<()> { - for element in self { - element.serialize(writer, version_map, app_version)?; - } - - Ok(()) - } - - #[inline] - fn deserialize( - reader: &mut R, - version_map: &VersionMap, - app_version: u16, - ) -> VersionizeResult { - let mut array = [T::default(); $ty]; - for i in 0..$ty { - array[i] = T::deserialize(reader, version_map, app_version)?; - } - Ok(array) - } + Ok(()) + } - // Not used yet. - fn version() -> u16 { - 1 - } + #[inline] + fn deserialize( + reader: &mut R, + version_map: &VersionMap, + app_version: u16, + ) -> VersionizeResult { + let mut array = [T::default(); N]; + for i in 0..N { + array[i] = T::deserialize(reader, version_map, app_version)?; } - }; -} + Ok(array) + } -// Conventionally, traits are available for primitive arrays only up to size 32 -// until the const generics feature is implemented. -// [https://doc.rust-lang.org/std/primitive.array.html] -// [https://github.com/rust-lang/rust/issues/44580] -macro_rules! impl_versionize_arrays { - ($($N:literal)+) => { - $( - impl_versionize_array_with_size!($N); - )+ + // Not used yet. + fn version() -> u16 { + 1 } } -impl_versionize_arrays! { - 1 2 3 4 5 6 7 8 9 10 - 11 12 13 14 15 16 17 18 19 20 - 21 22 23 24 25 26 27 28 29 30 - 31 32 -} impl Versionize for Box where From be694102f6699a159f62b6c6ee317e9594682242 Mon Sep 17 00:00:00 2001 From: Patrick Roy Date: Fri, 23 Dec 2022 10:33:07 +0000 Subject: [PATCH 2/2] Remove superfluous "; 1" from vec! invocation Signed-off-by: Patrick Roy --- src/primitives.rs | 7 +++---- src/version_map.rs | 4 ++-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/primitives.rs b/src/primitives.rs index 3d7b3fc..f2c5e57 100644 --- a/src/primitives.rs +++ b/src/primitives.rs @@ -114,7 +114,7 @@ impl Versionize for String { 1 } } - + impl Versionize for [T; N] where T: Copy + Default + Versionize, @@ -140,8 +140,8 @@ where app_version: u16, ) -> VersionizeResult { let mut array = [T::default(); N]; - for i in 0..N { - array[i] = T::deserialize(reader, version_map, app_version)?; + for elem in &mut array { + *elem = T::deserialize(reader, version_map, app_version)?; } Ok(array) } @@ -152,7 +152,6 @@ where } } - impl Versionize for Box where T: Versionize, diff --git a/src/version_map.rs b/src/version_map.rs index 7fbc56e..9cfc1e4 100644 --- a/src/version_map.rs +++ b/src/version_map.rs @@ -89,7 +89,7 @@ pub struct VersionMap { impl Default for VersionMap { fn default() -> Self { VersionMap { - versions: vec![HashMap::new(); 1], + versions: vec![HashMap::new()], filter: Arc::new(()), } } @@ -104,7 +104,7 @@ impl VersionMap { /// Create a new version map with specified version filter. pub fn with_filter(filter: Arc) -> Self { VersionMap { - versions: vec![HashMap::new(); 1], + versions: vec![HashMap::new()], filter, } }