Skip to content
Snippets Groups Projects
Commit 624b09ec authored by Éloïs's avatar Éloïs
Browse files

fix(kv_typed): deser empty BTreeSet

parent 56dd9795
No related branches found
No related tags found
No related merge requests found
Pipeline #12707 failed
...@@ -61,6 +61,20 @@ where ...@@ -61,6 +61,20 @@ where
} }
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_btreeset_as_bytes() {
BTreeSet::<u64>::new().as_bytes(|bytes| assert_eq!(bytes, &[]));
}
#[test]
fn test_hashset_as_bytes() {
HashSet::<u64>::new().as_bytes(|bytes| assert_eq!(bytes, &[]));
}
}
macro_rules! impl_as_bytes_for_le_numbers { macro_rules! impl_as_bytes_for_le_numbers {
($($T:ty),*) => {$( ($($T:ty),*) => {$(
impl AsBytes for $T { impl AsBytes for $T {
......
...@@ -97,12 +97,16 @@ where ...@@ -97,12 +97,16 @@ where
type Err = LayoutVerifiedErr; type Err = LayoutVerifiedErr;
fn from_bytes(bytes: &[u8]) -> Result<Self, Self::Err> { fn from_bytes(bytes: &[u8]) -> Result<Self, Self::Err> {
if bytes.is_empty() {
Ok(Self::new())
} else {
let layout_verified = zerocopy::LayoutVerified::<_, [T]>::new_slice(bytes) let layout_verified = zerocopy::LayoutVerified::<_, [T]>::new_slice(bytes)
.ok_or(LayoutVerifiedErr(stringify!(BTreeSet<T>)))?; .ok_or(LayoutVerifiedErr(stringify!(BTreeSet<T>)))?;
let slice = layout_verified.into_slice(); let slice = layout_verified.into_slice();
Ok(slice.iter().copied().collect()) Ok(slice.iter().copied().collect())
} }
} }
}
impl<T> FromBytes for HashSet<T> impl<T> FromBytes for HashSet<T>
where where
...@@ -111,12 +115,16 @@ where ...@@ -111,12 +115,16 @@ where
type Err = LayoutVerifiedErr; type Err = LayoutVerifiedErr;
fn from_bytes(bytes: &[u8]) -> Result<Self, Self::Err> { fn from_bytes(bytes: &[u8]) -> Result<Self, Self::Err> {
if bytes.is_empty() {
Ok(Self::new())
} else {
let layout_verified = zerocopy::LayoutVerified::<_, [T]>::new_slice(bytes) let layout_verified = zerocopy::LayoutVerified::<_, [T]>::new_slice(bytes)
.ok_or(LayoutVerifiedErr(stringify!(HashSet<T>)))?; .ok_or(LayoutVerifiedErr(stringify!(HashSet<T>)))?;
let slice = layout_verified.into_slice(); let slice = layout_verified.into_slice();
Ok(slice.iter().copied().collect()) Ok(slice.iter().copied().collect())
} }
} }
}
impl FromBytes for IVec { impl FromBytes for IVec {
type Err = std::convert::Infallible; type Err = std::convert::Infallible;
...@@ -125,3 +133,25 @@ impl FromBytes for IVec { ...@@ -125,3 +133,25 @@ impl FromBytes for IVec {
Ok(Self::from(bytes)) Ok(Self::from(bytes))
} }
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_btreeset_from_bytes() -> Result<(), LayoutVerifiedErr> {
assert_eq!(
<BTreeSet<u64> as FromBytes>::from_bytes(&[])?,
BTreeSet::<u64>::new()
);
Ok(())
}
#[test]
fn test_hashset_from_bytes() -> Result<(), LayoutVerifiedErr> {
assert_eq!(
<HashSet<u64> as FromBytes>::from_bytes(&[])?,
HashSet::<u64>::new()
);
Ok(())
}
}
...@@ -116,6 +116,11 @@ fn test_db<B: Backend>(db: &TestV1Db<B>) -> KvResult<()> { ...@@ -116,6 +116,11 @@ fn test_db<B: Backend>(db: &TestV1Db<B>) -> KvResult<()> {
})?; })?;
// Test get_ref_slice // Test get_ref_slice
db.col4_write().upsert(U64BE(3), BTreeSet::new())?;
db.col4().get_ref_slice(&U64BE(3), |numbers| {
assert_eq!(numbers, &[]);
Ok(())
})?;
db.col4_write() db.col4_write()
.upsert(U64BE(4), (&[3, 2, 4, 1]).iter().copied().collect())?; .upsert(U64BE(4), (&[3, 2, 4, 1]).iter().copied().collect())?;
db.col4().get_ref_slice(&U64BE(4), |numbers| { db.col4().get_ref_slice(&U64BE(4), |numbers| {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment