mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-26 03:45:04 +00:00
Replace RacyFlag with OnceCell
This commit is contained in:
parent
111cc34c8f
commit
731f7bfc02
4 changed files with 9 additions and 34 deletions
5
Cargo.lock
generated
5
Cargo.lock
generated
|
@ -612,6 +612,7 @@ dependencies = [
|
||||||
"hir_expand",
|
"hir_expand",
|
||||||
"itertools",
|
"itertools",
|
||||||
"log",
|
"log",
|
||||||
|
"once_cell",
|
||||||
"profile",
|
"profile",
|
||||||
"rustc-hash",
|
"rustc-hash",
|
||||||
"scoped-tls",
|
"scoped-tls",
|
||||||
|
@ -1053,9 +1054,9 @@ checksum = "8d3b63360ec3cb337817c2dbd47ab4a0f170d285d8e5a2064600f3def1402397"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "once_cell"
|
name = "once_cell"
|
||||||
version = "1.4.1"
|
version = "1.5.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "260e51e7efe62b592207e9e13a68e43692a7a279171d6ba57abd208bf23645ad"
|
checksum = "95c43eba5c640051fbde86a3377842386a94281df3ff0a5f0365c2075ed5c66f"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "oorandom"
|
name = "oorandom"
|
||||||
|
|
|
@ -35,3 +35,4 @@ expect-test = "1.0"
|
||||||
tracing = "0.1"
|
tracing = "0.1"
|
||||||
tracing-subscriber = { version = "0.2", default-features = false, features = ["env-filter", "registry"] }
|
tracing-subscriber = { version = "0.2", default-features = false, features = ["env-filter", "registry"] }
|
||||||
tracing-tree = { version = "0.1.4" }
|
tracing-tree = { version = "0.1.4" }
|
||||||
|
once_cell = { version = "1.5.0", features = ["unstable"] }
|
||||||
|
|
|
@ -22,7 +22,8 @@ use hir_def::{
|
||||||
AssocItemId, DefWithBodyId, LocalModuleId, Lookup, ModuleDefId,
|
AssocItemId, DefWithBodyId, LocalModuleId, Lookup, ModuleDefId,
|
||||||
};
|
};
|
||||||
use hir_expand::{db::AstDatabase, InFile};
|
use hir_expand::{db::AstDatabase, InFile};
|
||||||
use stdx::{format_to, RacyFlag};
|
use once_cell::race::OnceBool;
|
||||||
|
use stdx::format_to;
|
||||||
use syntax::{
|
use syntax::{
|
||||||
algo,
|
algo,
|
||||||
ast::{self, AstNode},
|
ast::{self, AstNode},
|
||||||
|
@ -40,8 +41,8 @@ use crate::{
|
||||||
// `env UPDATE_EXPECT=1 cargo test -p hir_ty` to update the snapshots.
|
// `env UPDATE_EXPECT=1 cargo test -p hir_ty` to update the snapshots.
|
||||||
|
|
||||||
fn setup_tracing() -> Option<tracing::subscriber::DefaultGuard> {
|
fn setup_tracing() -> Option<tracing::subscriber::DefaultGuard> {
|
||||||
static ENABLE: RacyFlag = RacyFlag::new();
|
static ENABLE: OnceBool = OnceBool::new();
|
||||||
if !ENABLE.get(|| env::var("CHALK_DEBUG").is_ok()) {
|
if !ENABLE.get_or_init(|| env::var("CHALK_DEBUG").is_ok()) {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,5 @@
|
||||||
//! Missing batteries for standard libraries.
|
//! Missing batteries for standard libraries.
|
||||||
use std::{
|
use std::time::Instant;
|
||||||
sync::atomic::{AtomicUsize, Ordering},
|
|
||||||
time::Instant,
|
|
||||||
};
|
|
||||||
|
|
||||||
mod macros;
|
mod macros;
|
||||||
pub mod panic_context;
|
pub mod panic_context;
|
||||||
|
@ -150,31 +147,6 @@ where
|
||||||
left
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
Loading…
Reference in a new issue