From 456c7dedee67dc50d10c9a5c4716f2fffaf1d406 Mon Sep 17 00:00:00 2001 From: Daniel Carl Jones Date: Fri, 10 Jan 2025 16:15:10 +0000 Subject: [PATCH] Replace lazy_static and once_cell with std library equivalents (#1212) The types provided by `lazy_static` and `once_cell` have now been added to the standard library as of Rust 1.80.0 (https://blog.rust-lang.org/2024/07/25/Rust-1.80.0.html#lazycell-and-lazylock), and we no longer need to use these crates for this functionality. This change removes those dependencies, and updates our code to use the new standard types. ### Does this change impact existing behavior? No change in existing behavior. ### Does this change need a changelog entry? No, no behavior change. --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license and I agree to the terms of the [Developer Certificate of Origin (DCO)](https://developercertificate.org/). Signed-off-by: Daniel Carl Jones --- Cargo.lock | 2 -- mountpoint-s3-client/Cargo.toml | 2 -- mountpoint-s3-client/src/endpoint_config.rs | 8 +++---- mountpoint-s3-client/src/instance_info.rs | 6 ++--- mountpoint-s3-client/src/mock_client.rs | 11 ++++------ .../src/s3_crt_client/head_object.rs | 22 ++++++++++--------- .../tests/common/tracing_test.rs | 5 ++--- 7 files changed, 24 insertions(+), 32 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d21f2e462..a43820d53 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2588,13 +2588,11 @@ dependencies = [ "const_format", "ctor", "futures", - "lazy_static", "md-5", "metrics", "mountpoint-s3-client", "mountpoint-s3-crt", "mountpoint-s3-crt-sys", - "once_cell", "percent-encoding", "pin-project", "platform-info", diff --git a/mountpoint-s3-client/Cargo.toml b/mountpoint-s3-client/Cargo.toml index ee493ff2a..0fe7811df 100644 --- a/mountpoint-s3-client/Cargo.toml +++ b/mountpoint-s3-client/Cargo.toml @@ -16,9 +16,7 @@ auto_impl = "1.2.0" base64ct = { version = "1.6.0", features = ["std"] } const_format = "0.2.34" futures = "0.3.31" -lazy_static = "1.5.0" metrics = "0.24.1" -once_cell = "1.20.2" percent-encoding = "2.3.1" pin-project = "1.1.7" platform-info = "2.0.4" diff --git a/mountpoint-s3-client/src/endpoint_config.rs b/mountpoint-s3-client/src/endpoint_config.rs index a27c30365..2b8b9585e 100644 --- a/mountpoint-s3-client/src/endpoint_config.rs +++ b/mountpoint-s3-client/src/endpoint_config.rs @@ -1,6 +1,6 @@ use std::os::unix::prelude::OsStrExt; +use std::sync::LazyLock; -use lazy_static::lazy_static; use mountpoint_s3_crt::{ auth::signing_config::SigningAlgorithm, common::{allocator::Allocator, uri::Uri}, @@ -8,10 +8,8 @@ use mountpoint_s3_crt::{ }; use thiserror::Error; -lazy_static! { - // A static s3 endpoint rule engine that can be shared across s3 client - static ref S3_ENDPOINT_RULE_ENGINE: RuleEngine = RuleEngine::new(&Default::default()).unwrap(); -} +/// A static s3 endpoint rule engine that can be shared across s3 client +static S3_ENDPOINT_RULE_ENGINE: LazyLock = LazyLock::new(|| RuleEngine::new(&Default::default()).unwrap()); #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] pub enum AddressingStyle { diff --git a/mountpoint-s3-client/src/instance_info.rs b/mountpoint-s3-client/src/instance_info.rs index a290d3dd3..04da831e2 100644 --- a/mountpoint-s3-client/src/instance_info.rs +++ b/mountpoint-s3-client/src/instance_info.rs @@ -1,9 +1,9 @@ //! A simple interface to retrieve information about the EC2 instance this client is running on by //! querying the Instance Metadata Service (IMDS). +use std::cell::LazyCell; use std::env; -use once_cell::unsync::Lazy; use thiserror::Error; use crate::imds_crt_client::{IdentityDocument, ImdsCrtClient, ImdsQueryRequestError}; @@ -12,7 +12,7 @@ use crate::imds_crt_client::{IdentityDocument, ImdsCrtClient, ImdsQueryRequestEr /// the `AWS_EC2_METADATA_DISABLED` environment variable is not set. #[derive(Debug)] pub struct InstanceInfo { - document: Lazy>, + document: LazyCell>, } impl InstanceInfo { @@ -20,7 +20,7 @@ impl InstanceInfo { /// called, and only if the `AWS_EC2_METADATA_DISABLED` environment variable is not set. pub fn new() -> Self { Self { - document: Lazy::new(|| { + document: LazyCell::new(|| { if !imds_disabled() { match retrieve_instance_identity_document() { Ok(identity_document) => { diff --git a/mountpoint-s3-client/src/mock_client.rs b/mountpoint-s3-client/src/mock_client.rs index 2d3dccf3a..30bfb1abd 100644 --- a/mountpoint-s3-client/src/mock_client.rs +++ b/mountpoint-s3-client/src/mock_client.rs @@ -7,13 +7,12 @@ use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; use std::fmt::Write; use std::pin::Pin; use std::sync::atomic::{AtomicU64, Ordering}; -use std::sync::{Arc, RwLock}; +use std::sync::{Arc, LazyLock, RwLock}; use std::task::{Context, Poll}; use std::time::{Duration, SystemTime}; use async_trait::async_trait; use futures::{Stream, StreamExt}; -use lazy_static::lazy_static; use mountpoint_s3_crt::s3::client::BufferPoolUsageStats; use rand::seq::SliceRandom; use rand::SeedableRng; @@ -54,11 +53,9 @@ pub fn ramp_bytes(seed: usize, size: usize) -> Vec { bytes } -lazy_static! { - // Generate bytes for ramping pattern (currently, just a simple linear ramp). - // Request RAMP_MODULUS extra bytes so we can read from any offset (modulo RAMP_MODULUS) - static ref RAMP_BYTES: Vec = ramp_bytes(0, RAMP_BUFFER_SIZE + RAMP_MODULUS); -} +/// Generate bytes for ramping pattern (currently, just a simple linear ramp). +/// Request RAMP_MODULUS extra bytes so we can read from any offset (modulo RAMP_MODULUS) +static RAMP_BYTES: LazyLock> = LazyLock::new(|| ramp_bytes(0, RAMP_BUFFER_SIZE + RAMP_MODULUS)); #[derive(Debug, Default, Clone)] pub struct MockClientConfig { diff --git a/mountpoint-s3-client/src/s3_crt_client/head_object.rs b/mountpoint-s3-client/src/s3_crt_client/head_object.rs index 35a04f07d..508eff8da 100644 --- a/mountpoint-s3-client/src/s3_crt_client/head_object.rs +++ b/mountpoint-s3-client/src/s3_crt_client/head_object.rs @@ -1,7 +1,6 @@ use std::str::FromStr; -use std::sync::{Arc, Mutex}; +use std::sync::{Arc, LazyLock, Mutex}; -use lazy_static::lazy_static; use mountpoint_s3_crt::http::request_response::{Header, Headers, HeadersError}; use mountpoint_s3_crt::s3::client::MetaRequestResult; use regex::Regex; @@ -33,14 +32,17 @@ pub enum ParseError { InvalidRestore(String), } -lazy_static! { - // Example: ongoing-request="true" - static ref RESTORE_IN_PROGRESS_RE: Regex = Regex::new(r#"^ongoing-request="(?[^"]*)"$"#).unwrap(); - - // Example: ongoing-request="false", expiry-date="Fri, 21 Dec 2012 00:00:00 GMT" - static ref RESTORE_DONE_RE: Regex = - Regex::new(r#"^ongoing-request="[^"]*",\s*expiry-date="(?[^"]*)"$"#).unwrap(); -} +/// Regex for determining if a restore is ongoing. +/// +/// Example: `ongoing-request="true"` +static RESTORE_IN_PROGRESS_RE: LazyLock = + LazyLock::new(|| Regex::new(r#"^ongoing-request="(?[^"]*)"$"#).unwrap()); + +/// Regex for determining a restore is complete. +/// +/// Example: `ongoing-request="false", expiry-date="Fri, 21 Dec 2012 00:00:00 GMT"` +static RESTORE_DONE_RE: LazyLock = + LazyLock::new(|| Regex::new(r#"^ongoing-request="[^"]*",\s*expiry-date="(?[^"]*)"$"#).unwrap()); impl HeadObjectResult { fn parse_restore_status(headers: &Headers) -> Result, ParseError> { diff --git a/mountpoint-s3-client/tests/common/tracing_test.rs b/mountpoint-s3-client/tests/common/tracing_test.rs index feb4e23cd..856eaf70d 100644 --- a/mountpoint-s3-client/tests/common/tracing_test.rs +++ b/mountpoint-s3-client/tests/common/tracing_test.rs @@ -1,14 +1,13 @@ use std::sync::atomic::{AtomicBool, Ordering}; -use std::sync::{Arc, Mutex}; +use std::sync::{Arc, LazyLock, Mutex}; -use once_cell::sync::Lazy; use tracing::{Event, Level, Subscriber}; use tracing_subscriber::field::VisitOutput; use tracing_subscriber::fmt::format::{DefaultVisitor, Writer}; use tracing_subscriber::layer::Context; use tracing_subscriber::Layer; -static TRACING_TEST_LAYER: Lazy = Lazy::new(TracingTestLayer::new); +static TRACING_TEST_LAYER: LazyLock = LazyLock::new(TracingTestLayer::new); /// This is a singleton [tracing::Layer] that can be used to write tests for log events. ///