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

Implement versionize for arrays via const generics #47

Merged
merged 5 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
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
83 changes: 30 additions & 53 deletions src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,64 +115,41 @@ impl Versionize for String {
}
}

macro_rules! impl_versionize_array_with_size {
($ty:literal) => {
impl<T> Versionize for [T; $ty]
where
T: Copy + Default + Versionize,
{
#[inline]
fn serialize<W: std::io::Write>(
&self,
writer: &mut W,
version_map: &VersionMap,
app_version: u16,
) -> VersionizeResult<()> {
for element in self {
element.serialize(writer, version_map, app_version)?;
}

Ok(())
}
impl<T, const N: usize> Versionize for [T; N]
where
T: Copy + Default + Versionize,
{
#[inline]
fn serialize<W: std::io::Write>(
&self,
writer: &mut W,
version_map: &VersionMap,
app_version: u16,
) -> VersionizeResult<()> {
for element in self {
element.serialize(writer, version_map, app_version)?;
}

#[inline]
fn deserialize<R: std::io::Read>(
reader: &mut R,
version_map: &VersionMap,
app_version: u16,
) -> VersionizeResult<Self> {
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<R: std::io::Read>(
reader: &mut R,
version_map: &VersionMap,
app_version: u16,
) -> VersionizeResult<Self> {
let mut array = [T::default(); N];
for elem in &mut array {
*elem = T::deserialize(reader, version_map, app_version)?;
}
};
}

// 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);
)+
Ok(array)
}
}

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
// Not used yet.
fn version() -> u16 {
1
}
}

impl<T> Versionize for Box<T>
Expand Down
4 changes: 2 additions & 2 deletions src/version_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(()),
}
}
Expand All @@ -104,7 +104,7 @@ impl VersionMap {
/// Create a new version map with specified version filter.
pub fn with_filter(filter: Arc<dyn VersionFilter + Send + Sync>) -> Self {
VersionMap {
versions: vec![HashMap::new(); 1],
versions: vec![HashMap::new()],
filter,
}
}
Expand Down