Skip to content

Commit 731f7bf

Browse files
committed
Replace RacyFlag with OnceCell
1 parent 111cc34 commit 731f7bf

File tree

4 files changed

+9
-34
lines changed

4 files changed

+9
-34
lines changed

Cargo.lock

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/hir_ty/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ expect-test = "1.0"
3535
tracing = "0.1"
3636
tracing-subscriber = { version = "0.2", default-features = false, features = ["env-filter", "registry"] }
3737
tracing-tree = { version = "0.1.4" }
38+
once_cell = { version = "1.5.0", features = ["unstable"] }

crates/hir_ty/src/tests.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ use hir_def::{
2222
AssocItemId, DefWithBodyId, LocalModuleId, Lookup, ModuleDefId,
2323
};
2424
use hir_expand::{db::AstDatabase, InFile};
25-
use stdx::{format_to, RacyFlag};
25+
use once_cell::race::OnceBool;
26+
use stdx::format_to;
2627
use syntax::{
2728
algo,
2829
ast::{self, AstNode},
@@ -40,8 +41,8 @@ use crate::{
4041
// `env UPDATE_EXPECT=1 cargo test -p hir_ty` to update the snapshots.
4142

4243
fn setup_tracing() -> Option<tracing::subscriber::DefaultGuard> {
43-
static ENABLE: RacyFlag = RacyFlag::new();
44-
if !ENABLE.get(|| env::var("CHALK_DEBUG").is_ok()) {
44+
static ENABLE: OnceBool = OnceBool::new();
45+
if !ENABLE.get_or_init(|| env::var("CHALK_DEBUG").is_ok()) {
4546
return None;
4647
}
4748

crates/stdx/src/lib.rs

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
//! Missing batteries for standard libraries.
2-
use std::{
3-
sync::atomic::{AtomicUsize, Ordering},
4-
time::Instant,
5-
};
2+
use std::time::Instant;
63

74
mod macros;
85
pub mod panic_context;
@@ -150,31 +147,6 @@ where
150147
left
151148
}
152149

153-
pub struct RacyFlag(AtomicUsize);
154-
155-
impl 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-
178150
#[cfg(test)]
179151
mod tests {
180152
use super::*;

0 commit comments

Comments
 (0)