|
| 1 | + |
| 2 | +use std::cell::Cell; |
| 3 | +use std::cmp::Ordering; |
| 4 | +use std::fmt; |
| 5 | + |
| 6 | +use std::ops::{Deref, DerefMut}; |
| 7 | + |
| 8 | +/// A transparent wrapper of [`Cell<T>`](std::cell::Cell) which is identical in every way, except |
| 9 | +/// it will implement arithmetic operators as well. |
| 10 | +/// |
| 11 | +/// The purpose of `MathCell` is to be used from [.cell_view()](crate::ArrayBase::cell_view). |
| 12 | +/// The `MathCell` derefs to `Cell`, so all the cell's methods are available. |
| 13 | +#[repr(transparent)] |
| 14 | +#[derive(Default)] |
| 15 | +pub struct MathCell<T>(Cell<T>); |
| 16 | + |
| 17 | +impl<T> MathCell<T> { |
| 18 | + /// Create a new cell with the given value |
| 19 | + #[inline(always)] |
| 20 | + pub const fn new(value: T) -> Self { MathCell(Cell::new(value)) } |
| 21 | + |
| 22 | + /// Return the inner value |
| 23 | + pub fn into_inner(self) -> T { Cell::into_inner(self.0) } |
| 24 | + |
| 25 | + /// Swap value with another cell |
| 26 | + pub fn swap(&self, other: &Self) { |
| 27 | + Cell::swap(&self.0, &other.0) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +impl<T> Deref for MathCell<T> { |
| 32 | + type Target = Cell<T>; |
| 33 | + #[inline(always)] |
| 34 | + fn deref(&self) -> &Self::Target { &self.0 } |
| 35 | +} |
| 36 | + |
| 37 | +impl<T> DerefMut for MathCell<T> { |
| 38 | + #[inline(always)] |
| 39 | + fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } |
| 40 | +} |
| 41 | + |
| 42 | +impl<T> Clone for MathCell<T> |
| 43 | + where T: Copy |
| 44 | +{ |
| 45 | + fn clone(&self) -> Self { |
| 46 | + MathCell::new(self.get()) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +impl<T> PartialEq for MathCell<T> |
| 51 | + where T: Copy + PartialEq |
| 52 | +{ |
| 53 | + fn eq(&self, rhs: &Self) -> bool { |
| 54 | + self.get() == rhs.get() |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +impl<T> Eq for MathCell<T> |
| 59 | + where T: Copy + Eq |
| 60 | +{ } |
| 61 | + |
| 62 | +impl<T> PartialOrd for MathCell<T> |
| 63 | + where T: Copy + PartialOrd |
| 64 | +{ |
| 65 | + fn partial_cmp(&self, rhs: &Self) -> Option<Ordering> { |
| 66 | + self.get().partial_cmp(&rhs.get()) |
| 67 | + } |
| 68 | + |
| 69 | + fn lt(&self, rhs: &Self) -> bool { self.get().lt(&rhs.get()) } |
| 70 | + fn le(&self, rhs: &Self) -> bool { self.get().le(&rhs.get()) } |
| 71 | + fn gt(&self, rhs: &Self) -> bool { self.get().gt(&rhs.get()) } |
| 72 | + fn ge(&self, rhs: &Self) -> bool { self.get().ge(&rhs.get()) } |
| 73 | +} |
| 74 | + |
| 75 | +impl<T> Ord for MathCell<T> |
| 76 | + where T: Copy + Ord |
| 77 | +{ |
| 78 | + fn cmp(&self, rhs: &Self) -> Ordering { |
| 79 | + self.get().cmp(&rhs.get()) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +impl<T> fmt::Debug for MathCell<T> |
| 84 | + where T: Copy + fmt::Debug |
| 85 | +{ |
| 86 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 87 | + self.get().fmt(f) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | + |
| 92 | +#[cfg(test)] |
| 93 | +mod tests { |
| 94 | + use super::MathCell; |
| 95 | + |
| 96 | + #[test] |
| 97 | + fn test_basic() { |
| 98 | + let c = &MathCell::new(0); |
| 99 | + c.set(1); |
| 100 | + assert_eq!(c.get(), 1); |
| 101 | + } |
| 102 | +} |
0 commit comments