mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-18 16:20:18 +00:00
Replace RacyFlag with OnceCell
This commit is contained in:
parent
111cc34c8f
commit
731f7bfc02
4 changed files with 9 additions and 34 deletions
|
@ -1,8 +1,5 @@
|
|||
//! Missing batteries for standard libraries.
|
||||
use std::{
|
||||
sync::atomic::{AtomicUsize, Ordering},
|
||||
time::Instant,
|
||||
};
|
||||
use std::time::Instant;
|
||||
|
||||
mod macros;
|
||||
pub mod panic_context;
|
||||
|
@ -150,31 +147,6 @@ where
|
|||
left
|
||||
}
|
||||
|
||||
pub struct RacyFlag(AtomicUsize);
|
||||
|
||||
impl RacyFlag {
|
||||
pub const fn new() -> RacyFlag {
|
||||
RacyFlag(AtomicUsize::new(!0))
|
||||
}
|
||||
|
||||
pub fn get(&self, init: impl FnMut() -> bool) -> bool {
|
||||
let mut init = Some(init);
|
||||
self.get_impl(&mut || init.take().map_or(false, |mut f| f()))
|
||||
}
|
||||
|
||||
fn get_impl(&self, init: &mut dyn FnMut() -> bool) -> bool {
|
||||
match self.0.load(Ordering::Relaxed) {
|
||||
0 => false,
|
||||
1 => true,
|
||||
_ => {
|
||||
let res = init();
|
||||
self.0.store(if res { 1 } else { 0 }, Ordering::Relaxed);
|
||||
res
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue