aboutsummaryrefslogtreecommitdiff
path: root/crates/stdx/src
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-08-18 16:20:10 +0100
committerAleksey Kladov <[email protected]>2020-08-18 16:20:57 +0100
commitaad911fb0cd772e809270be3e0a526d4738b9dd5 (patch)
tree071693ea923db713b5e590a5e7cecfa2fff04185 /crates/stdx/src
parente81c310b6224946318b8e6af56a55021716ea9b5 (diff)
Speedup ty tests
Closes #5792
Diffstat (limited to 'crates/stdx/src')
-rw-r--r--crates/stdx/src/lib.rs30
1 files changed, 29 insertions, 1 deletions
diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs
index 265d19288..5d60f0219 100644
--- a/crates/stdx/src/lib.rs
+++ b/crates/stdx/src/lib.rs
@@ -1,5 +1,8 @@
1//! Missing batteries for standard libraries. 1//! Missing batteries for standard libraries.
2use std::time::Instant; 2use std::{
3 sync::atomic::{AtomicUsize, Ordering},
4 time::Instant,
5};
3 6
4mod macros; 7mod macros;
5 8
@@ -134,6 +137,31 @@ where
134 left 137 left
135} 138}
136 139
140pub struct RacyFlag(AtomicUsize);
141
142impl RacyFlag {
143 pub const fn new() -> RacyFlag {
144 RacyFlag(AtomicUsize::new(0))
145 }
146
147 pub fn get(&self, init: impl FnMut() -> bool) -> bool {
148 let mut init = Some(init);
149 self.get_impl(&mut || init.take().map_or(false, |mut f| f()))
150 }
151
152 fn get_impl(&self, init: &mut dyn FnMut() -> bool) -> bool {
153 match self.0.load(Ordering::Relaxed) {
154 0 => false,
155 1 => true,
156 _ => {
157 let res = init();
158 self.0.store(if res { 1 } else { 0 }, Ordering::Relaxed);
159 res
160 }
161 }
162 }
163}
164
137#[cfg(test)] 165#[cfg(test)]
138mod tests { 166mod tests {
139 use super::*; 167 use super::*;