From f2cd59ad048c9b867e240cc29e92de1cbffc2d5b Mon Sep 17 00:00:00 2001 From: Bruce Guenter Date: Wed, 30 Aug 2023 12:27:28 -0600 Subject: [PATCH] chore(deps): Drop use of `once_cell::{sync,unsync}::OnceCell` (#17621) * chore(deps): Drop use of `once_cell::{sync,unsync}::OnceCell` This is now part of `std::cell` and `std::sync`. This bumps the MSRV to 1.70.0 which is when those features were stabilized. * Fix `const fn` lint * Another const lint * Fix more uses * Deny `once_cell::sync::OnceCell` with clippy --- Cargo.lock | 1 - clippy.toml | 5 +++++ lib/vector-config/Cargo.toml | 1 - lib/vector-config/src/schema/parser/query.rs | 7 +++---- lib/vector-core/src/config/log_schema.rs | 6 ++++-- lib/vector-core/src/config/telemetry.rs | 6 ++++-- lib/vector-core/src/metrics/mod.rs | 5 ++--- lib/vector-core/src/metrics/recorder.rs | 3 +-- src/aws/mod.rs | 5 ++--- src/proto/mod.rs | 5 +++-- src/sinks/datadog/metrics/encoder.rs | 5 ++--- src/sources/kafka.rs | 5 ++--- src/trace.rs | 5 ++--- vdev/src/app.rs | 11 ++++++----- 14 files changed, 36 insertions(+), 34 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 93fc6d8c26b49..561cd1ebb9aa0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9773,7 +9773,6 @@ dependencies = [ "inventory", "no-proxy", "num-traits", - "once_cell", "serde", "serde_json", "serde_with 3.3.0", diff --git a/clippy.toml b/clippy.toml index 63d9f13b03554..f8cb0ecaa39f7 100644 --- a/clippy.toml +++ b/clippy.toml @@ -5,3 +5,8 @@ cognitive-complexity-threshold = 75 disallowed-methods = [ { path = "std::io::Write::write", reason = "This doesn't handle short writes, use `write_all` instead." }, ] + +disallowed-types = [ + { path = "once_cell::sync::OnceCell", reason = "Use `std::sync::OnceLock` instead." }, + { path = "once_cell::unsync::OnceCell", reason = "Use `std::cell::OnceCell` instead." }, +] diff --git a/lib/vector-config/Cargo.toml b/lib/vector-config/Cargo.toml index 5e68cec8b3b77..cc65e96ba4590 100644 --- a/lib/vector-config/Cargo.toml +++ b/lib/vector-config/Cargo.toml @@ -18,7 +18,6 @@ indexmap = { version = "2.0", default-features = false, features = ["std"] } inventory = { version = "0.3" } no-proxy = { version = "0.3.4", default-features = false, features = ["serialize"] } num-traits = { version = "0.2.16", default-features = false } -once_cell = { version = "1", default-features = false } serde = { version = "1.0", default-features = false } serde_json = { version = "1.0", default-features = false, features = ["std"] } serde_with = { version = "3.3.0", default-features = false, features = ["std"] } diff --git a/lib/vector-config/src/schema/parser/query.rs b/lib/vector-config/src/schema/parser/query.rs index a2e719353b08c..cc673b1e2aad1 100644 --- a/lib/vector-config/src/schema/parser/query.rs +++ b/lib/vector-config/src/schema/parser/query.rs @@ -1,6 +1,5 @@ -use std::{fs::File, io::BufReader, path::Path}; +use std::{fs::File, io::BufReader, path::Path, sync::OnceLock}; -use once_cell::sync::OnceCell; use serde_json::Value; use snafu::Snafu; use vector_config_common::{ @@ -415,8 +414,8 @@ impl<'a> QueryableSchema for SimpleSchema<'a> { } fn schema_to_simple_schema(schema: &Schema) -> SimpleSchema<'_> { - static TRUE_SCHEMA_OBJECT: OnceCell = OnceCell::new(); - static FALSE_SCHEMA_OBJECT: OnceCell = OnceCell::new(); + static TRUE_SCHEMA_OBJECT: OnceLock = OnceLock::new(); + static FALSE_SCHEMA_OBJECT: OnceLock = OnceLock::new(); let schema_object = match schema { Schema::Bool(bool) => { diff --git a/lib/vector-core/src/config/log_schema.rs b/lib/vector-core/src/config/log_schema.rs index a21c87e4076db..a8b0388cda6c0 100644 --- a/lib/vector-core/src/config/log_schema.rs +++ b/lib/vector-core/src/config/log_schema.rs @@ -1,9 +1,11 @@ +use std::sync::OnceLock; + use lookup::lookup_v2::OptionalTargetPath; use lookup::{OwnedTargetPath, OwnedValuePath}; -use once_cell::sync::{Lazy, OnceCell}; +use once_cell::sync::Lazy; use vector_config::configurable_component; -static LOG_SCHEMA: OnceCell = OnceCell::new(); +static LOG_SCHEMA: OnceLock = OnceLock::new(); static LOG_SCHEMA_DEFAULT: Lazy = Lazy::new(LogSchema::default); const MESSAGE: &str = "message"; diff --git a/lib/vector-core/src/config/telemetry.rs b/lib/vector-core/src/config/telemetry.rs index 71348c509ef94..d75d5407fcfbe 100644 --- a/lib/vector-core/src/config/telemetry.rs +++ b/lib/vector-core/src/config/telemetry.rs @@ -1,8 +1,10 @@ -use once_cell::sync::{Lazy, OnceCell}; +use std::sync::OnceLock; + +use once_cell::sync::Lazy; use vector_common::request_metadata::GroupedCountByteSize; use vector_config::configurable_component; -static TELEMETRY: OnceCell = OnceCell::new(); +static TELEMETRY: OnceLock = OnceLock::new(); static TELEMETRY_DEFAULT: Lazy = Lazy::new(Telemetry::default); /// Loads the telemetry options from configurations and sets the global options. diff --git a/lib/vector-core/src/metrics/mod.rs b/lib/vector-core/src/metrics/mod.rs index 375dba4af624b..b021d7f29dfa9 100644 --- a/lib/vector-core/src/metrics/mod.rs +++ b/lib/vector-core/src/metrics/mod.rs @@ -4,13 +4,12 @@ mod recency; mod recorder; mod storage; -use std::time::Duration; +use std::{sync::OnceLock, time::Duration}; use chrono::Utc; use metrics::Key; use metrics_tracing_context::TracingContextLayer; use metrics_util::layers::Layer; -use once_cell::sync::OnceCell; use snafu::Snafu; pub use self::ddsketch::{AgentDDSketch, BinMap, Config}; @@ -29,7 +28,7 @@ pub enum Error { TimeoutMustBePositive { timeout: f64 }, } -static CONTROLLER: OnceCell = OnceCell::new(); +static CONTROLLER: OnceLock = OnceLock::new(); // Cardinality counter parameters, expose the internal metrics registry // cardinality. Useful for the end users to help understand the characteristics diff --git a/lib/vector-core/src/metrics/recorder.rs b/lib/vector-core/src/metrics/recorder.rs index 20a8853963fd8..4e637c9aec0c2 100644 --- a/lib/vector-core/src/metrics/recorder.rs +++ b/lib/vector-core/src/metrics/recorder.rs @@ -1,10 +1,9 @@ use std::sync::{atomic::Ordering, Arc, RwLock}; -use std::time::Duration; +use std::{cell::OnceCell, time::Duration}; use chrono::Utc; use metrics::{Counter, Gauge, Histogram, Key, KeyName, Recorder, SharedString, Unit}; use metrics_util::{registry::Registry as MetricsRegistry, MetricKindMask}; -use once_cell::unsync::OnceCell; use quanta::Clock; use super::recency::{GenerationalStorage, Recency}; diff --git a/src/aws/mod.rs b/src/aws/mod.rs index 34bd3a77db179..956b3f184897a 100644 --- a/src/aws/mod.rs +++ b/src/aws/mod.rs @@ -6,7 +6,7 @@ use std::error::Error; use std::future::Future; use std::pin::Pin; use std::sync::atomic::{AtomicUsize, Ordering}; -use std::sync::Arc; +use std::sync::{Arc, OnceLock}; use std::task::{Context, Poll}; use std::time::SystemTime; @@ -27,7 +27,6 @@ use aws_types::SdkConfig; use bytes::Bytes; use http::HeaderMap; use http_body::Body; -use once_cell::sync::OnceCell; use pin_project::pin_project; use regex::RegexSet; pub use region::RegionOrEndpoint; @@ -38,7 +37,7 @@ use crate::http::{build_proxy_connector, build_tls_connector}; use crate::internal_events::AwsBytesSent; use crate::tls::{MaybeTlsSettings, TlsConfig}; -static RETRIABLE_CODES: OnceCell = OnceCell::new(); +static RETRIABLE_CODES: OnceLock = OnceLock::new(); pub fn is_retriable_error(error: &SdkError) -> bool { match error { diff --git a/src/proto/mod.rs b/src/proto/mod.rs index efa1728fb6988..bf576c8f7e542 100644 --- a/src/proto/mod.rs +++ b/src/proto/mod.rs @@ -6,11 +6,12 @@ pub mod vector; #[cfg(feature = "sinks-datadog_metrics")] pub mod fds { - use once_cell::sync::OnceCell; + use std::sync::OnceLock; + use prost_reflect::DescriptorPool; pub fn protobuf_descriptors() -> &'static DescriptorPool { - static PROTOBUF_FDS: OnceCell = OnceCell::new(); + static PROTOBUF_FDS: OnceLock = OnceLock::new(); PROTOBUF_FDS.get_or_init(|| { DescriptorPool::decode(include_bytes!(concat!(env!("OUT_DIR"), "/protobuf-fds.bin")).as_ref()) .expect("should not fail to decode protobuf file descriptor set generated from build script") diff --git a/src/sinks/datadog/metrics/encoder.rs b/src/sinks/datadog/metrics/encoder.rs index 347271d5651ae..dfe5260897829 100644 --- a/src/sinks/datadog/metrics/encoder.rs +++ b/src/sinks/datadog/metrics/encoder.rs @@ -2,12 +2,11 @@ use std::{ cmp, io::{self, Write}, mem, - sync::Arc, + sync::{Arc, OnceLock}, }; use bytes::{BufMut, Bytes}; use chrono::{DateTime, Utc}; -use once_cell::sync::OnceCell; use prost::Message; use snafu::{ResultExt, Snafu}; use vector_common::request_metadata::GroupedCountByteSize; @@ -372,7 +371,7 @@ impl DatadogMetricsEncoder { } fn get_sketch_payload_sketches_field_number() -> u32 { - static SKETCH_PAYLOAD_SKETCHES_FIELD_NUM: OnceCell = OnceCell::new(); + static SKETCH_PAYLOAD_SKETCHES_FIELD_NUM: OnceLock = OnceLock::new(); *SKETCH_PAYLOAD_SKETCHES_FIELD_NUM.get_or_init(|| { let descriptors = protobuf_descriptors(); let descriptor = descriptors diff --git a/src/sources/kafka.rs b/src/sources/kafka.rs index 3bfbd0f09e366..56383d9f7b566 100644 --- a/src/sources/kafka.rs +++ b/src/sources/kafka.rs @@ -1,7 +1,7 @@ use std::{ collections::{BTreeMap, HashMap}, io::Cursor, - sync::Arc, + sync::{Arc, OnceLock}, time::Duration, }; @@ -14,7 +14,6 @@ use codecs::{ }; use futures::{Stream, StreamExt}; use lookup::{lookup_v2::OptionalValuePath, owned_value_path, path, OwnedValuePath}; -use once_cell::sync::OnceCell; use rdkafka::{ consumer::{CommitMode, Consumer, ConsumerContext, Rebalance, StreamConsumer}, message::{BorrowedMessage, Headers as _, Message}, @@ -727,7 +726,7 @@ fn create_consumer(config: &KafkaSourceConfig) -> crate::Result>>, + finalizer: OnceLock>>, } impl CustomContext { diff --git a/src/trace.rs b/src/trace.rs index 936d20c16a9c2..defb3997331e8 100644 --- a/src/trace.rs +++ b/src/trace.rs @@ -5,14 +5,13 @@ use std::{ str::FromStr, sync::{ atomic::{AtomicBool, Ordering}, - Mutex, MutexGuard, + Mutex, MutexGuard, OnceLock, }, }; use futures_util::{future::ready, Stream, StreamExt}; use lookup::event_path; use metrics_tracing_context::MetricsLayer; -use once_cell::sync::OnceCell; use tokio::sync::{ broadcast::{self, Receiver, Sender}, oneshot, @@ -51,7 +50,7 @@ static SUBSCRIBERS: Mutex>>>> = /// SENDER holds the sender/receiver handle that will receive a copy of all the internal log events *after* the topology /// has been initialized. -static SENDER: OnceCell> = OnceCell::new(); +static SENDER: OnceLock> = OnceLock::new(); fn metrics_layer_enabled() -> bool { !matches!(std::env::var("DISABLE_INTERNAL_METRICS_TRACING_INTEGRATION"), Ok(x) if x == "true") diff --git a/vdev/src/app.rs b/vdev/src/app.rs index 0a99eb9aa8f78..3fcf6bcf30764 100644 --- a/vdev/src/app.rs +++ b/vdev/src/app.rs @@ -1,13 +1,14 @@ use std::ffi::{OsStr, OsString}; pub use std::process::Command; use std::{ - borrow::Cow, env, io::Read, path::PathBuf, process::ExitStatus, process::Stdio, time::Duration, + borrow::Cow, env, io::Read, path::PathBuf, process::ExitStatus, process::Stdio, sync::OnceLock, + time::Duration, }; use anyhow::{bail, Context as _, Result}; use indicatif::{ProgressBar, ProgressStyle}; use log::LevelFilter; -use once_cell::sync::{Lazy, OnceCell}; +use once_cell::sync::Lazy; use crate::{config::Config, git, platform, util}; @@ -25,9 +26,9 @@ const DEFAULT_SHELL: &str = "/bin/sh"; pub static SHELL: Lazy = Lazy::new(|| (env::var_os("SHELL").unwrap_or_else(|| DEFAULT_SHELL.into()))); -static VERBOSITY: OnceCell = OnceCell::new(); -static CONFIG: OnceCell = OnceCell::new(); -static PATH: OnceCell = OnceCell::new(); +static VERBOSITY: OnceLock = OnceLock::new(); +static CONFIG: OnceLock = OnceLock::new(); +static PATH: OnceLock = OnceLock::new(); pub fn verbosity() -> &'static LevelFilter { VERBOSITY.get().expect("verbosity is not initialized")