1
0
Fork 0
forked from wry/wry

autocommit 2022-01-08 23:04:44 CET

This commit is contained in:
Julian Orth 2022-01-08 23:04:44 +01:00
parent 3336f1ab6a
commit 4efb187e24
3 changed files with 109 additions and 64 deletions

View file

@ -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
}
}