Skip to content

Commit

Permalink
📝 refine epoch RcuReader
Browse files Browse the repository at this point in the history
  • Loading branch information
Xudong-Huang committed Nov 25, 2024
1 parent f8a3203 commit f0a2e5d
Showing 1 changed file with 55 additions and 8 deletions.
63 changes: 55 additions & 8 deletions src/epoch.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::{ops::Deref, sync::atomic::Ordering};
use core::{cmp, fmt, ops::Deref, sync::atomic::Ordering};
use crossbeam_epoch::{Atomic, Guard, Owned, Shared};

pub struct RcuCell<T> {
Expand Down Expand Up @@ -101,19 +101,12 @@ impl<T> RcuCell<T> {
}
}

#[derive(Debug)]
pub struct RcuReader<T> {
// hold the guard to ensure the data is valid
_guard: Guard,
ptr: *const T,
}

impl<T> PartialEq for RcuReader<T> {
fn eq(&self, other: &Self) -> bool {
self.ptr == other.ptr
}
}

impl<T> Deref for RcuReader<T> {
type Target = T;

Expand All @@ -123,6 +116,60 @@ impl<T> Deref for RcuReader<T> {
}
}

impl<T> AsRef<T> for RcuReader<T> {
fn as_ref(&self) -> &T {
self.deref()
}
}

impl<T: PartialEq> PartialEq for RcuReader<T> {
fn eq(&self, other: &RcuReader<T>) -> bool {
*(*self) == *(*other)
}
}

impl<T: PartialOrd> PartialOrd for RcuReader<T> {
fn partial_cmp(&self, other: &RcuReader<T>) -> Option<cmp::Ordering> {
(**self).partial_cmp(&**other)
}

fn lt(&self, other: &RcuReader<T>) -> bool {
*(*self) < *(*other)
}

fn le(&self, other: &RcuReader<T>) -> bool {
*(*self) <= *(*other)
}

fn gt(&self, other: &RcuReader<T>) -> bool {
*(*self) > *(*other)
}

fn ge(&self, other: &RcuReader<T>) -> bool {
*(*self) >= *(*other)
}
}

impl<T: Ord> Ord for RcuReader<T> {
fn cmp(&self, other: &RcuReader<T>) -> cmp::Ordering {
(**self).cmp(&**other)
}
}

impl<T: Eq> Eq for RcuReader<T> {}

impl<T: fmt::Debug> fmt::Debug for RcuReader<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&**self, f)
}
}

impl<T> fmt::Pointer for RcuReader<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Pointer::fmt(&self.ptr, f)
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down

0 comments on commit f0a2e5d

Please sign in to comment.