fast_able/
unsafe_cell_type.rs1use std::{
2 cell::UnsafeCell,
3 ops::{Add, Deref, Div, Mul, Sub},
4};
5
6pub struct U<T> {
8 _inner: UnsafeCell<T>,
9}
10
11impl<T> U<T> {
12 pub const fn new(v: T) -> U<T> {
13 U {
14 _inner: UnsafeCell::new(v),
15 }
16 }
17 #[inline(always)]
18 pub fn store(&self, v: T) {
19 let s = unsafe { &mut *self._inner.get() };
20 *s = v;
21 }
22 #[inline(always)]
23 pub fn as_mut(&self) -> &mut T {
24 unsafe { &mut *self._inner.get() }
25 }
26}
27
28impl<T: Clone> Clone for U<T> {
29 fn clone(&self) -> Self {
30 Self {
31 _inner: UnsafeCell::new(self.deref().clone()),
32 }
33 }
34}
35
36impl<T: Eq> Eq for U<T> {}
37
38impl<T: PartialEq> PartialEq for U<T> {
39 fn eq(&self, other: &Self) -> bool {
40 self.deref() == other.deref()
41 }
42}
43
44impl<T: Default> Default for U<T> {
45 fn default() -> Self {
46 Self {
47 _inner: Default::default(),
48 }
49 }
50}
51
52impl<T: Clone> U<T> {
53 pub fn load(&self) -> T {
54 self.deref().clone()
55 }
56 pub fn fetch_end(&self, v: T) -> T {
57 let r = self.deref().clone();
58 self.store(v);
59 r
60 }
61}
62
63impl<T: Add<Output = T> + Clone> U<T> {
64 pub fn fetch_add(&self, v: T) -> T {
65 let r = self.deref().clone();
66 let s = unsafe { &mut *self._inner.get() };
67 *s = self.deref().clone() + v;
68 r
69 }
70}
71
72impl<T: Sub<Output = T> + Clone> U<T> {
73 pub fn fetch_sub(&self, v: T) -> T {
74 let r = self.deref().clone();
75 let s = unsafe { &mut *self._inner.get() };
76 *s = self.deref().clone() - v;
77 r
78 }
79}
80
81use core::fmt::Debug;
82impl<T: Debug> Debug for U<T> {
83 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
84 f.write_fmt(format_args!("{:?}", self.deref()))
85 }
86}
87
88use core::fmt::Display;
89impl<T: Display> Display for U<T> {
90 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
91 f.write_fmt(format_args!("{}", self.deref()))
92 }
93}
94
95impl<T: Add<Output = T> + Clone> Add for U<T> {
96 fn add(self, rhs: Self) -> Self::Output {
97 let v1 = unsafe { &*self._inner.get() };
98 let v2 = unsafe { &*rhs._inner.get() };
99 Self::Output::new(v1.clone() + v2.clone())
100 }
101
102 type Output = U<T>;
103}
104
105impl<T: Sub<Output = T> + Clone> Sub for U<T> {
106 fn sub(self, rhs: Self) -> Self::Output {
107 let v1 = unsafe { &*self._inner.get() };
108 let v2 = unsafe { &*rhs._inner.get() };
109 Self::Output::new(v1.clone() - v2.clone())
110 }
111
112 type Output = U<T>;
113}
114
115impl<T: Div<Output = T> + Clone> Div for U<T> {
116 fn div(self, rhs: Self) -> Self::Output {
117 let v1 = unsafe { &*self._inner.get() };
118 let v2 = unsafe { &*rhs._inner.get() };
119 Self::Output::new(v1.clone() / v2.clone())
120 }
121
122 type Output = U<T>;
123}
124
125impl<T: Mul<Output = T> + Clone> Mul for U<T> {
126 fn mul(self, rhs: Self) -> Self::Output {
127 let v1 = unsafe { &*self._inner.get() };
128 let v2 = unsafe { &*rhs._inner.get() };
129 Self::Output::new(v1.clone() * v2.clone())
130 }
131
132 type Output = U<T>;
133}
134
135unsafe impl<T: Send> Send for U<T> {}
136unsafe impl<T: Sync> Sync for U<T> {}
137
138impl<T> Deref for U<T> {
139 type Target = T;
140 fn deref(&self) -> &Self::Target {
141 unsafe { &*self._inner.get() }
142 }
143}
144
145impl<T> AsRef<T> for U<T> {
146 fn as_ref(&self) -> &T {
147 self.deref()
148 }
149}
150
151impl<T> From<T> for U<T> {
152 fn from(value: T) -> Self {
153 U {
154 _inner: UnsafeCell::new(value),
155 }
156 }
157}
158
159#[test]
160fn test() {
161 let v1 = U::new(1);
162 let v2 = 1.into();
163 let v3 = v1.clone() + v2;
164 println!("r: {}", v3);
165 assert_eq!(v3.load(), 2);
166
167 let v2 = 1.into();
168 let v3 = v1.clone() - v2;
169 println!("r: {}", v3);
170 assert_eq!(v3.load(), 0);
171
172 let v3 = v1.fetch_add(3);
173 println!("r: {}", v3);
174 assert_eq!(v3, 1);
175 assert_eq!(v1.load(), 4);
176
177 let v3 = v1.fetch_end(5);
178 println!("r: {}", v3);
179 assert_eq!(v3, 4);
180
181 let v3 = v1.load();
182 println!("r: {}", v3);
183 assert_eq!(v3, 5);
184
185 v1.store(6);
186 println!("r: {}", v1.load());
187 assert_eq!(v1.load(), 6);
188
189 v1.fetch_sub(5);
190 println!("r: {}", v1.load());
191 assert_eq!(v1.load(), 1);
192}
193
194#[test]
195fn test_mut_thread() {
196 std::env::set_var("RUST_LOG", "debug");
197 env_logger::init();
198
199 static V: U<usize> = U::new(0);
200 std::thread::spawn(move || loop {
201 V.load();
202 });
203 std::thread::spawn(move || loop {
204 V.load();
205 });
206 std::thread::spawn(move || loop {
207 V.load();
208 });
209 std::thread::spawn(move || loop {
210 V.load();
211 });
212 std::thread::spawn(move || loop {
213 V.load();
214 });
215 std::thread::spawn(move || loop {
216 V.load();
217 });
218
219 for i in 0..1000000 {
220 std::thread::sleep(std::time::Duration::from_millis(100));
221 let r = V.fetch_add(i);
222 debug!("loop {}: {r}", i);
223 }
224}