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

Impl versionize for statically sized arrays #1450

Merged
merged 3 commits into from
Aug 5, 2024
Merged
Changes from 1 commit
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
Next Next commit
feat(versionable): Add support for statically sized arrays
nsarlin-zama committed Aug 5, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit ed244d7cc615b2804d4e4602ba568fbf9945a110
49 changes: 48 additions & 1 deletion utils/tfhe-versionable/src/lib.rs
Original file line number Diff line number Diff line change
@@ -81,6 +81,12 @@ pub enum UnversionizeError {
from_type: String,
source: Box<dyn Error + Send + Sync>,
},

/// The length of a statically sized array is wrong
ArrayLength {
expected_size: usize,
found_size: usize,
},
}

impl Display for UnversionizeError {
@@ -97,6 +103,15 @@ impl Display for UnversionizeError {
Self::Conversion { from_type, source } => {
write!(f, "Failed to convert from {from_type}: {source}")
}
Self::ArrayLength {
expected_size,
found_size,
} => {
write!(
f,
"Expected array of size {expected_size}, found array of size {found_size}"
)
}
}
}
}
@@ -106,6 +121,7 @@ impl Error for UnversionizeError {
match self {
UnversionizeError::Upgrade { source, .. } => Some(source.as_ref()),
UnversionizeError::Conversion { source, .. } => Some(source.as_ref()),
UnversionizeError::ArrayLength { .. } => None,
}
}
}
@@ -262,7 +278,7 @@ impl<T: VersionizeVec + Clone> VersionizeOwned for Box<[T]> {
type VersionedOwned = T::VersionedVec;

fn versionize_owned(self) -> Self::VersionedOwned {
T::versionize_vec(self.iter().cloned().collect())
T::versionize_vec(self.to_vec())
}
}

@@ -312,6 +328,37 @@ impl<T: UnversionizeVec> Unversionize for Vec<T> {
}
}

// Since serde doesn't support arbitrary length arrays with const generics, the array
// is converted to a slice/vec.
impl<const N: usize, T: VersionizeSlice> Versionize for [T; N] {
type Versioned<'vers> = T::VersionedSlice<'vers> where T: 'vers;

fn versionize(&self) -> Self::Versioned<'_> {
T::versionize_slice(self)
}
}

impl<const N: usize, T: VersionizeVec + Clone> VersionizeOwned for [T; N] {
type VersionedOwned = T::VersionedVec;

fn versionize_owned(self) -> Self::VersionedOwned {
T::versionize_vec(self.to_vec())
}
}

impl<const N: usize, T: UnversionizeVec + Clone> Unversionize for [T; N] {
fn unversionize(versioned: Self::VersionedOwned) -> Result<Self, UnversionizeError> {
let v = T::unversionize_vec(versioned)?;
let boxed_slice = v.into_boxed_slice();
TryInto::<Box<[T; N]>>::try_into(boxed_slice)
.map(|array| *array)
.map_err(|slice| UnversionizeError::ArrayLength {
expected_size: N,
found_size: slice.len(),
})
}
}

impl Versionize for String {
type Versioned<'vers> = &'vers str;