Skip to content

Commit

Permalink
Replace lazy_static and once_cell with std library equivalents (#1212)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
dannycjones authored Jan 10, 2025
1 parent 3da84c5 commit 456c7de
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 32 deletions.
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions mountpoint-s3-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 3 additions & 5 deletions mountpoint-s3-client/src/endpoint_config.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
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},
s3::endpoint_resolver::{RequestContext, ResolvedEndpoint, ResolverError, RuleEngine},
};
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<RuleEngine> = LazyLock::new(|| RuleEngine::new(&Default::default()).unwrap());

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum AddressingStyle {
Expand Down
6 changes: 3 additions & 3 deletions mountpoint-s3-client/src/instance_info.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -12,15 +12,15 @@ 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<Result<IdentityDocument, InstanceInfoError>>,
document: LazyCell<Result<IdentityDocument, InstanceInfoError>>,
}

impl InstanceInfo {
/// Create a new instance. The IMDS client will only be queried when a method on the instance is
/// 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) => {
Expand Down
11 changes: 4 additions & 7 deletions mountpoint-s3-client/src/mock_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,11 +53,9 @@ pub fn ramp_bytes(seed: usize, size: usize) -> Vec<u8> {
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<u8> = 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<Vec<u8>> = LazyLock::new(|| ramp_bytes(0, RAMP_BUFFER_SIZE + RAMP_MODULUS));

#[derive(Debug, Default, Clone)]
pub struct MockClientConfig {
Expand Down
22 changes: 12 additions & 10 deletions mountpoint-s3-client/src/s3_crt_client/head_object.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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="(?<ongoing>[^"]*)"$"#).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="(?<expiry>[^"]*)"$"#).unwrap();
}
/// Regex for determining if a restore is ongoing.
///
/// Example: `ongoing-request="true"`
static RESTORE_IN_PROGRESS_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r#"^ongoing-request="(?<ongoing>[^"]*)"$"#).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<Regex> =
LazyLock::new(|| Regex::new(r#"^ongoing-request="[^"]*",\s*expiry-date="(?<expiry>[^"]*)"$"#).unwrap());

impl HeadObjectResult {
fn parse_restore_status(headers: &Headers) -> Result<Option<RestoreStatus>, ParseError> {
Expand Down
5 changes: 2 additions & 3 deletions mountpoint-s3-client/tests/common/tracing_test.rs
Original file line number Diff line number Diff line change
@@ -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<TracingTestLayer> = Lazy::new(TracingTestLayer::new);
static TRACING_TEST_LAYER: LazyLock<TracingTestLayer> = LazyLock::new(TracingTestLayer::new);

/// This is a singleton [tracing::Layer] that can be used to write tests for log events.
///
Expand Down

0 comments on commit 456c7de

Please sign in to comment.