forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatic_cell.rs
More file actions
192 lines (167 loc) · 5.41 KB
/
Copy pathstatic_cell.rs
File metadata and controls
192 lines (167 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#[cfg(feature = "threading")]
mod threading {
use crate::lock::OnceCell;
pub struct StaticCell<T: 'static> {
inner: OnceCell<T>,
}
impl<T> StaticCell<T> {
#[doc(hidden)]
pub const fn _from_once_cell(inner: OnceCell<T>) -> Self {
Self { inner }
}
pub fn get(&'static self) -> Option<&'static T> {
self.inner.get()
}
pub fn set(&'static self, value: T) -> Result<(), T> {
self.inner.set(value)
}
pub fn get_or_init<F>(&'static self, f: F) -> &'static T
where
F: FnOnce() -> T,
{
self.inner.get_or_init(f)
}
pub fn get_or_try_init<F, E>(&'static self, f: F) -> Result<&'static T, E>
where
F: FnOnce() -> Result<T, E>,
{
if let Some(val) = self.inner.get() {
return Ok(val);
}
let val = f()?;
let _ = self.inner.set(val);
Ok(self.inner.get().unwrap())
}
}
#[macro_export]
macro_rules! static_cell {
($($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty;)+) => {
$($(#[$attr])*
$vis static $name: $crate::static_cell::StaticCell<$t> =
$crate::static_cell::StaticCell::_from_once_cell($crate::lock::OnceCell::new());)+
};
}
}
#[cfg(feature = "threading")]
pub use threading::*;
#[cfg(all(not(feature = "threading"), feature = "std"))]
mod non_threading {
use crate::lock::OnceCell;
use std::thread::LocalKey;
pub struct StaticCell<T: 'static> {
inner: &'static LocalKey<OnceCell<&'static T>>,
}
fn leak<T>(x: T) -> &'static T {
Box::leak(Box::new(x))
}
impl<T> StaticCell<T> {
#[doc(hidden)]
#[must_use]
pub const fn _from_local_key(inner: &'static LocalKey<OnceCell<&'static T>>) -> Self {
Self { inner }
}
#[must_use]
pub fn get(&'static self) -> Option<&'static T> {
self.inner.with(|x| x.get().copied())
}
pub fn set(&'static self, value: T) -> Result<(), T> {
self.inner.with(|x| {
if x.get().is_some() {
Err(value)
} else {
let _ = x.set(leak(value));
Ok(())
}
})
}
pub fn get_or_init<F>(&'static self, f: F) -> &'static T
where
F: FnOnce() -> T,
{
self.inner.with(|x| *x.get_or_init(|| leak(f())))
}
pub fn get_or_try_init<F, E>(&'static self, f: F) -> Result<&'static T, E>
where
F: FnOnce() -> Result<T, E>,
{
self.inner.with(|x| {
if let Some(val) = x.get() {
Ok(*val)
} else {
let val = leak(f()?);
let _ = x.set(val);
Ok(val)
}
})
}
}
#[macro_export]
macro_rules! static_cell {
($($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty;)+) => {
$($(#[$attr])*
$vis static $name: $crate::static_cell::StaticCell<$t> = {
::std::thread_local! {
$vis static $name: $crate::lock::OnceCell<&'static $t> = const {
$crate::lock::OnceCell::new()
};
}
$crate::static_cell::StaticCell::_from_local_key(&$name)
};)+
};
}
}
#[cfg(all(not(feature = "threading"), feature = "std"))]
pub use non_threading::*;
// Same as `threading` variant, but wraps unsync::OnceCell with Sync.
#[cfg(all(not(feature = "threading"), not(feature = "std")))]
mod no_std {
use crate::lock::OnceCell;
// unsync::OnceCell is !Sync, but without std there can be no threads.
struct SyncOnceCell<T>(OnceCell<T>);
// SAFETY: Without std, threading is impossible.
unsafe impl<T> Sync for SyncOnceCell<T> {}
pub struct StaticCell<T: 'static> {
inner: SyncOnceCell<T>,
}
impl<T> StaticCell<T> {
#[doc(hidden)]
pub const fn _from_once_cell(inner: OnceCell<T>) -> Self {
Self {
inner: SyncOnceCell(inner),
}
}
pub fn get(&'static self) -> Option<&'static T> {
self.inner.0.get()
}
pub fn set(&'static self, value: T) -> Result<(), T> {
self.inner.0.set(value)
}
pub fn get_or_init<F>(&'static self, f: F) -> &'static T
where
F: FnOnce() -> T,
{
self.inner.0.get_or_init(f)
}
pub fn get_or_try_init<F, E>(&'static self, f: F) -> Result<&'static T, E>
where
F: FnOnce() -> Result<T, E>,
{
if let Some(val) = self.inner.0.get() {
return Ok(val);
}
let val = f()?;
let _ = self.inner.0.set(val);
Ok(self.inner.0.get().unwrap())
}
}
#[macro_export]
macro_rules! static_cell {
($($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty;)+) => {
$($(#[$attr])*
$vis static $name: $crate::static_cell::StaticCell<$t> =
$crate::static_cell::StaticCell::_from_once_cell($crate::lock::OnceCell::new());)+
};
}
}
#[cfg(all(not(feature = "threading"), not(feature = "std")))]
pub use no_std::*;