autocommit 2022-01-08 23:04:44 CET
This commit is contained in:
parent
3336f1ab6a
commit
4efb187e24
3 changed files with 109 additions and 64 deletions
|
|
@ -1,5 +1,5 @@
|
|||
use std::cell::Cell;
|
||||
use std::ops::{Add, Sub};
|
||||
use std::ops::{Add, BitAnd, BitOr, Sub};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct NumCell<T> {
|
||||
|
|
@ -7,14 +7,17 @@ pub struct NumCell<T> {
|
|||
}
|
||||
|
||||
impl<T> NumCell<T> {
|
||||
#[inline(always)]
|
||||
pub fn new(t: T) -> Self {
|
||||
Self { t: Cell::new(t) }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn replace(&self, n: T) -> T {
|
||||
self.t.replace(n)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn load(&self) -> T
|
||||
where
|
||||
T: Copy,
|
||||
|
|
@ -22,6 +25,7 @@ impl<T> NumCell<T> {
|
|||
self.t.get()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn fetch_add(&self, n: T) -> T
|
||||
where
|
||||
T: Copy + Add<T, Output = T>,
|
||||
|
|
@ -31,6 +35,7 @@ impl<T> NumCell<T> {
|
|||
res
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn fetch_sub(&self, n: T) -> T
|
||||
where
|
||||
T: Copy + Sub<T, Output = T>,
|
||||
|
|
@ -39,4 +44,38 @@ impl<T> NumCell<T> {
|
|||
self.t.set(res - n);
|
||||
res
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn or_assign(&self, n: T)
|
||||
where
|
||||
T: Copy + BitOr<Output = T>,
|
||||
{
|
||||
self.t.set(self.t.get() | n);
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn and_assign(&self, n: T)
|
||||
where
|
||||
T: Copy + BitAnd<Output = T>,
|
||||
{
|
||||
self.t.set(self.t.get() & n);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: BitOr<Output = T> + Copy> BitOr<T> for &'_ NumCell<T> {
|
||||
type Output = T;
|
||||
|
||||
#[inline(always)]
|
||||
fn bitor(self, rhs: T) -> Self::Output {
|
||||
self.t.get() | rhs
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: BitAnd<Output = T> + Copy> BitAnd<T> for &'_ NumCell<T> {
|
||||
type Output = T;
|
||||
|
||||
#[inline(always)]
|
||||
fn bitand(self, rhs: T) -> Self::Output {
|
||||
self.t.get() & rhs
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,18 +7,21 @@ pub trait MutPtrExt<T: ?Sized> {
|
|||
}
|
||||
|
||||
impl<T: ?Sized> PtrExt<T> for *const T {
|
||||
#[inline(always)]
|
||||
unsafe fn deref<'a>(self) -> &'a T {
|
||||
&*self
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized> PtrExt<T> for *mut T {
|
||||
#[inline(always)]
|
||||
unsafe fn deref<'a>(self) -> &'a T {
|
||||
&*self
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized> MutPtrExt<T> for *mut T {
|
||||
#[inline(always)]
|
||||
unsafe fn deref_mut<'a>(self) -> &'a mut T {
|
||||
&mut *self
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue