aboutsummaryrefslogtreecommitdiff
path: root/crates/stdx
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-11-11 02:13:46 +0000
committerGitHub <[email protected]>2020-11-11 02:13:46 +0000
commit23c9f5b87ea93a7b163301e4ee64d891ba17fc50 (patch)
treea9d49e525b3ff107816b7b3168a961cc504b2ed0 /crates/stdx
parent111cc34c8f181315f4dcfa85c616d54d47eff0b9 (diff)
parent731f7bfc020a69ca277921c3d17a57fdcf0d7cc1 (diff)
Merge #6526
6526: Replace RacyFlag with OnceCell r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/stdx')
-rw-r--r--crates/stdx/src/lib.rs30
1 files changed, 1 insertions, 29 deletions
diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs
index 59d89f47d..374ed5910 100644
--- a/crates/stdx/src/lib.rs
+++ b/crates/stdx/src/lib.rs
@@ -1,8 +1,5 @@
1//! Missing batteries for standard libraries. 1//! Missing batteries for standard libraries.
2use std::{ 2use std::time::Instant;
3 sync::atomic::{AtomicUsize, Ordering},
4 time::Instant,
5};
6 3
7mod macros; 4mod macros;
8pub mod panic_context; 5pub mod panic_context;
@@ -150,31 +147,6 @@ where
150 left 147 left
151} 148}
152 149
153pub struct RacyFlag(AtomicUsize);
154
155impl RacyFlag {
156 pub const fn new() -> RacyFlag {
157 RacyFlag(AtomicUsize::new(!0))
158 }
159
160 pub fn get(&self, init: impl FnMut() -> bool) -> bool {
161 let mut init = Some(init);
162 self.get_impl(&mut || init.take().map_or(false, |mut f| f()))
163 }
164
165 fn get_impl(&self, init: &mut dyn FnMut() -> bool) -> bool {
166 match self.0.load(Ordering::Relaxed) {
167 0 => false,
168 1 => true,
169 _ => {
170 let res = init();
171 self.0.store(if res { 1 } else { 0 }, Ordering::Relaxed);
172 res
173 }
174 }
175 }
176}
177
178#[cfg(test)] 150#[cfg(test)]
179mod tests { 151mod tests {
180 use super::*; 152 use super::*;