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

[feat] kv_typed: add key U64BE

parent b9ea8da3
Branches
No related tags found
1 merge request!1356[feat] kv_typed: add key U64BE
......@@ -83,4 +83,4 @@ macro_rules! impl_as_bytes_for_be_numbers {
}
)*};
}
impl_as_bytes_for_be_numbers!(U32BE);
impl_as_bytes_for_be_numbers!(U32BE, U64BE);
......@@ -73,7 +73,7 @@ macro_rules! impl_explorable_key_for_be_numbers {
}
)*};
}
impl_explorable_key_for_be_numbers!(U32BE);
impl_explorable_key_for_be_numbers!(U32BE, U64BE);
pub trait ExplorableValue: Sized {
fn from_explorer_str(source: &str) -> Result<Self, FromExplorerValueErr>;
......
......@@ -45,7 +45,7 @@ macro_rules! impl_from_bytes_for_be_numbers {
}
)*};
}
impl_from_bytes_for_be_numbers!((U32BE, u32));
impl_from_bytes_for_be_numbers!((U32BE, u32), (U64BE, u64));
impl FromBytes for String {
type Err = std::str::Utf8Error;
......
......@@ -52,6 +52,14 @@ impl<T> Key for T where
{
}
pub trait KeyZc: Key {
type Ref: Sized + zerocopy::AsBytes + zerocopy::FromBytes;
}
impl KeyZc for () {
type Ref = ();
}
#[derive(
Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, zerocopy::AsBytes, zerocopy::FromBytes,
)]
......@@ -64,14 +72,22 @@ impl From<&zerocopy::U32<byteorder::BigEndian>> for U32BE {
}
}
pub trait KeyZc: Key {
type Ref: Sized + zerocopy::AsBytes + zerocopy::FromBytes;
impl KeyZc for U32BE {
type Ref = zerocopy::U32<byteorder::BigEndian>;
}
impl KeyZc for () {
type Ref = ();
#[derive(
Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, zerocopy::AsBytes, zerocopy::FromBytes,
)]
#[repr(transparent)]
pub struct U64BE(pub u64);
impl From<&zerocopy::U64<byteorder::BigEndian>> for U64BE {
fn from(u32_zc: &zerocopy::U64<byteorder::BigEndian>) -> Self {
U64BE(u32_zc.get())
}
}
impl KeyZc for U32BE {
type Ref = zerocopy::U32<byteorder::BigEndian>;
impl KeyZc for U64BE {
type Ref = zerocopy::U64<byteorder::BigEndian>;
}
......@@ -85,7 +85,7 @@ pub mod prelude {
pub use crate::iter::{
keys::KvIterKeys, values::KvIterValues, EntryIter, KvIter, KvIterRefSlice, ResultIter,
};
pub use crate::key::{Key, KeyZc, U32BE};
pub use crate::key::{Key, KeyZc, U32BE, U64BE};
pub use crate::subscription::{NewSubscribers, Subscriber, Subscribers};
pub use crate::transactional_read::{TransactionalRead, TxColRo};
pub use crate::transactional_write::{DbTxCollectionRw, TransactionalWrite, TxColRw};
......
......@@ -9,7 +9,7 @@ db_schema!(
["c1", Col1, i32, String],
["c2", Col2, usize, ()],
["c3", Col3, U32BE, Vec<u128>],
["c4", Col4, u64, BTreeSet<u128>],
["c4", Col4, U64BE, BTreeSet<u128>],
]
);
......@@ -27,7 +27,7 @@ fn test_macro_db() {
("col1", "i32", "String"),
("col2", "usize", "()"),
("col3", "U32BE", "Vec<u128>"),
("col4", "u64", "BTreeSet<u128>")
("col4", "U64BE", "BTreeSet<u128>")
]
);
}
......@@ -120,8 +120,8 @@ fn test_db<B: Backend>(db: &TestV1Db<B>) -> KvResult<()> {
// Test get_ref_slice
db.col4_write()
.upsert(4, (&[3, 2, 4, 1]).iter().copied().collect())?;
db.col4().get_ref_slice(&4, |numbers| {
.upsert(U64BE(4), (&[3, 2, 4, 1]).iter().copied().collect())?;
db.col4().get_ref_slice(&U64BE(4), |numbers| {
assert_eq!(numbers, &[1, 2, 3, 4]);
Ok(())
})?;
......@@ -151,7 +151,7 @@ fn test_db<B: Backend>(db: &TestV1Db<B>) -> KvResult<()> {
assert_eq!(iter.collect::<KvResult<Vec<_>>>()?, vec![U32BE(4)]);
Ok::<(), KvError>(())
})?;
c4.get_ref_slice(&4, |numbers| {
c4.get_ref_slice(&U64BE(4), |numbers| {
assert_eq!(numbers, &[1, 2, 3, 4]);
Ok(())
})?;
......@@ -195,7 +195,7 @@ fn test_db<B: Backend>(db: &TestV1Db<B>) -> KvResult<()> {
Ok::<(), KvError>(())
})?;
c4.upsert(4, (&[7, 8, 6, 5]).iter().copied().collect());
c4.upsert(U64BE(4), (&[7, 8, 6, 5]).iter().copied().collect());
Ok(())
});
tres?;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment