Skip to content

Commit 0dcc1a1

Browse files
committed
Implement Versionize for arrays with size up to 32
Signed-off-by: Ioana Chirca <[email protected]>
1 parent 94a2619 commit 0dcc1a1

File tree

4 files changed

+64
-2
lines changed

4 files changed

+64
-2
lines changed

coverage_config_aarch64.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"coverage_score": 92.8, "exclude_path": "", "crate_features": ""}
1+
{"coverage_score": 92.7, "exclude_path": "", "crate_features": ""}

coverage_config_x86_64.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"coverage_score": 93.0, "exclude_path": "", "crate_features": ""}
1+
{"coverage_score": 93.2, "exclude_path": "", "crate_features": ""}

src/primitives.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,66 @@ impl_versionize!(f64);
6262
impl_versionize!(char);
6363
impl_versionize!(String);
6464

65+
macro_rules! impl_versionize_array_with_size {
66+
($ty:literal) => {
67+
impl<T> Versionize for [T; $ty]
68+
where
69+
T: Copy + Default + Versionize,
70+
{
71+
#[inline]
72+
fn serialize<W: std::io::Write>(
73+
&self,
74+
writer: &mut W,
75+
version_map: &VersionMap,
76+
app_version: u16,
77+
) -> VersionizeResult<()> {
78+
for element in self {
79+
element.serialize(writer, version_map, app_version)?;
80+
}
81+
82+
Ok(())
83+
}
84+
85+
#[inline]
86+
fn deserialize<R: std::io::Read>(
87+
reader: &mut R,
88+
version_map: &VersionMap,
89+
app_version: u16,
90+
) -> VersionizeResult<Self> {
91+
let mut array = [T::default(); $ty];
92+
for i in 0..$ty {
93+
array[i] = T::deserialize(reader, version_map, app_version)?;
94+
}
95+
Ok(array)
96+
}
97+
98+
// Not used yet.
99+
fn version() -> u16 {
100+
1
101+
}
102+
}
103+
};
104+
}
105+
106+
// Conventionally, traits are available for primitive arrays only up to size 32
107+
// until the const generics feature is implemented.
108+
// [https://doc.rust-lang.org/std/primitive.array.html]
109+
// [https://github.com/rust-lang/rust/issues/44580]
110+
macro_rules! impl_versionize_arrays {
111+
($($N:literal)+) => {
112+
$(
113+
impl_versionize_array_with_size!($N);
114+
)+
115+
}
116+
}
117+
118+
impl_versionize_arrays! {
119+
0 1 2 3 4 5 6 7 8 9
120+
10 11 12 13 14 15 16 17 18 19
121+
20 21 22 23 24 25 26 27 28 29
122+
30 31 32
123+
}
124+
65125
impl<T> Versionize for Box<T>
66126
where
67127
T: Versionize,

tests/test.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,11 +636,13 @@ fn test_versionize_struct_with_array() {
636636
struct TestStruct {
637637
a: [u32; SIZE],
638638
b: [u8; dummy_mod::SIZE],
639+
c: Option<[i16; SIZE]>,
639640
}
640641

641642
let test_struct = TestStruct {
642643
a: [1; SIZE],
643644
b: [2; dummy_mod::SIZE],
645+
c: Some([3; SIZE]),
644646
};
645647

646648
let mut mem = vec![0; 4096];

0 commit comments

Comments
 (0)