From e5e09d2d38c2f06b05096acfe08312fa7f163212 Mon Sep 17 00:00:00 2001 From: Al Liu Date: Sat, 14 Sep 2024 17:23:11 +0800 Subject: [PATCH] bumpup `among` version --- Cargo.toml | 2 +- src/lib.rs | 2 + src/swmr/generic.rs | 51 ++++++++----------------- src/swmr/generic/tests.rs | 2 + src/swmr/generic/traits/impls/bytes.rs | 16 +++++++- src/swmr/generic/traits/impls/string.rs | 12 +++--- 6 files changed, 41 insertions(+), 44 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 87edbdb2..afcafbaa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,7 +27,7 @@ test-swmr = ["std"] test-swmr-generic = ["std"] [dependencies] -among = { version = "0.1", default-features = false } +among = { version = "0.1", default-features = false, features = ["either"] } bitflags = { version = "1", default-features = false } dbutils = { version = "0.3", default-features = false, features = ["crc32fast"] } rarena-allocator = { version = "0.2", default-features = false, features = ["memmap"] } diff --git a/src/lib.rs b/src/lib.rs index 6d81fe4a..a6a5ae2f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,6 +8,8 @@ use core::{borrow::Borrow, cmp, marker::PhantomData, mem, slice}; +#[doc(inline)] +pub use among; use among::Among; use crossbeam_skiplist::SkipSet; use error::Error; diff --git a/src/swmr/generic.rs b/src/swmr/generic.rs index 44271cc6..aa3d30ee 100644 --- a/src/swmr/generic.rs +++ b/src/swmr/generic.rs @@ -46,14 +46,6 @@ pub struct Pointer { _m: PhantomData<(K, V)>, } -impl Clone for Pointer { - fn clone(&self) -> Self { - *self - } -} - -impl Copy for Pointer {} - impl PartialEq for Pointer { fn eq(&self, other: &Self) -> bool { self.as_key_slice() == other.as_key_slice() @@ -669,7 +661,7 @@ where opts: Options, open_options: OpenOptions, ) -> Result { - Self::map_mut_with_path_builder::<_, ()>(|| Ok(path.as_ref().to_path_buf()), opts, open_options) + Self::map_mut_with_path_builder::<_, ()>(|| dummy_path_builder(path), opts, open_options) .map_err(|e| e.unwrap_right()) } @@ -730,7 +722,7 @@ where path: P, opts: Options, ) -> Result, Error> { - Self::map_with_path_builder::<_, ()>(|| Ok(path.as_ref().to_path_buf()), opts) + Self::map_with_path_builder::<_, ()>(|| dummy_path_builder(path), opts) .map_err(|e| e.unwrap_right()) } @@ -910,7 +902,7 @@ where cks: S, ) -> Result { Self::map_mut_with_path_builder_and_checksumer::<_, ()>( - || Ok(path.as_ref().to_path_buf()), + || dummy_path_builder(path), opts, open_options, cks, @@ -1021,12 +1013,8 @@ where opts: Options, cks: S, ) -> Result, Error> { - Self::map_with_path_builder_and_checksumer::<_, ()>( - || Ok(path.as_ref().to_path_buf()), - opts, - cks, - ) - .map_err(|e| e.unwrap_right()) + Self::map_with_path_builder_and_checksumer::<_, ()>(|| dummy_path_builder(path), opts, cks) + .map_err(|e| e.unwrap_right()) } /// Open a write-ahead log backed by a file backed memory map in read only mode with the given [`Checksumer`]. @@ -1220,9 +1208,7 @@ where Some(e) => e, None => match self.insert_in(Among::Middle(key), Among::Right(value)) { Ok(_) => Either::Right(Ok(())), - Err(Among::Left(e)) => Either::Right(Err(Either::Left(e))), - Err(Among::Right(e)) => Either::Right(Err(Either::Right(e))), - _ => unreachable!(), + Err(e) => Either::Right(Err(e.into_left_right())), }, } } @@ -1247,9 +1233,7 @@ where Some(e) => e, None => match self.insert_in(Among::Right(key), Among::Middle(value)) { Ok(_) => Either::Right(Ok(())), - Err(Among::Middle(e)) => Either::Right(Err(Either::Left(e))), - Err(Among::Right(e)) => Either::Right(Err(Either::Right(e))), - _ => unreachable!(), + Err(e) => Either::Right(Err(e.into_middle_right())), }, } } @@ -1274,9 +1258,7 @@ where Some(e) => e, None => match self.insert_in(Among::Right(key), Among::Left(value())) { Ok(_) => Either::Right(Ok(())), - Err(Among::Middle(e)) => Either::Right(Err(Either::Left(e))), - Err(Among::Right(e)) => Either::Right(Err(Either::Right(e))), - _ => unreachable!(), + Err(e) => Either::Right(Err(e.into_middle_right())), }, } } @@ -1311,11 +1293,7 @@ where ) -> Result<(), Either> { self .insert_in(Among::Middle(key), Among::Right(value)) - .map_err(|e| match e { - Among::Left(e) => Either::Left(e), - Among::Right(e) => Either::Right(e), - _ => unreachable!(), - }) + .map_err(Among::into_left_right) } /// Inserts a key in bytes format and value in structured format into the write-ahead log directly. @@ -1479,11 +1457,7 @@ where ) -> Result<(), Either> { self .insert_in(Among::Right(key), Among::Middle(value)) - .map_err(|e| match e { - Among::Middle(e) => Either::Left(e), - Among::Right(e) => Either::Right(e), - _ => unreachable!(), - }) + .map_err(Among::into_middle_right) } fn insert_in( @@ -1577,3 +1551,8 @@ where ) } } + +#[inline] +fn dummy_path_builder(p: impl AsRef) -> Result { + Ok(p.as_ref().to_path_buf()) +} diff --git a/src/swmr/generic/tests.rs b/src/swmr/generic/tests.rs index 23e3b558..ad031dda 100644 --- a/src/swmr/generic/tests.rs +++ b/src/swmr/generic/tests.rs @@ -314,6 +314,8 @@ fn construct_map_file() { .insert(&person, &"My name is Alice!".to_string()) .unwrap(); assert_eq!(wal.get(&person).unwrap().value(), "My name is Alice!"); + + assert_eq!(*wal.path().unwrap().as_ref(), path); } let pr = PersonRef { diff --git a/src/swmr/generic/traits/impls/bytes.rs b/src/swmr/generic/traits/impls/bytes.rs index 9122b0d8..34c752fa 100644 --- a/src/swmr/generic/traits/impls/bytes.rs +++ b/src/swmr/generic/traits/impls/bytes.rs @@ -1,3 +1,4 @@ +use core::borrow::Borrow; use dbutils::equivalent::*; use std::{borrow::Cow, sync::Arc}; @@ -77,6 +78,12 @@ impl<'a> TypeRef<'a> for &'a [u8] { #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct SliceRef<'a>(&'a [u8]); +impl<'a> Borrow<[u8]> for SliceRef<'a> { + fn borrow(&self) -> &[u8] { + self.0 + } +} + impl<'a> From<&'a [u8]> for SliceRef<'a> { fn from(src: &'a [u8]) -> Self { Self(src) @@ -101,6 +108,13 @@ impl AsRef<[u8]> for SliceRef<'_> { } } +impl core::ops::Deref for SliceRef<'_> { + type Target = [u8]; + fn deref(&self) -> &Self::Target { + self.0 + } +} + impl PartialEq<[u8]> for SliceRef<'_> { fn eq(&self, other: &[u8]) -> bool { self.0 == other @@ -151,7 +165,7 @@ impl PartialEq> for &Vec { impls! { Cow<'_, [u8]>, - // &'static [u8] // TODO: implement this + &'static [u8], Vec, Box<[u8]>, Arc<[u8]>, diff --git a/src/swmr/generic/traits/impls/string.rs b/src/swmr/generic/traits/impls/string.rs index c6322ba3..b6092ff8 100644 --- a/src/swmr/generic/traits/impls/string.rs +++ b/src/swmr/generic/traits/impls/string.rs @@ -109,11 +109,11 @@ impl Borrow for Str<'_> { } } -impl<'a> Borrow<&'a str> for Str<'a> { - fn borrow(&self) -> &&'a str { - &self.0 - } -} +// impl<'a> Borrow<&'a str> for Str<'a> { +// fn borrow(&self) -> &&'a str { +// &self.0 +// } +// } impl core::ops::Deref for Str<'_> { type Target = str; @@ -197,7 +197,7 @@ impl PartialOrd> for &str { impls! { Cow<'_, str>, - // &'static str, TODO: add back this + &'static str, String, Arc, Box,