1
0
Fork 0
forked from wry/wry

autocommit 2022-01-02 15:13:33 CET

This commit is contained in:
Julian Orth 2022-01-02 15:13:33 +01:00
commit d6172b273f
50 changed files with 5807 additions and 0 deletions

38
src/utils/numcell.rs Normal file
View file

@ -0,0 +1,38 @@
use std::cell::Cell;
use std::ops::{Add, Sub};
#[derive(Default)]
pub struct NumCell<T> {
t: Cell<T>,
}
impl<T> NumCell<T> {
pub fn new(t: T) -> Self {
Self { t: Cell::new(t) }
}
pub fn load(&self) -> T
where
T: Copy,
{
self.t.get()
}
pub fn fetch_add(&self, n: T) -> T
where
T: Copy + Add<T, Output = T>,
{
let res = self.t.get();
self.t.set(res + n);
res
}
pub fn fetch_sub(&self, n: T) -> T
where
T: Copy + Sub<T, Output = T>,
{
let res = self.t.get();
self.t.set(res - n);
res
}
}