Skip to content

Commit

Permalink
Migrate from lazy_static to once_cell (TraceMachina#377)
Browse files Browse the repository at this point in the history
The latter is more idiomatic and will soon be integrated into the Rust
standard library.
  • Loading branch information
aaronmondal authored Nov 5, 2023
1 parent dd99eb0 commit c4b296c
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 39 deletions.
10 changes: 5 additions & 5 deletions Cargo.Bazel.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"checksum": "ffa058436431ad11db44bfbd1a61c7e419838e474661896794dc25b1d63f9993",
"checksum": "e639852098069997f943c9f51c62356a6faa0254614942e6d6d6fc71174e5a39",
"crates": {
"addr2line 0.21.0": {
"name": "addr2line",
Expand Down Expand Up @@ -2634,10 +2634,6 @@
"id": "json5 0.4.1",
"target": "json5"
},
{
"id": "lazy_static 1.4.0",
"target": "lazy_static"
},
{
"id": "log 0.4.20",
"target": "log"
Expand Down Expand Up @@ -2666,6 +2662,10 @@
"id": "nix 0.26.4",
"target": "nix"
},
{
"id": "once_cell 1.18.0",
"target": "once_cell"
},
{
"id": "parking_lot 0.12.1",
"target": "parking_lot"
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ hex = "0.4.3"
http = "^0.2"
hyper = "0.14.27"
json5 = "0.4.1"
lazy_static = "1.4.0"
log = "0.4.19"
lru = "0.10.1"
lz4_flex = "0.11.1"
maplit = "1.0.2"
memory-stats = "1.1.0"
mock_instant = "0.3.1"
nix = "0.26.2"
once_cell = "1.18.0"
parking_lot = "0.12.1"
pin-project-lite = "0.2.10"
pretty_assertions = "1.4.0"
Expand Down
12 changes: 6 additions & 6 deletions cas/store/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ rust_library(
"//util:error",
"//util:fastcdc",
"@crate_index//:bincode",
"@crate_index//:blake3",
"@crate_index//:futures",
"@crate_index//:serde",
"@crate_index//:blake3",
"@crate_index//:tokio",
"@crate_index//:tokio-util",
],
Expand Down Expand Up @@ -239,7 +239,7 @@ rust_library(
"@crate_index//:bytes",
"@crate_index//:futures",
"@crate_index//:http",
"@crate_index//:lazy_static",
"@crate_index//:once_cell",
"@crate_index//:rand",
"@crate_index//:rusoto_core",
"@crate_index//:rusoto_s3",
Expand Down Expand Up @@ -357,8 +357,8 @@ rust_test(
name = "shard_store_test",
srcs = ["tests/shard_store_test.rs"],
deps = [
":shard_store",
":memory_store",
":shard_store",
":traits",
"//config",
"//util:common",
Expand All @@ -373,7 +373,7 @@ rust_test(
name = "memory_store_test",
srcs = ["tests/memory_store_test.rs"],
env = {
"RUST_TEST_THREADS": "1", # This test can't run in parallel.
"RUST_TEST_THREADS": "1", # This test can't run in parallel.
},
deps = [
":memory_store",
Expand Down Expand Up @@ -464,6 +464,7 @@ rust_test(
rust_test(
name = "filesystem_store_test",
srcs = ["tests/filesystem_store_test.rs"],
proc_macro_deps = ["@crate_index//:async-trait"],
deps = [
":filesystem_store",
":traits",
Expand All @@ -475,13 +476,12 @@ rust_test(
"@crate_index//:async-lock",
"@crate_index//:filetime",
"@crate_index//:futures",
"@crate_index//:lazy_static",
"@crate_index//:once_cell",
"@crate_index//:pretty_assertions",
"@crate_index//:rand",
"@crate_index//:tokio",
"@crate_index//:tokio-stream",
],
proc_macro_deps = ["@crate_index//:async-trait"],
)

rust_test(
Expand Down
7 changes: 3 additions & 4 deletions cas/store/s3_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use futures::future::{try_join_all, FutureExt};
use futures::stream::{unfold, FuturesUnordered};
use futures::TryStreamExt;
use http::status::StatusCode;
use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use rand::{rngs::OsRng, Rng};
use rusoto_core::credential::DefaultCredentialsProvider;
use rusoto_core::request::{DispatchSignedRequest, DispatchSignedRequestFuture};
Expand Down Expand Up @@ -92,9 +92,8 @@ impl DispatchSignedRequest for ThrottledDispatcher {
}
}

lazy_static! {
static ref SHARED_CLIENT: Mutex<Arc<ThrottledDispatcher>> = Mutex::new(Arc::new(ThrottledDispatcher::new()));
}
static SHARED_CLIENT: Lazy<Mutex<Arc<ThrottledDispatcher>>> =
Lazy::new(|| Mutex::new(Arc::new(ThrottledDispatcher::new())));

fn should_retry<T, E>(result: Result<T, RusotoError<E>>) -> RetryResult<T>
where
Expand Down
10 changes: 3 additions & 7 deletions cas/store/tests/filesystem_store_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use filetime::{set_file_atime, FileTime};
use futures::executor::block_on;
use futures::task::Poll;
use futures::{poll, Future};
use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use rand::{thread_rng, Rng};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::sync::Barrier;
Expand Down Expand Up @@ -703,9 +703,7 @@ mod filesystem_store_tests {
let small_digest = DigestInfo::try_new(HASH1, VALUE1.len())?;
let big_digest = DigestInfo::try_new(HASH1, BIG_VALUE.len())?;

lazy_static! {
static ref UNREFED_DIGESTS: Mutex<Vec<DigestInfo>> = Mutex::new(Vec::new());
}
static UNREFED_DIGESTS: Lazy<Mutex<Vec<DigestInfo>>> = Lazy::new(|| Mutex::new(Vec::new()));
struct LocalHooks {}
impl FileEntryHooks for LocalHooks {
fn on_unref<Fe: FileEntry>(file_entry: &Fe) {
Expand Down Expand Up @@ -753,9 +751,7 @@ mod filesystem_store_tests {
let content_path = make_temp_path("content_path");
let temp_path = make_temp_path("temp_path");

lazy_static! {
static ref FILE_DELETED_BARRIER: Arc<Barrier> = Arc::new(Barrier::new(2));
}
static FILE_DELETED_BARRIER: Lazy<Arc<Barrier>> = Lazy::new(|| Arc::new(Barrier::new(2)));

struct LocalHooks {}
impl FileEntryHooks for LocalHooks {
Expand Down
4 changes: 2 additions & 2 deletions cas/worker/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ rust_library(
srcs = ["running_actions_manager.rs"],
proc_macro_deps = ["@crate_index//:async-trait"],
deps = [
"//config",
"//cas/scheduler:action_messages",
"//cas/store",
"//cas/store:ac_utils",
"//cas/store:fast_slow_store",
"//cas/store:filesystem_store",
"//cas/store:grpc_store",
"//config",
"//proto",
"//util:common",
"//util:error",
Expand Down Expand Up @@ -147,7 +147,7 @@ rust_test(
"//util:common",
"//util:error",
"@crate_index//:futures",
"@crate_index//:lazy_static",
"@crate_index//:once_cell",
"@crate_index//:pretty_assertions",
"@crate_index//:prost",
"@crate_index//:prost-types",
Expand Down
12 changes: 5 additions & 7 deletions cas/worker/tests/running_actions_manager_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use futures::{FutureExt, TryFutureExt};
use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use prost::Message;
use rand::{thread_rng, Rng};
use tokio::sync::oneshot;
Expand Down Expand Up @@ -2047,12 +2047,10 @@ exit 1
}

type StaticOneshotTuple = Mutex<(Option<oneshot::Sender<()>>, Option<oneshot::Receiver<()>>)>;
lazy_static! {
static ref TIMEOUT_ONESHOT: StaticOneshotTuple = {
let (tx, rx) = oneshot::channel();
Mutex::new((Some(tx), Some(rx)))
};
}
static TIMEOUT_ONESHOT: Lazy<StaticOneshotTuple> = Lazy::new(|| {
let (tx, rx) = oneshot::channel();
Mutex::new((Some(tx), Some(rx)))
});
let root_work_directory = make_temp_path("root_work_directory");
fs::create_dir_all(&root_work_directory).await?;

Expand Down
2 changes: 1 addition & 1 deletion gencargo/filesystem_store_test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async-lock = { workspace = true }
async-trait = { workspace = true }
filetime = { workspace = true }
futures = { workspace = true }
lazy_static = { workspace = true }
once_cell = { workspace = true }
pretty_assertions = { workspace = true }
rand = { workspace = true }
tokio = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion gencargo/running_actions_manager_test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ doctest = false

[dependencies]
futures = { workspace = true }
lazy_static = { workspace = true }
once_cell = { workspace = true }
pretty_assertions = { workspace = true }
prost = { workspace = true }
prost-types = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion gencargo/s3_store/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async-trait = { workspace = true }
bytes = { workspace = true }
futures = { workspace = true }
http = { workspace = true }
lazy_static = { workspace = true }
once_cell = { workspace = true }
rand = { workspace = true }
rusoto_core = { workspace = true }
rusoto_s3 = { workspace = true }
Expand Down
6 changes: 3 additions & 3 deletions tools/cargo_shared.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,6 @@ PACKAGES = {
"byteorder": {
"version": "1.4.3",
},
"lazy_static": {
"version": "1.4.0",
},
"filetime": {
"version": "0.2.21",
},
Expand Down Expand Up @@ -179,4 +176,7 @@ PACKAGES = {
"formatx": {
"version": "0.2.1",
},
"once_cell": {
"version": "1.18.0",
},
}

0 comments on commit c4b296c

Please sign in to comment.