Skip to content

Commit

Permalink
implement some traits for Generic
Browse files Browse the repository at this point in the history
  • Loading branch information
al8n committed Oct 2, 2024
1 parent eb3a89d commit 83f6df9
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 11 deletions.
117 changes: 109 additions & 8 deletions src/entry.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use core::borrow::Borrow;

use crossbeam_skiplist::set::Entry as SetEntry;
use dbutils::traits::{Type, TypeRef};
use dbutils::{
equivalent::{Comparable, Equivalent},
traits::{Type, TypeRef},
};
use rarena_allocator::either::Either;

use super::{
Expand Down Expand Up @@ -215,6 +218,98 @@ pub struct Generic<'a, T: ?Sized> {
data: Either<&'a T, &'a [u8]>,
}

impl<T> PartialEq<T> for Generic<'_, T>
where
T: ?Sized + PartialEq + Type + for<'a> Equivalent<T::Ref<'a>>,
{
#[inline]
fn eq(&self, other: &T) -> bool {
match &self.data {
Either::Left(val) => (*val).eq(other),
Either::Right(val) => {
let ref_ = unsafe { <T::Ref<'_> as TypeRef<'_>>::from_slice(val) };
other.equivalent(&ref_)

Check warning on line 231 in src/entry.rs

View check run for this annotation

Codecov / codecov/patch

src/entry.rs#L226-L231

Added lines #L226 - L231 were not covered by tests
}
}
}
}

impl<T> PartialEq for Generic<'_, T>
where
T: ?Sized + PartialEq + Type + for<'a> Equivalent<T::Ref<'a>>,
{
#[inline]
fn eq(&self, other: &Self) -> bool {
match (&self.data, &other.data) {
(Either::Left(val), Either::Left(other_val)) => val.eq(other_val),
(Either::Right(val), Either::Right(other_val)) => val.eq(other_val),
(Either::Left(val), Either::Right(other_val)) => {
let ref_ = unsafe { <T::Ref<'_> as TypeRef<'_>>::from_slice(other_val) };
val.equivalent(&ref_)

Check warning on line 248 in src/entry.rs

View check run for this annotation

Codecov / codecov/patch

src/entry.rs#L242-L248

Added lines #L242 - L248 were not covered by tests
}
(Either::Right(val), Either::Left(other_val)) => {
let ref_ = unsafe { <T::Ref<'_> as TypeRef<'_>>::from_slice(val) };
other_val.equivalent(&ref_)

Check warning on line 252 in src/entry.rs

View check run for this annotation

Codecov / codecov/patch

src/entry.rs#L250-L252

Added lines #L250 - L252 were not covered by tests
}
}
}
}

impl<T> Eq for Generic<'_, T> where T: ?Sized + Eq + Type + for<'a> Equivalent<T::Ref<'a>> {}

impl<T> PartialOrd for Generic<'_, T>
where
T: ?Sized + Ord + Type + for<'a> Comparable<T::Ref<'a>>,
for<'a> T::Ref<'a>: Comparable<T> + Ord,
{
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(self.cmp(other))

Check warning on line 267 in src/entry.rs

View check run for this annotation

Codecov / codecov/patch

src/entry.rs#L266-L267

Added lines #L266 - L267 were not covered by tests
}
}

impl<T> PartialOrd<T> for Generic<'_, T>
where
T: ?Sized + PartialOrd + Type + for<'a> Comparable<T::Ref<'a>>,
{
#[inline]
fn partial_cmp(&self, other: &T) -> Option<core::cmp::Ordering> {
match &self.data {
Either::Left(val) => (*val).partial_cmp(other),
Either::Right(val) => {
let ref_ = unsafe { <T::Ref<'_> as TypeRef<'_>>::from_slice(val) };
Some(other.compare(&ref_).reverse())

Check warning on line 281 in src/entry.rs

View check run for this annotation

Codecov / codecov/patch

src/entry.rs#L276-L281

Added lines #L276 - L281 were not covered by tests
}
}
}
}

