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