Skip to content
Snippets Groups Projects
Commit 079e9194 authored by Pascal Engélibert's avatar Pascal Engélibert :bicyclist:
Browse files

fix(kv_typed): BTreeSet<T: zerocopy::Unaligned>

parent eebd1685
No related branches found
No related tags found
No related merge requests found
Pipeline #13073 passed
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aho-corasick"
version = "0.7.15"
......@@ -688,8 +690,7 @@ checksum = "4bb454f0228b18c7f4c3b0ebbee346ed9c52e7443b0999cd543ff3571205701d"
[[package]]
name = "dubp"
version = "0.58.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "429ad631310968b6f0ef8b0621ff685ff03a1f69304174c7d8e7dbed47ec68aa"
source = "git+https://git.duniter.org/libs/dubp-rs-libs#f4116b8d2b32ca7ad4e25100a2f5edd7da58edc6"
dependencies = [
"dubp-block",
"dubp-common",
......@@ -704,8 +705,7 @@ dependencies = [
[[package]]
name = "dubp-block"
version = "0.58.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8481be98e093e9f599ef685f8b2880ac273145ce4b12bae45a6fe28b158acb38"
source = "git+https://git.duniter.org/libs/dubp-rs-libs#f4116b8d2b32ca7ad4e25100a2f5edd7da58edc6"
dependencies = [
"dubp-documents",
"dubp-documents-parser",
......@@ -719,8 +719,7 @@ dependencies = [
[[package]]
name = "dubp-common"
version = "0.58.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab01b4870cd960917b638dfd951996dcb19850fdc9868a46d81c7a48c1882f2a"
source = "git+https://git.duniter.org/libs/dubp-rs-libs#f4116b8d2b32ca7ad4e25100a2f5edd7da58edc6"
dependencies = [
"dup-crypto",
"serde",
......@@ -732,8 +731,7 @@ dependencies = [
[[package]]
name = "dubp-documents"
version = "0.58.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38c2667a2c02b657ecce47a2f8d421ddd8f193a77277518aa74585bd42303246"
source = "git+https://git.duniter.org/libs/dubp-rs-libs#f4116b8d2b32ca7ad4e25100a2f5edd7da58edc6"
dependencies = [
"beef",
"dubp-wallet",
......@@ -746,8 +744,7 @@ dependencies = [
[[package]]
name = "dubp-documents-parser"
version = "0.58.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9597df5d36e1e269802341908d8970d58ccde6162b402748f8b8b1bd50c1bec6"
source = "git+https://git.duniter.org/libs/dubp-rs-libs#f4116b8d2b32ca7ad4e25100a2f5edd7da58edc6"
dependencies = [
"dubp-documents",
"json-pest-parser",
......@@ -760,8 +757,7 @@ dependencies = [
[[package]]
name = "dubp-wallet"
version = "0.58.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d044529bbf5928e9519b9040516365e6e20f2d65f8c8e565930f5561b8ff0126"
source = "git+https://git.duniter.org/libs/dubp-rs-libs#f4116b8d2b32ca7ad4e25100a2f5edd7da58edc6"
dependencies = [
"byteorder",
"dubp-common",
......@@ -797,8 +793,7 @@ dependencies = [
[[package]]
name = "duniter-bda-types"
version = "0.58.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a04e49bc24390430f986d07c43a5cafd431ded3954d31d554efd6c34b77c2788"
source = "git+https://git.duniter.org/libs/dubp-rs-libs#f4116b8d2b32ca7ad4e25100a2f5edd7da58edc6"
dependencies = [
"arrayvec 0.7.0",
"bincode",
......@@ -930,8 +925,7 @@ dependencies = [
[[package]]
name = "duniter-peer"
version = "0.58.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "952ebba568abb1a8a1a7f83ff960a88b4c1de9cd29c74c6885ecc83ca56cccaf"
source = "git+https://git.duniter.org/libs/dubp-rs-libs#f4116b8d2b32ca7ad4e25100a2f5edd7da58edc6"
dependencies = [
"beef",
"dubp-common",
......@@ -945,8 +939,7 @@ dependencies = [
[[package]]
name = "dup-crypto"
version = "0.58.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f74bc2e8282dd156cd62e54686bc77a22dbaa711facac93b27d6f2c72ff5d07a"
source = "git+https://git.duniter.org/libs/dubp-rs-libs#f4116b8d2b32ca7ad4e25100a2f5edd7da58edc6"
dependencies = [
"base64",
"blake3",
......
......@@ -43,7 +43,7 @@ members = [
]
[patch.crates-io]
#dubp = { git = "https://git.duniter.org/libs/dubp-rs-libs" }
dubp = { git = "https://git.duniter.org/libs/dubp-rs-libs" }
#dubp = { path = "../dubp-rs-libs" }
......
......@@ -17,7 +17,16 @@ use crate::*;
use std::{collections::HashMap, ops::Deref};
#[derive(
Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, zerocopy::AsBytes, zerocopy::FromBytes,
Clone,
Copy,
Debug,
Eq,
Ord,
PartialEq,
PartialOrd,
zerocopy::AsBytes,
zerocopy::FromBytes,
zerocopy::Unaligned,
)]
#[repr(transparent)]
pub struct UtxoValV2([u8; 52]); // 16(SourceAmount) + 32(Hash) + 4(u32)
......
......@@ -96,7 +96,7 @@ where
impl<T> FromBytes for BTreeSet<T>
where
T: Copy + zerocopy::FromBytes + Ord,
T: Copy + zerocopy::FromBytes + Ord + zerocopy::Unaligned,
{
type Err = LayoutVerifiedErr;
......@@ -104,7 +104,7 @@ where
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_unaligned(bytes)
.ok_or(LayoutVerifiedErr(stringify!(BTreeSet<T>)))?;
let slice = layout_verified.into_slice();
Ok(slice.iter().copied().collect())
......@@ -145,8 +145,24 @@ mod tests {
#[test]
fn test_btreeset_from_bytes() -> Result<(), LayoutVerifiedErr> {
assert_eq!(
<BTreeSet<u64> as FromBytes>::from_bytes(&[])?,
BTreeSet::<u64>::new()
<BTreeSet<U64UBE> as FromBytes>::from_bytes(&[])?,
BTreeSet::<U64UBE>::new()
);
assert_eq!(
<BTreeSet<U64UBE> as FromBytes>::from_bytes(&<BTreeSet<U64UBE> as AsBytes>::as_bytes(
&BTreeSet::<U64UBE>::new(),
|b| b.to_vec()
))?,
BTreeSet::<U64UBE>::new()
);
let mut my_btreeset = BTreeSet::<U64UBE>::new();
my_btreeset.insert(U64UBE::new(5));
assert_eq!(
<BTreeSet<U64UBE> as FromBytes>::from_bytes(&<BTreeSet<U64UBE> as AsBytes>::as_bytes(
&my_btreeset,
|b| b.to_vec()
))?,
my_btreeset
);
Ok(())
}
......
......@@ -91,6 +91,7 @@ pub mod prelude {
pub use crate::transactional_read::{TransactionalRead, TxColRo};
pub use crate::transactional_write::{DbTxCollectionRw, TransactionalWrite, TxColRw};
pub use crate::utils::arc::Arc;
pub use crate::utils::{U128UBE, U64UBE};
pub use crate::value::{Value, ValueSliceZc, ValueZc};
pub use crate::OwnedOrRef;
}
......
......@@ -15,6 +15,11 @@
//! KV Typed utils
use std::{
cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd},
convert::From,
};
pub mod arc;
#[cfg(not(feature = "sled_backend"))]
pub mod ivec;
......@@ -22,3 +27,80 @@ pub mod ivec;
pub mod ivec {
pub use sled::IVec;
}
macro_rules! num_unaligned {
(
$Wrapper:ident,
$Inner:ident,
$Std:ident
) => {
#[derive(
zerocopy::Unaligned,
PartialEq,
Eq,
Hash,
Debug,
Copy,
Clone,
zerocopy::FromBytes,
zerocopy::AsBytes,
Default,
)]
#[repr(transparent)]
pub struct $Wrapper(pub zerocopy::byteorder::$Inner<byteorder::BE>);
impl PartialOrd<$Wrapper> for $Wrapper {
fn partial_cmp(&self, other: &$Wrapper) -> Option<Ordering> {
self.0.get().partial_cmp(&other.0.get())
}
}
impl Ord for $Wrapper {
fn cmp(&self, other: &$Wrapper) -> Ordering {
self.0.get().cmp(&other.0.get())
}
}
impl PartialEq<$Std> for $Wrapper {
fn eq(&self, other: &$Std) -> bool {
self.0.get().eq(other)
}
}
impl From<$Std> for $Wrapper {
fn from(v: $Std) -> Self {
Self(zerocopy::byteorder::$Inner::new(v))
}
}
impl std::fmt::Display for $Wrapper {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl std::str::FromStr for $Wrapper {
type Err = std::num::ParseIntError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
$Std::from_str(s).map(Self::new)
}
}
impl $Wrapper {
pub fn new(v: $Std) -> Self {
Self(zerocopy::byteorder::$Inner::new(v))
}
}
impl From<$Wrapper> for $Std {
fn from(v: $Wrapper) -> Self {
v.0.get()
}
}
};
}
num_unaligned! {U128UBE, U128, u128}
num_unaligned! {U64UBE, U64, u64}
num_unaligned! {U32UBE, U32, u32}
num_unaligned! {U16UBE, U16, u16}
......@@ -144,7 +144,8 @@ where
+ Sized
+ Sync
+ zerocopy::AsBytes
+ zerocopy::FromBytes,
+ zerocopy::FromBytes
+ zerocopy::Unaligned,
E: Error + Send + Sync + 'static,
{
type Elem = T;
......
......@@ -8,7 +8,7 @@ db_schema!(
["c1", Col1, i32, String],
["c2", Col2, usize, ()],
["c3", Col3, U32BE, Vec<u128>],
["c4", Col4, U64BE, BTreeSet<u128>],
["c4", Col4, U64BE, BTreeSet<U128UBE>],
]
);
......@@ -24,7 +24,7 @@ fn test_macro_db() {
("col1", "i32", "String"),
("col2", "usize", "()"),
("col3", "U32BE", "Vec<u128>"),
("col4", "U64BE", "BTreeSet<u128>")
("col4", "U64BE", "BTreeSet<U128UBE>")
]
);
}
......@@ -118,11 +118,13 @@ fn test_db<B: Backend>(db: &TestV1Db<B>) -> KvResult<()> {
// Test get_ref_slice
db.col4_write().upsert(U64BE(3), BTreeSet::new())?;
db.col4().get_ref_slice(&U64BE(3), |numbers| {
assert_eq!(numbers, &[]);
assert_eq!(numbers, &[] as &[U128UBE]);
Ok(())
})?;
db.col4_write()
.upsert(U64BE(4), (&[3, 2, 4, 1]).iter().copied().collect())?;
db.col4_write().upsert(
U64BE(4),
(&[3, 2, 4, 1]).iter().map(|&i| U128UBE::new(i)).collect(),
)?;
db.col4().get_ref_slice(&U64BE(4), |numbers| {
assert_eq!(numbers, &[1, 2, 3, 4]);
Ok(())
......@@ -197,7 +199,10 @@ fn test_db<B: Backend>(db: &TestV1Db<B>) -> KvResult<()> {
Ok::<(), KvError>(())
})?;
c4.upsert(U64BE(4), (&[7, 8, 6, 5]).iter().copied().collect());
c4.upsert(
U64BE(4),
(&[7, 8, 6, 5]).iter().map(|&i| U128UBE::new(i)).collect(),
);
Ok(())
});
tres?;
......@@ -209,9 +214,10 @@ fn test_db<B: Backend>(db: &TestV1Db<B>) -> KvResult<()> {
// Test transactional 2
db.write(|mut db_tx| {
db_tx
.col4
.upsert(U64BE(47), (&[5, 9, 3, 2]).iter().copied().collect());
db_tx.col4.upsert(
U64BE(47),
(&[5, 9, 3, 2]).iter().map(|&i| U128UBE::new(i)).collect(),
);
Ok(())
})?;
db.col4().get_ref_slice(&U64BE(47), |numbers| {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment