diff --git a/Cargo.toml b/Cargo.toml index f3b4f50..0485a24 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "orderwal" -version = "0.3.0" +version = "0.3.1" edition = "2021" repository = "https://github.com/al8n/orderwal" homepage = "https://github.com/al8n/orderwal" @@ -26,7 +26,7 @@ xxhash64 = ["dbutils/xxhash64", "std"] [dependencies] among = { version = "0.1", default-features = false, features = ["either"] } bitflags = { version = "1", default-features = false } -dbutils = { version = "0.3", default-features = false, features = ["crc32fast"] } +dbutils = { version = "0.4", default-features = false, features = ["crc32fast"] } rarena-allocator = { version = "0.3", default-features = false, features = ["memmap"] } crossbeam-skiplist = { version = "0.1", default-features = false, package = "crossbeam-skiplist-pr1132" } paste = "1" diff --git a/examples/zero_cost.rs b/examples/zero_cost.rs index a3b8767..f73d55e 100644 --- a/examples/zero_cost.rs +++ b/examples/zero_cost.rs @@ -84,7 +84,7 @@ impl<'a> KeyRef<'a, Person> for PersonRef<'a> { Comparable::compare(a, self).reverse() } - fn compare_binary(this: &[u8], other: &[u8]) -> cmp::Ordering { + unsafe fn compare_binary(this: &[u8], other: &[u8]) -> cmp::Ordering { let (this_id_size, this_id) = decode_u64_varint(this).unwrap(); let (other_id_size, other_id) = decode_u64_varint(other).unwrap(); @@ -107,10 +107,10 @@ impl Type for Person { encoded_u64_varint_len(self.id) + self.name.len() } - fn encode(&self, buf: &mut [u8]) -> Result<(), Self::Error> { + fn encode(&self, buf: &mut [u8]) -> Result { let id_size = encode_u64_varint(self.id, buf)?; buf[id_size..].copy_from_slice(self.name.as_bytes()); - Ok(()) + Ok(id_size + self.name.len()) } } diff --git a/src/entry.rs b/src/entry.rs index cd39aca..55198a8 100644 --- a/src/entry.rs +++ b/src/entry.rs @@ -278,13 +278,13 @@ impl Generic<'_, T> { } #[inline] - pub(crate) fn encode(&self, buf: &mut [u8]) -> Result<(), T::Error> { + pub(crate) fn encode(&self, buf: &mut [u8]) -> Result { match &self.data { Among::Left(val) => val.encode(buf), Among::Middle(val) => val.encode(buf), Among::Right(val) => { buf.copy_from_slice(val); - Ok(()) + Ok(buf.len()) } } } diff --git a/src/lib.rs b/src/lib.rs index f5360cc..24eaf76 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,7 +22,8 @@ extern crate std; pub use dbutils::{ checksum::{self, Crc32}, - Ascend, CheapClone, Comparator, Descend, + traits::{Ascend, Descend}, + CheapClone, Comparator, }; #[cfg(feature = "xxhash3")] diff --git a/src/pointer.rs b/src/pointer.rs index 9963a18..c114be0 100644 --- a/src/pointer.rs +++ b/src/pointer.rs @@ -137,7 +137,8 @@ where for<'a> K::Ref<'a>: KeyRef<'a, K>, { fn cmp(&self, other: &Self) -> cmp::Ordering { - as KeyRef>::compare_binary(self.as_key_slice(), other.as_key_slice()) + // SAFETY: WALs guarantee that the self and other must be the same as the result returned by `::encode`. + unsafe { as KeyRef>::compare_binary(self.as_key_slice(), other.as_key_slice()) } } } diff --git a/src/swmr/generic.rs b/src/swmr/generic.rs index 34817ba..c9db0ed 100644 --- a/src/swmr/generic.rs +++ b/src/swmr/generic.rs @@ -85,7 +85,7 @@ where for<'a> K::Ref<'a>: KeyRef<'a, K>, { fn cmp(&self, other: &Self) -> cmp::Ordering { - as KeyRef>::compare_binary(self.as_key_slice(), other.as_key_slice()) + unsafe { as KeyRef>::compare_binary(self.as_key_slice(), other.as_key_slice()) } } } diff --git a/src/swmr/generic/tests.rs b/src/swmr/generic/tests.rs index ddadbd6..11d08f3 100644 --- a/src/swmr/generic/tests.rs +++ b/src/swmr/generic/tests.rs @@ -121,7 +121,7 @@ impl<'a> KeyRef<'a, Person> for PersonRef<'a> { Comparable::compare(a, self).reverse() } - fn compare_binary(this: &[u8], other: &[u8]) -> cmp::Ordering { + unsafe fn compare_binary(this: &[u8], other: &[u8]) -> cmp::Ordering { let (this_id_size, this_id) = decode_u64_varint(this).unwrap(); let (other_id_size, other_id) = decode_u64_varint(other).unwrap(); PersonRef { @@ -143,10 +143,10 @@ impl Type for Person { encoded_u64_varint_len(self.id) + self.name.len() } - fn encode(&self, buf: &mut [u8]) -> Result<(), Self::Error> { + fn encode(&self, buf: &mut [u8]) -> Result { let id_size = encode_u64_varint(self.id, buf)?; buf[id_size..].copy_from_slice(self.name.as_bytes()); - Ok(()) + Ok(id_size + self.name.len()) } } diff --git a/src/swmr/wal.rs b/src/swmr/wal.rs index 3be71f8..37d4b01 100644 --- a/src/swmr/wal.rs +++ b/src/swmr/wal.rs @@ -4,12 +4,9 @@ use crate::{ error::Error, pointer::Pointer, wal::sealed::{Constructor, Sealed, WalCore}, - Options, -}; -use dbutils::{ - checksum::{BuildChecksumer, Crc32}, - Ascend, + Ascend, Options, }; +use dbutils::checksum::{BuildChecksumer, Crc32}; use rarena_allocator::{either::Either, Allocator}; pub use crate::{ diff --git a/src/wal/type.rs b/src/wal/type.rs index bcbb1cd..7c2b0b6 100644 --- a/src/wal/type.rs +++ b/src/wal/type.rs @@ -1,55 +1,9 @@ -use core::cmp; - use among::Among; -use dbutils::equivalent::Comparable; - -mod impls; -pub use impls::*; - -/// The type trait for limiting the types that can be used as keys and values in the [`GenericOrderWal`](crate::swmr::GenericOrderWal). -/// -/// This trait and its implementors can only be used with the [`GenericOrderWal`](crate::swmr::GenericOrderWal) type, otherwise -/// the correctness of the implementations is not guaranteed. -pub trait Type: core::fmt::Debug { - /// The reference type for the type. - type Ref<'a>: TypeRef<'a>; - - /// The error type for encoding the type into a binary format. - type Error; - - /// Returns the length of the encoded type size. - fn encoded_len(&self) -> usize; - - /// Encodes the type into a bytes slice, you can assume that the buf length is equal to the value returned by [`encoded_len`](Type::encoded_len). - fn encode(&self, buf: &mut [u8]) -> Result<(), Self::Error>; - - /// Encodes the type into a [`Vec`]. - #[inline] - fn encode_into_vec(&self) -> Result, Self::Error> { - let mut buf = vec![0; self.encoded_len()]; - self.encode(&mut buf)?; - Ok(buf) - } -} - -impl Type for &T { - type Ref<'a> = T::Ref<'a>; - type Error = T::Error; - - #[inline] - fn encoded_len(&self) -> usize { - T::encoded_len(*self) - } - - #[inline] - fn encode(&self, buf: &mut [u8]) -> Result<(), Self::Error> { - T::encode(*self, buf) - } -} +pub use dbutils::traits::{KeyRef, Type, TypeRef}; pub(crate) trait InsertAmongExt { fn encoded_len(&self) -> usize; - fn encode(&self, buf: &mut [u8]) -> Result<(), T::Error>; + fn encode(&self, buf: &mut [u8]) -> Result; } impl InsertAmongExt for Among { @@ -63,35 +17,14 @@ impl InsertAmongExt for Among { } #[inline] - fn encode(&self, buf: &mut [u8]) -> Result<(), T::Error> { + fn encode(&self, buf: &mut [u8]) -> Result { match self { Among::Left(t) => t.encode(buf), Among::Middle(t) => t.encode(buf), Among::Right(t) => { buf.copy_from_slice(t); - Ok(()) + Ok(buf.len()) } } } } - -/// The reference type trait for the [`Type`] trait. -pub trait TypeRef<'a>: core::fmt::Debug { - /// Creates a reference type from a binary slice, when using it with [`GenericOrderWal`](crate::swmr::GenericOrderWal), - /// you can assume that the slice is the same as the one returned by [`encode`](Type::encode). - /// - /// ## Safety - /// - the `src` must the same as the one returned by [`encode`](Type::encode). - unsafe fn from_slice(src: &'a [u8]) -> Self; -} - -/// The key reference trait for comparing `K` in the [`GenericOrderWal`](crate::swmr::GenericOrderWal). -pub trait KeyRef<'a, K>: Ord + Comparable { - /// Compares with a type `Q` which can be borrowed from [`T::Ref`](Type::Ref). - fn compare(&self, a: &Q) -> cmp::Ordering - where - Q: ?Sized + Ord + Comparable; - - /// Compares two binary formats of the `K` directly. - fn compare_binary(a: &[u8], b: &[u8]) -> cmp::Ordering; -} diff --git a/src/wal/type/impls.rs b/src/wal/type/impls.rs deleted file mode 100644 index 3781314..0000000 --- a/src/wal/type/impls.rs +++ /dev/null @@ -1,204 +0,0 @@ -use super::*; - -mod bytes; -pub use bytes::*; -mod string; -pub use string::Str; - -mod net; - -impl Type for () { - type Ref<'a> = (); - type Error = (); - - fn encoded_len(&self) -> usize { - 0 - } - - fn encode(&self, _buf: &mut [u8]) -> Result<(), Self::Error> { - Ok(()) - } -} - -impl TypeRef<'_> for () { - unsafe fn from_slice(_buf: &[u8]) -> Self {} -} - -impl Type for [u8; N] { - type Ref<'a> = Self; - - type Error = (); - - fn encoded_len(&self) -> usize { - N - } - - fn encode(&self, buf: &mut [u8]) -> Result<(), Self::Error> { - buf[..N].copy_from_slice(self.as_ref()); - Ok(()) - } -} - -impl TypeRef<'_> for [u8; N] { - #[inline] - unsafe fn from_slice(src: &'_ [u8]) -> Self { - let mut this = [0; N]; - this.copy_from_slice(src); - this - } -} - -macro_rules! impl_numbers { - ($($ty:ident), +$(,)?) => { - $( - impl Type for $ty { - type Ref<'a> = Self; - - type Error = (); - - #[inline] - fn encoded_len(&self) -> usize { - core::mem::size_of::<$ty>() - } - - #[inline] - fn encode(&self, buf: &mut [u8]) -> Result<(), Self::Error> { - const SIZE: usize = core::mem::size_of::<$ty>(); - Ok(buf[..SIZE].copy_from_slice(self.to_le_bytes().as_ref())) - } - } - - impl TypeRef<'_> for $ty { - #[inline] - unsafe fn from_slice(buf: &[u8]) -> Self { - const SIZE: usize = core::mem::size_of::<$ty>(); - - $ty::from_le_bytes(buf[..SIZE].try_into().unwrap()) - } - } - - impl KeyRef<'_, $ty> for $ty { - #[inline] - fn compare(&self, a: &Q) -> core::cmp::Ordering - where - Q: ?Sized + Ord + Comparable<$ty> { - Comparable::compare(a, self).reverse() - } - - #[inline] - fn compare_binary(a: &[u8], b: &[u8]) -> core::cmp::Ordering { - const SIZE: usize = core::mem::size_of::<$ty>(); - - let a = $ty::from_le_bytes(a[..SIZE].try_into().unwrap()); - let b = $ty::from_le_bytes(b[..SIZE].try_into().unwrap()); - - a.cmp(&b) - } - } - )* - }; -} - -impl_numbers!(i8, i16, i32, i64, i128, u8, u16, u32, u64, u128,); - -impl Type for f32 { - type Ref<'a> = Self; - - type Error = (); - - #[inline] - fn encoded_len(&self) -> usize { - core::mem::size_of::() - } - - #[inline] - fn encode(&self, buf: &mut [u8]) -> Result<(), Self::Error> { - const SIZE: usize = core::mem::size_of::(); - buf[..SIZE].copy_from_slice(self.to_le_bytes().as_ref()); - Ok(()) - } -} - -impl TypeRef<'_> for f32 { - #[inline] - unsafe fn from_slice(buf: &[u8]) -> Self { - const SIZE: usize = core::mem::size_of::(); - - f32::from_le_bytes(buf[..SIZE].try_into().unwrap()) - } -} - -impl Type for f64 { - type Ref<'a> = Self; - - type Error = (); - - #[inline] - fn encoded_len(&self) -> usize { - core::mem::size_of::() - } - - #[inline] - fn encode(&self, buf: &mut [u8]) -> Result<(), Self::Error> { - const SIZE: usize = core::mem::size_of::(); - buf[..SIZE].copy_from_slice(self.to_le_bytes().as_ref()); - Ok(()) - } -} - -impl TypeRef<'_> for f64 { - #[inline] - unsafe fn from_slice(buf: &[u8]) -> Self { - const SIZE: usize = core::mem::size_of::(); - - f64::from_le_bytes(buf[..SIZE].try_into().unwrap()) - } -} - -impl Type for bool { - type Ref<'a> = Self; - - type Error = (); - - #[inline] - fn encoded_len(&self) -> usize { - 1 - } - - #[inline] - fn encode(&self, buf: &mut [u8]) -> Result<(), Self::Error> { - buf[0] = *self as u8; - Ok(()) - } -} - -impl TypeRef<'_> for bool { - #[inline] - unsafe fn from_slice(buf: &[u8]) -> Self { - buf[0] != 0 - } -} - -impl Type for char { - type Ref<'a> = Self; - - type Error = (); - - #[inline] - fn encoded_len(&self) -> usize { - self.len_utf8() - } - - #[inline] - fn encode(&self, buf: &mut [u8]) -> Result<(), Self::Error> { - self.encode_utf8(buf); - Ok(()) - } -} - -impl TypeRef<'_> for char { - #[inline] - unsafe fn from_slice(buf: &[u8]) -> Self { - core::str::from_utf8_unchecked(buf).chars().next().unwrap() - } -} diff --git a/src/wal/type/impls/bytes.rs b/src/wal/type/impls/bytes.rs deleted file mode 100644 index 4461ed0..0000000 --- a/src/wal/type/impls/bytes.rs +++ /dev/null @@ -1,254 +0,0 @@ -use core::borrow::Borrow; -use dbutils::equivalent::*; -use std::{borrow::Cow, sync::Arc}; - -use super::*; - -macro_rules! impls { - ($( $(#[cfg($cfg:meta)])? $ty:ty),+ $(,)?) => { - $( - $(#[cfg($cfg)])? - const _: () = { - impl Type for $ty { - type Ref<'a> = SliceRef<'a>; - type Error = (); - - fn encoded_len(&self) -> usize { - self.len() - } - - fn encode(&self, buf: &mut [u8]) -> Result<(), Self::Error> { - buf.copy_from_slice(self.as_ref()); - Ok(()) - } - } - - impl<'a> KeyRef<'a, $ty> for SliceRef<'a> { - fn compare(&self, a: &Q) -> cmp::Ordering - where - Q: ?Sized + Ord + Comparable, - { - Comparable::compare(a, self).reverse() - } - - fn compare_binary(a: &[u8], b: &[u8]) -> cmp::Ordering { - a.cmp(b) - } - } - - impl Equivalent> for $ty { - fn equivalent(&self, key: &SliceRef<'_>) -> bool { - let this: &[u8] = self.as_ref(); - this.eq(key.0) - } - } - - impl Comparable> for $ty { - fn compare(&self, other: &SliceRef<'_>) -> cmp::Ordering { - let this: &[u8] = self.as_ref(); - this.cmp(other.0) - } - } - - impl Equivalent<$ty> for SliceRef<'_> { - fn equivalent(&self, key: &$ty) -> bool { - let that: &[u8] = key.as_ref(); - self.0.eq(that) - } - } - - impl Comparable<$ty> for SliceRef<'_> { - fn compare(&self, other: &$ty) -> cmp::Ordering { - let that: &[u8] = other.as_ref(); - self.0.cmp(that) - } - } - }; - )* - }; -} - -impl<'a> TypeRef<'a> for &'a [u8] { - unsafe fn from_slice(src: &'a [u8]) -> Self { - src - } -} - -/// A wrapper type for `&'a [u8]`. -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct SliceRef<'a>(&'a [u8]); - -impl Borrow<[u8]> for SliceRef<'_> { - fn borrow(&self) -> &[u8] { - self.0 - } -} - -impl<'a> From<&'a [u8]> for SliceRef<'a> { - fn from(src: &'a [u8]) -> Self { - Self(src) - } -} - -impl<'a> From> for &'a [u8] { - fn from(src: SliceRef<'a>) -> Self { - src.0 - } -} - -impl<'a> TypeRef<'a> for SliceRef<'a> { - unsafe fn from_slice(src: &'a [u8]) -> Self { - Self(src) - } -} - -impl AsRef<[u8]> for SliceRef<'_> { - fn as_ref(&self) -> &[u8] { - self.0 - } -} - -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 - } -} - -impl PartialEq<&[u8]> for SliceRef<'_> { - fn eq(&self, other: &&[u8]) -> bool { - self.0 == *other - } -} - -impl PartialEq> for [u8] { - fn eq(&self, other: &SliceRef<'_>) -> bool { - self == other.0 - } -} - -impl PartialEq> for &[u8] { - fn eq(&self, other: &SliceRef<'_>) -> bool { - *self == other.0 - } -} - -impl PartialEq> for SliceRef<'_> { - fn eq(&self, other: &Vec) -> bool { - self.0 == other.as_slice() - } -} - -impl PartialEq<&Vec> for SliceRef<'_> { - fn eq(&self, other: &&Vec) -> bool { - self.0 == other.as_slice() - } -} - -impl PartialEq> for Vec { - fn eq(&self, other: &SliceRef<'_>) -> bool { - self.as_slice() == other.0 - } -} - -impl PartialEq> for &Vec { - fn eq(&self, other: &SliceRef<'_>) -> bool { - self.as_slice() == other.0 - } -} - -impls! { - Cow<'_, [u8]>, - &'static [u8], - Vec, - Box<[u8]>, - Arc<[u8]>, - #[cfg(feature = "bytes")] - ::bytes::Bytes, - #[cfg(feature = "smallvec-wrapper")] - ::smallvec_wrapper::OneOrMore, - #[cfg(feature = "smallvec-wrapper")] - ::smallvec_wrapper::TinyVec, - #[cfg(feature = "smallvec-wrapper")] - ::smallvec_wrapper::TriVec, - #[cfg(feature = "smallvec-wrapper")] - ::smallvec_wrapper::SmallVec, - #[cfg(feature = "smallvec-wrapper")] - ::smallvec_wrapper::MediumVec, - #[cfg(feature = "smallvec-wrapper")] - ::smallvec_wrapper::LargeVec, - #[cfg(feature = "smallvec-wrapper")] - ::smallvec_wrapper::XLargeVec, - #[cfg(feature = "smallvec-wrapper")] - ::smallvec_wrapper::XXLargeVec, - #[cfg(feature = "smallvec-wrapper")] - ::smallvec_wrapper::XXXLargeVec, -} - -#[cfg(feature = "smallvec")] -const _: () = { - use smallvec::SmallVec; - - use super::*; - - impl Type for SmallVec<[u8; N]> { - type Ref<'a> = SliceRef<'a>; - type Error = (); - - fn encoded_len(&self) -> usize { - self.len() - } - - fn encode(&self, buf: &mut [u8]) -> Result<(), Self::Error> { - buf.copy_from_slice(self.as_ref()); - Ok(()) - } - } - - impl<'a, const N: usize> KeyRef<'a, SmallVec<[u8; N]>> for SliceRef<'a> { - fn compare(&self, a: &Q) -> cmp::Ordering - where - Q: ?Sized + Ord + Comparable, - { - Comparable::compare(a, self).reverse() - } - - fn compare_binary(a: &[u8], b: &[u8]) -> cmp::Ordering { - a.cmp(b) - } - } - - impl Equivalent> for SmallVec<[u8; N]> { - fn equivalent(&self, key: &SliceRef<'_>) -> bool { - let this: &[u8] = self.as_ref(); - this.eq(key.0) - } - } - - impl Comparable> for SmallVec<[u8; N]> { - fn compare(&self, other: &SliceRef<'_>) -> cmp::Ordering { - let this: &[u8] = self.as_ref(); - this.cmp(other.0) - } - } - - impl Equivalent> for SliceRef<'_> { - fn equivalent(&self, key: &SmallVec<[u8; N]>) -> bool { - let that: &[u8] = key.as_ref(); - self.0.eq(that) - } - } - - impl Comparable> for SliceRef<'_> { - fn compare(&self, other: &SmallVec<[u8; N]>) -> cmp::Ordering { - let that: &[u8] = other.as_ref(); - self.0.cmp(that) - } - } -}; diff --git a/src/wal/type/impls/net.rs b/src/wal/type/impls/net.rs deleted file mode 100644 index 95c0728..0000000 --- a/src/wal/type/impls/net.rs +++ /dev/null @@ -1,176 +0,0 @@ -use core::cmp; -use std::net::{Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6}; - -use super::{Comparable, KeyRef, Type, TypeRef}; - -impl Type for Ipv4Addr { - type Ref<'a> = Self; - - type Error = (); - - #[inline] - fn encoded_len(&self) -> usize { - 4 - } - - #[inline] - fn encode(&self, buf: &mut [u8]) -> Result<(), Self::Error> { - buf[..4].copy_from_slice(self.octets().as_ref()); - Ok(()) - } -} - -impl TypeRef<'_> for Ipv4Addr { - #[inline] - unsafe fn from_slice(buf: &[u8]) -> Self { - let octets = <[u8; 4]>::from_slice(&buf[..4]); - Ipv4Addr::from(octets) - } -} - -impl KeyRef<'_, Ipv4Addr> for Ipv4Addr { - fn compare(&self, a: &Q) -> cmp::Ordering - where - Q: ?Sized + Ord + Comparable, - { - Comparable::compare(a, self).reverse() - } - - fn compare_binary(a: &[u8], b: &[u8]) -> cmp::Ordering { - unsafe { - let a = ::from_slice(a); - let b = ::from_slice(b); - a.cmp(&b) - } - } -} - -impl Type for Ipv6Addr { - type Ref<'a> = Self; - - type Error = (); - - #[inline] - fn encoded_len(&self) -> usize { - 16 - } - - #[inline] - fn encode(&self, buf: &mut [u8]) -> Result<(), Self::Error> { - buf[..16].copy_from_slice(self.octets().as_ref()); - Ok(()) - } -} - -impl TypeRef<'_> for Ipv6Addr { - #[inline] - unsafe fn from_slice(buf: &[u8]) -> Self { - let octets = <[u8; 16]>::from_slice(&buf[..16]); - Ipv6Addr::from(octets) - } -} - -impl KeyRef<'_, Ipv6Addr> for Ipv6Addr { - fn compare(&self, a: &Q) -> cmp::Ordering - where - Q: ?Sized + Ord + Comparable, - { - Comparable::compare(a, self).reverse() - } - - fn compare_binary(a: &[u8], b: &[u8]) -> cmp::Ordering { - unsafe { - let a = ::from_slice(a); - let b = ::from_slice(b); - a.cmp(&b) - } - } -} - -impl Type for SocketAddrV4 { - type Ref<'a> = Self; - - type Error = (); - - #[inline] - fn encoded_len(&self) -> usize { - 6 - } - - #[inline] - fn encode(&self, buf: &mut [u8]) -> Result<(), Self::Error> { - buf[..4].copy_from_slice(self.ip().octets().as_ref()); - buf[4..6].copy_from_slice(&self.port().to_le_bytes()); - Ok(()) - } -} - -impl TypeRef<'_> for SocketAddrV4 { - #[inline] - unsafe fn from_slice(buf: &[u8]) -> Self { - let octets = <[u8; 4]>::from_slice(&buf[..4]); - let port = u16::from_le_bytes(buf[4..6].try_into().unwrap()); - SocketAddrV4::new(Ipv4Addr::from(octets), port) - } -} - -impl KeyRef<'_, SocketAddrV4> for SocketAddrV4 { - fn compare(&self, a: &Q) -> cmp::Ordering - where - Q: ?Sized + Ord + Comparable, - { - Comparable::compare(a, self).reverse() - } - - fn compare_binary(a: &[u8], b: &[u8]) -> cmp::Ordering { - unsafe { - let a = ::from_slice(a); - let b = ::from_slice(b); - a.cmp(&b) - } - } -} - -impl Type for SocketAddrV6 { - type Ref<'a> = Self; - - type Error = (); - - #[inline] - fn encoded_len(&self) -> usize { - 18 - } - - #[inline] - fn encode(&self, buf: &mut [u8]) -> Result<(), Self::Error> { - buf[..16].copy_from_slice(self.ip().octets().as_ref()); - buf[16..18].copy_from_slice(&self.port().to_le_bytes()); - Ok(()) - } -} - -impl TypeRef<'_> for SocketAddrV6 { - #[inline] - unsafe fn from_slice(buf: &[u8]) -> Self { - let octets = <[u8; 16]>::from_slice(&buf[..16]); - let port = u16::from_le_bytes(buf[16..18].try_into().unwrap()); - SocketAddrV6::new(Ipv6Addr::from(octets), port, 0, 0) - } -} - -impl KeyRef<'_, SocketAddrV6> for SocketAddrV6 { - fn compare(&self, a: &Q) -> cmp::Ordering - where - Q: ?Sized + Ord + Comparable, - { - Comparable::compare(a, self).reverse() - } - - fn compare_binary(a: &[u8], b: &[u8]) -> cmp::Ordering { - unsafe { - let a = ::from_slice(a); - let b = ::from_slice(b); - a.cmp(&b) - } - } -} diff --git a/src/wal/type/impls/string.rs b/src/wal/type/impls/string.rs deleted file mode 100644 index 7ab8c4c..0000000 --- a/src/wal/type/impls/string.rs +++ /dev/null @@ -1,202 +0,0 @@ -use core::borrow::Borrow; -use std::{borrow::Cow, sync::Arc}; - -use crossbeam_skiplist::Equivalent; - -use super::*; - -macro_rules! impls { - ($( $(#[cfg($cfg:meta)])? $ty:ty),+ $(,)?) => { - $( - $(#[cfg($cfg)])? - const _: () = { - impl Type for $ty { - type Ref<'a> = Str<'a>; - type Error = (); - - fn encoded_len(&self) -> usize { - self.len() - } - - fn encode(&self, buf: &mut [u8]) -> Result<(), Self::Error> { - buf.copy_from_slice(self.as_bytes()); - Ok(()) - } - } - - impl<'a> KeyRef<'a, $ty> for Str<'a> { - fn compare(&self, a: &Q) -> cmp::Ordering - where - Q: ?Sized + Ord + Comparable, - { - Comparable::compare(a, self).reverse() - } - - fn compare_binary(a: &[u8], b: &[u8]) -> cmp::Ordering { - a.cmp(b) - } - } - - impl Equivalent> for $ty { - fn equivalent(&self, key: &Str<'_>) -> bool { - let this: &str = self.as_ref(); - this.eq(key.0) - } - } - - impl Comparable> for $ty { - fn compare(&self, other: &Str<'_>) -> cmp::Ordering { - let this: &str = self.as_ref(); - this.cmp(other.0) - } - } - - impl Equivalent<$ty> for Str<'_> { - fn equivalent(&self, key: &$ty) -> bool { - let that: &str = key.as_ref(); - self.0.eq(that) - } - } - - impl Comparable<$ty> for Str<'_> { - fn compare(&self, other: &$ty) -> cmp::Ordering { - let that: &str = other.as_ref(); - self.0.cmp(that) - } - } - }; - )* - }; -} - -impl<'a> TypeRef<'a> for &'a str { - unsafe fn from_slice(src: &'a [u8]) -> Self { - core::str::from_utf8(src).unwrap() - } -} - -/// A wrapper type for `&'a str`. -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct Str<'a>(&'a str); - -impl<'a> From<&'a str> for Str<'a> { - fn from(src: &'a str) -> Self { - Self(src) - } -} - -impl<'a> From> for &'a str { - fn from(src: Str<'a>) -> Self { - src.0 - } -} - -impl<'a> TypeRef<'a> for Str<'a> { - unsafe fn from_slice(src: &'a [u8]) -> Self { - Self(core::str::from_utf8(src).unwrap()) - } -} - -impl AsRef for Str<'_> { - fn as_ref(&self) -> &str { - self.0 - } -} - -impl Borrow for Str<'_> { - fn borrow(&self) -> &str { - self.0 - } -} - -impl core::ops::Deref for Str<'_> { - type Target = str; - - fn deref(&self) -> &Self::Target { - self.0 - } -} - -impl PartialEq for Str<'_> { - fn eq(&self, other: &str) -> bool { - self.0 == other - } -} - -impl PartialEq for Str<'_> { - fn eq(&self, other: &String) -> bool { - self.0 == other - } -} - -impl PartialEq> for String { - fn eq(&self, other: &Str<'_>) -> bool { - self == other.0 - } -} - -impl PartialEq<&String> for Str<'_> { - fn eq(&self, other: &&String) -> bool { - self.0 == *other - } -} - -impl PartialEq> for &String { - fn eq(&self, other: &Str<'_>) -> bool { - *self == other.0 - } -} - -impl PartialEq> for str { - fn eq(&self, other: &Str<'_>) -> bool { - self == other.0 - } -} - -impl PartialEq<&str> for Str<'_> { - fn eq(&self, other: &&str) -> bool { - self.0 == *other - } -} - -impl PartialEq> for &str { - fn eq(&self, other: &Str<'_>) -> bool { - *self == other.0 - } -} - -impl PartialOrd for Str<'_> { - fn partial_cmp(&self, other: &str) -> Option { - Some(self.0.cmp(other)) - } -} - -impl PartialOrd> for str { - fn partial_cmp(&self, other: &Str<'_>) -> Option { - Some(self.cmp(other.0)) - } -} - -impl PartialOrd<&str> for Str<'_> { - fn partial_cmp(&self, other: &&str) -> Option { - Some(self.0.cmp(*other)) - } -} - -impl PartialOrd> for &str { - fn partial_cmp(&self, other: &Str<'_>) -> Option { - Some(self.cmp(&other.0)) - } -} - -impls! { - Cow<'_, str>, - &'static str, - String, - Arc, - Box, - #[cfg(feature = "smol_str")] - ::smol_str::SmolStr, - #[cfg(feature = "faststr")] - ::faststr::FastStr, -}