Skip to content

Commit f71d4d1

Browse files
committed
Remove internal once_cell usages
There are two spots left where we can't remove once-cell yet: - In `memory_profiler.rs`, we use `OnceCell::get_or_try_init`. This function is not stabilized in std yet. - The `foundations::security::allow_list` macro generates OnceCell instances in user code. Changing this requires a major bump.
1 parent 7269025 commit f71d4d1

File tree

5 files changed

+21
-24
lines changed

5 files changed

+21
-24
lines changed

foundations-macros/src/metrics/mod.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,6 @@ fn expand_from_parsed(args: MacroArgs, extern_: Mod) -> proc_macro2::TokenStream
103103
fns,
104104
} = extern_;
105105

106-
let reexports = quote! { #foundations::reexports_for_macros };
107-
108106
// This should be using `Span::def_site` but it is currently unstable.
109107
let metrics_struct = Ident::new(&format!("__{mod_name}_Metrics"), Span::call_site());
110108

@@ -152,8 +150,8 @@ fn expand_from_parsed(args: MacroArgs, extern_: Mod) -> proc_macro2::TokenStream
152150
#(#label_set_structs)*
153151

154152
#[allow(non_upper_case_globals)]
155-
static #metrics_struct: #reexports::once_cell::sync::Lazy<#metrics_struct> =
156-
#reexports::once_cell::sync::Lazy::new(|| {
153+
static #metrics_struct: ::std::sync::LazyLock<#metrics_struct> =
154+
::std::sync::LazyLock::new(|| {
157155
#init_registry
158156
#init_opt_registry
159157

@@ -424,8 +422,8 @@ mod tests {
424422
struct __empty_Metrics {}
425423

426424
#[allow(non_upper_case_globals)]
427-
static __empty_Metrics: ::foundations::reexports_for_macros::once_cell::sync::Lazy<__empty_Metrics> =
428-
::foundations::reexports_for_macros::once_cell::sync::Lazy::new(|| { __empty_Metrics {} });
425+
static __empty_Metrics: ::std::sync::LazyLock<__empty_Metrics> =
426+
::std::sync::LazyLock::new(|| { __empty_Metrics {} });
429427
}
430428
};
431429

@@ -459,8 +457,8 @@ mod tests {
459457
}
460458

461459
#[allow(non_upper_case_globals)]
462-
static __oxy_Metrics: tarmac::reexports_for_macros::once_cell::sync::Lazy<__oxy_Metrics> =
463-
tarmac::reexports_for_macros::once_cell::sync::Lazy::new(|| {
460+
static __oxy_Metrics: ::std::sync::LazyLock<__oxy_Metrics> =
461+
::std::sync::LazyLock::new(|| {
464462
let registry = &mut *tarmac::telemetry::metrics::internal::Registries::get_subsystem(stringify!(oxy), false, true);
465463

466464
__oxy_Metrics {
@@ -516,8 +514,8 @@ mod tests {
516514
}
517515

518516
#[allow(non_upper_case_globals)]
519-
static __oxy_Metrics: ::foundations::reexports_for_macros::once_cell::sync::Lazy<__oxy_Metrics> =
520-
::foundations::reexports_for_macros::once_cell::sync::Lazy::new(|| {
517+
static __oxy_Metrics: ::std::sync::LazyLock<__oxy_Metrics> =
518+
::std::sync::LazyLock::new(|| {
521519
let opt_registry = &mut *::foundations::telemetry::metrics::internal::Registries::get_subsystem(stringify!(oxy), true, true);
522520

523521
__oxy_Metrics {
@@ -577,8 +575,8 @@ mod tests {
577575
}
578576

579577
#[allow(non_upper_case_globals)]
580-
static __oxy_Metrics: ::foundations::reexports_for_macros::once_cell::sync::Lazy<__oxy_Metrics> =
581-
::foundations::reexports_for_macros::once_cell::sync::Lazy::new(|| {
578+
static __oxy_Metrics: ::std::sync::LazyLock<__oxy_Metrics> =
579+
::std::sync::LazyLock::new(|| {
582580
let registry = &mut *::foundations::telemetry::metrics::internal::Registries::get_subsystem(stringify!(oxy), false, false);
583581
let opt_registry = &mut *::foundations::telemetry::metrics::internal::Registries::get_subsystem(stringify!(oxy), true, false);
584582

@@ -682,8 +680,8 @@ mod tests {
682680
}
683681

684682
#[allow(non_upper_case_globals)]
685-
static __oxy_Metrics: ::foundations::reexports_for_macros::once_cell::sync::Lazy<__oxy_Metrics> =
686-
::foundations::reexports_for_macros::once_cell::sync::Lazy::new(|| {
683+
static __oxy_Metrics: ::std::sync::LazyLock<__oxy_Metrics> =
684+
::std::sync::LazyLock::new(|| {
687685
let registry = &mut *::foundations::telemetry::metrics::internal::Registries::get_subsystem(stringify!(oxy), false, true);
688686

689687
__oxy_Metrics {
@@ -777,8 +775,8 @@ mod tests {
777775
}
778776

779777
#[allow(non_upper_case_globals)]
780-
static __oxy_Metrics: ::foundations::reexports_for_macros::once_cell::sync::Lazy<__oxy_Metrics> =
781-
::foundations::reexports_for_macros::once_cell::sync::Lazy::new(|| {
778+
static __oxy_Metrics: ::std::sync::LazyLock<__oxy_Metrics> =
779+
::std::sync::LazyLock::new(|| {
782780
let registry = &mut *::foundations::telemetry::metrics::internal::Registries::get_subsystem(stringify!(oxy), false, true);
783781

784782
__oxy_Metrics {

foundations/Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ tokio-runtime-metrics = [
102102
# Enables logging functionality.
103103
logging = [
104104
"dep:governor",
105-
"dep:once_cell",
106105
"dep:parking_lot",
107106
"dep:slog-async",
108107
"dep:slog-json",
@@ -118,7 +117,6 @@ logging = [
118117
tracing = [
119118
"dep:foundations-macros",
120119
"dep:governor",
121-
"dep:once_cell",
122120
"dep:parking_lot",
123121
"dep:rand",
124122
"dep:cf-rustracing-jaeger",
@@ -138,7 +136,6 @@ tracing = [
138136
metrics = [
139137
"dep:foundations-macros",
140138
"dep:erased-serde",
141-
"dep:once_cell",
142139
"dep:parking_lot",
143140
"dep:prometheus-client",
144141
"dep:prometheus",

foundations/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ pub mod security;
100100
pub mod reexports_for_macros {
101101
#[cfg(feature = "tracing")]
102102
pub use cf_rustracing;
103-
#[cfg(any(feature = "metrics", feature = "security"))]
103+
#[cfg(any(feature = "security"))]
104104
pub use once_cell;
105105
#[cfg(feature = "metrics")]
106106
pub use parking_lot;

foundations/src/telemetry/memory_profiler.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ use std::fs::File;
77
use std::io::Read;
88
use std::os::raw::c_char;
99
use std::sync::mpsc::{self};
10+
use std::sync::OnceLock;
1011
use tempfile::NamedTempFile;
1112
use tokio::sync::{oneshot, Mutex as AsyncMutex};
1213

14+
// TODO(once_cell_try): replace with `std::sync::OnceLock`
1315
static PROFILER: OnceCell<Option<MemoryProfiler>> = OnceCell::new();
14-
static HEAP_PROFILE_REQUEST_SENDER: OnceCell<
16+
static HEAP_PROFILE_REQUEST_SENDER: OnceLock<
1517
AsyncMutex<mpsc::Sender<oneshot::Sender<Result<String>>>>,
16-
> = OnceCell::new();
18+
> = OnceLock::new();
1719

1820
mod control {
1921
use super::*;

foundations/src/telemetry/metrics/internal.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
use super::{info_metric, ExtraProducer, InfoMetric};
22
use crate::telemetry::settings::{MetricsSettings, ServiceNameFormat};
33
use crate::{Result, ServiceInfo};
4-
use once_cell::sync::OnceCell;
54
use prometheus_client::encoding::text::{encode, EncodeMetric};
65
use prometheus_client::registry::Registry;
76
use prometools::serde::InfoGauge;
87
use std::any::TypeId;
98
use std::borrow::Cow;
109
use std::collections::HashMap;
1110
use std::ops::DerefMut;
11+
use std::sync::OnceLock;
1212

13-
static REGISTRIES: OnceCell<Registries> = OnceCell::new();
13+
static REGISTRIES: OnceLock<Registries> = OnceLock::new();
1414

1515
enum MetricsServiceName {
1616
Prefix(String),

0 commit comments

Comments
 (0)