impl<T> Ord for Generic<'_, T>
where
T: ?Sized + Ord + Type + for<'a> Comparable<T::Ref<'a>>,
for<'a> T::Ref<'a>: Comparable<T> + Ord,
{
#[inline]
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
match (&self.data, &other.data) {
(Either::Left(val), Either::Left(other_val)) => (*val).cmp(other_val),
(Either::Right(val), Either::Right(other_val)) => {
let this = unsafe { <T::Ref<'_> as TypeRef<'_>>::from_slice(val) };
let other = unsafe { <T::Ref<'_> as TypeRef<'_>>::from_slice(other_val) };
this.cmp(&other)

Check warning on line 299 in src/entry.rs

View check run for this annotation

Codecov / codecov/patch

src/entry.rs#L293-L299

Added lines #L293 - L299 were not covered by tests
}
(Either::Left(val), Either::Right(other_val)) => {
let other = unsafe { <T::Ref<'_> as TypeRef<'_>>::from_slice(other_val) };
other.compare(*val).reverse()

Check warning on line 303 in src/entry.rs

View check run for this annotation

Codecov / codecov/patch

src/entry.rs#L301-L303

Added lines #L301 - L303 were not covered by tests
}
(Either::Right(val), Either::Left(other_val)) => {
let this = unsafe { <T::Ref<'_> as TypeRef<'_>>::from_slice(val) };
this.compare(*other_val)

Check warning on line 307 in src/entry.rs

View check run for this annotation

Codecov / codecov/patch

src/entry.rs#L305-L307

Added lines #L305 - L307 were not covered by tests
}
}
}
}

impl<T: Type + ?Sized> Generic<'_, T> {
#[inline]
pub(crate) fn encoded_len(&self) -> usize {
Expand Down Expand Up @@ -429,20 +524,26 @@ where

impl<'a, K, V> GenericEntryRef<'a, K, V>
where
K: Type + ?Sized,
K: ?Sized,
V: Type + ?Sized,
{
/// Returns the key of the entry.
/// Returns the value of the entry.
#[inline]
pub fn key(&self) -> K::Ref<'a> {
pub fn value(&self) -> V::Ref<'a> {
let p = self.ent.value();
unsafe { TypeRef::from_slice(p.as_key_slice()) }
unsafe { TypeRef::from_slice(p.as_value_slice()) }
}
}

/// Returns the value of the entry.
impl<'a, K, V> GenericEntryRef<'a, K, V>
where
K: Type + ?Sized,
V: ?Sized,
{
/// Returns the key of the entry.
#[inline]
pub fn value(&self) -> V::Ref<'a> {
pub fn key(&self) -> K::Ref<'a> {
let p = self.ent.value();
unsafe { TypeRef::from_slice(p.as_value_slice()) }
unsafe { TypeRef::from_slice(p.as_key_slice()) }
}
}
4 changes: 2 additions & 2 deletions src/swmr/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ impl<K, V, S> GenericOrderWalCore<K, V, S>
where
K: Type + Ord + ?Sized,
for<'a> <K as Type>::Ref<'a>: KeyRef<'a, K>,
V: Type + ?Sized,
V: ?Sized,
{
#[inline]
fn contains_key<'a, Q>(&'a self, key: &'a Q) -> bool
Expand Down Expand Up @@ -642,7 +642,7 @@ impl<K, V, S> GenericOrderWal<K, V, S>
where
K: Type + Ord + ?Sized,
for<'a> K::Ref<'a>: KeyRef<'a, K>,
V: Type + ?Sized,
V: ?Sized,
{
/// Returns `true` if the key exists in the WAL.
#[inline]
Expand Down
2 changes: 1 addition & 1 deletion src/swmr/generic/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl<K, V, S> GenericWalReader<K, V, S>
where
K: Type + Ord + ?Sized,
for<'a> K::Ref<'a>: KeyRef<'a, K>,
V: Type + ?Sized,
V: ?Sized,
{
/// Returns `true` if the key exists in the WAL.
#[inline]
Expand Down

0 comments on commit 83f6df9

Please sign in to comment.