Skip to content

Commit

Permalink
Add comparable
Browse files Browse the repository at this point in the history
  • Loading branch information
al8n committed Sep 11, 2024
1 parent 33aa5d9 commit 78c6b15
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
68 changes: 62 additions & 6 deletions src/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use core::{
borrow::Borrow,
marker::PhantomData,
ptr::{self, NonNull},
slice,
};

use crossbeam_skiplist::{Comparable, Equivalent};

/// Returns when the bytes are too large to be written to the vacant buffer.
#[derive(Debug, Default, Clone, Copy)]
pub struct TooLarge {
Expand Down Expand Up @@ -166,9 +169,44 @@ impl<'a> AsMut<[u8]> for VacantBuffer<'a> {
}
}

impl<'a> PartialEq<[u8]> for VacantBuffer<'a> {
fn eq(&self, other: &[u8]) -> bool {
self.as_ref().eq(other)
impl<'a, Q> PartialEq<Q> for VacantBuffer<'a>
where
[u8]: Borrow<Q>,
Q: ?Sized + Eq,
{
fn eq(&self, other: &Q) -> bool {
self.as_ref().borrow().eq(other)

Check warning on line 178 in src/buffer.rs

View check run for this annotation

Codecov / codecov/patch

src/buffer.rs#L177-L178

Added lines #L177 - L178 were not covered by tests
}
}

impl<'a, Q> PartialOrd<Q> for VacantBuffer<'a>
where
[u8]: Borrow<Q>,
Q: ?Sized + Ord,
{
fn partial_cmp(&self, other: &Q) -> Option<core::cmp::Ordering> {

Check warning on line 187 in src/buffer.rs

View check run for this annotation

Codecov / codecov/patch

src/buffer.rs#L187

Added line #L187 was not covered by tests
#[allow(clippy::needless_borrow)]
Some(self.as_ref().borrow().cmp(&other))

Check warning on line 189 in src/buffer.rs

View check run for this annotation

Codecov / codecov/patch

src/buffer.rs#L189

Added line #L189 was not covered by tests
}
}

impl<'a, Q> Equivalent<Q> for VacantBuffer<'a>
where
[u8]: Borrow<Q>,
Q: ?Sized + Eq,
{
fn equivalent(&self, key: &Q) -> bool {
self.as_ref().borrow().eq(key)

Check warning on line 199 in src/buffer.rs

View check run for this annotation

Codecov / codecov/patch

src/buffer.rs#L198-L199

Added lines #L198 - L199 were not covered by tests
}
}

impl<'a, Q> Comparable<Q> for VacantBuffer<'a>
where
[u8]: Borrow<Q>,
Q: ?Sized + Ord,
{
fn compare(&self, other: &Q) -> core::cmp::Ordering {
self.as_ref().borrow().compare(other)

Check warning on line 209 in src/buffer.rs

View check run for this annotation

Codecov / codecov/patch

src/buffer.rs#L208-L209

Added lines #L208 - L209 were not covered by tests
}
}

Expand All @@ -178,21 +216,33 @@ impl<'a> PartialEq<VacantBuffer<'a>> for [u8] {
}
}

impl<'a> PartialOrd<VacantBuffer<'a>> for [u8] {
fn partial_cmp(&self, other: &VacantBuffer<'a>) -> Option<core::cmp::Ordering> {
Some(self.as_ref().cmp(other.as_ref()))

Check warning on line 221 in src/buffer.rs

View check run for this annotation

Codecov / codecov/patch

src/buffer.rs#L220-L221

Added lines #L220 - L221 were not covered by tests
}
}

impl<'a> PartialEq<[u8]> for &VacantBuffer<'a> {
fn eq(&self, other: &[u8]) -> bool {
self.as_ref().eq(other)
}
}

impl<'a> PartialOrd<[u8]> for &VacantBuffer<'a> {
fn partial_cmp(&self, other: &[u8]) -> Option<core::cmp::Ordering> {
Some(self.as_ref().cmp(other))

Check warning on line 233 in src/buffer.rs

View check run for this annotation

Codecov / codecov/patch

src/buffer.rs#L232-L233

Added lines #L232 - L233 were not covered by tests
}
}

impl<'a> PartialEq<&VacantBuffer<'a>> for [u8] {
fn eq(&self, other: &&VacantBuffer<'a>) -> bool {
self.eq(other.as_ref())
}
}

impl<'a, const N: usize> PartialEq<[u8; N]> for VacantBuffer<'a> {
fn eq(&self, other: &[u8; N]) -> bool {
self.as_ref().eq(other.as_ref())
impl<'a> PartialOrd<&VacantBuffer<'a>> for [u8] {
fn partial_cmp(&self, other: &&VacantBuffer<'a>) -> Option<core::cmp::Ordering> {
Some(self.cmp(other.as_ref()))

Check warning on line 245 in src/buffer.rs

View check run for this annotation

Codecov / codecov/patch

src/buffer.rs#L244-L245

Added lines #L244 - L245 were not covered by tests
}
}

Expand All @@ -202,6 +252,12 @@ impl<'a, const N: usize> PartialEq<VacantBuffer<'a>> for [u8; N] {
}
}

impl<'a, const N: usize> PartialOrd<VacantBuffer<'a>> for [u8; N] {
fn partial_cmp(&self, other: &VacantBuffer<'a>) -> Option<core::cmp::Ordering> {
Some(self.as_ref().cmp(other.as_ref()))

Check warning on line 257 in src/buffer.rs

View check run for this annotation

Codecov / codecov/patch

src/buffer.rs#L256-L257

Added lines #L256 - L257 were not covered by tests
}
}

impl<'a, const N: usize> PartialEq<&VacantBuffer<'a>> for [u8; N] {
fn eq(&self, other: &&VacantBuffer<'a>) -> bool {
self.as_ref().eq(other.as_ref())
Expand Down
1 change: 0 additions & 1 deletion src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use wal::ImmutableWal;

const MB: usize = 1024 * 1024;

#[macro_export]
macro_rules! common_unittests {
($prefix:ident::$wal:ident) => {
paste::paste! {
Expand Down

0 comments on commit 78c6b15

Please sign in to comment.