Skip to content

Commit 9d0e0ca

Browse files
authored
Replace stabilized once_cell api (#1477)
- **Replace once_cell::sync::Lazy with std::sync::LazyLock** - **Replace once_cell::sync::OnceCell with std::sync::OnceLock, unless used with get_or_try_init**
1 parent 6890ee2 commit 9d0e0ca

File tree

11 files changed

+32
-42
lines changed

11 files changed

+32
-42
lines changed

Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scarb/src/core/manifest/summary.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::ops::Deref;
2-
use std::sync::Arc;
2+
use std::sync::{Arc, LazyLock};
33

4-
use once_cell::sync::Lazy;
54
use typed_builder::TypedBuilder;
65

76
#[cfg(doc)]
@@ -68,7 +67,7 @@ impl Summary {
6867
}
6968

7069
pub fn implicit_dependencies(&self) -> impl Iterator<Item = &ManifestDependency> {
71-
static CORE_DEPENDENCY: Lazy<ManifestDependency> = Lazy::new(|| {
70+
static CORE_DEPENDENCY: LazyLock<ManifestDependency> = LazyLock::new(|| {
7271
// NOTE: Pin `core` to exact version, because we know that's the only one we have.
7372
let cairo_version = crate::version::get().cairo.version.parse().unwrap();
7473
ManifestDependency::builder()

scarb/src/core/source/id.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use std::fmt;
22
use std::hash::{Hash, Hasher};
33
use std::ops::Deref;
4-
use std::sync::Arc;
4+
use std::sync::{Arc, LazyLock};
55

66
use anyhow::{anyhow, bail, Context, Result};
77
use camino::{Utf8Path, Utf8PathBuf};
8-
use once_cell::sync::Lazy;
98
use serde::{Deserialize, Deserializer, Serialize, Serializer};
109
use smol_str::SmolStr;
1110
use url::Url;
@@ -228,15 +227,15 @@ impl SourceId {
228227
}
229228

230229
pub fn for_std() -> Self {
231-
static CACHE: Lazy<SourceId> = Lazy::new(|| {
230+
static CACHE: LazyLock<SourceId> = LazyLock::new(|| {
232231
let url = Url::parse("scarb:/std").unwrap();
233232
SourceId::new(url, SourceKind::Std).unwrap()
234233
});
235234
*CACHE
236235
}
237236

238237
pub fn default_registry() -> Self {
239-
static CACHE: Lazy<SourceId> = Lazy::new(|| {
238+
static CACHE: LazyLock<SourceId> = LazyLock::new(|| {
240239
let url = Url::parse(DEFAULT_REGISTRY_INDEX).unwrap();
241240
SourceId::new(url, SourceKind::Registry).unwrap()
242241
});

scarb/src/internal/static_hash_cache.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
use std::collections::HashSet;
22
use std::hash::Hash;
3-
use std::sync::Mutex;
3+
use std::sync::{Mutex, OnceLock};
44

5-
use once_cell::sync::OnceCell;
6-
7-
pub struct StaticHashCache<T: 'static + Eq + Hash>(OnceCell<Mutex<HashSet<&'static T>>>);
5+
pub struct StaticHashCache<T: 'static + Eq + Hash>(OnceLock<Mutex<HashSet<&'static T>>>);
86

97
impl<T: 'static + Eq + Hash> StaticHashCache<T> {
108
pub const fn new() -> Self {
11-
Self(OnceCell::new())
9+
Self(OnceLock::new())
1210
}
1311

1412
pub fn intern(&self, value: T) -> &'static T {

scarb/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#![warn(rust_2018_idioms)]
1010

1111
use camino::Utf8PathBuf;
12-
use once_cell::sync::Lazy;
12+
use std::sync::LazyLock;
1313
pub use subcommands::EXTERNAL_CMD_PREFIX;
1414

1515
pub mod compiler;
@@ -32,8 +32,8 @@ pub const DEFAULT_MODULE_MAIN_FILE: &str = "lib.cairo";
3232
pub const DEFAULT_TESTS_PATH: &str = "tests";
3333
pub const DEFAULT_TARGET_DIR_NAME: &str = "target";
3434
pub const SCARB_IGNORE_FILE_NAME: &str = ".scarbignore";
35-
pub static DEFAULT_SOURCE_PATH: Lazy<Utf8PathBuf> =
36-
Lazy::new(|| ["src", "lib.cairo"].iter().collect());
35+
pub static DEFAULT_SOURCE_PATH: LazyLock<Utf8PathBuf> =
36+
LazyLock::new(|| ["src", "lib.cairo"].iter().collect());
3737
pub const DEFAULT_README_FILE_NAME: &str = "README.md";
3838
pub const DEFAULT_LICENSE_FILE_NAME: &str = "LICENSE";
3939
pub const STARKNET_PLUGIN_NAME: &str = "starknet";

scarb/src/version.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
//! Version information about Scarb and Cairo.
22
3-
use std::fmt;
4-
use std::fmt::Write;
5-
63
use indoc::formatdoc;
7-
use once_cell::sync::Lazy;
84
use serde::{Deserialize, Serialize};
5+
use std::fmt;
6+
use std::fmt::Write;
7+
use std::sync::LazyLock;
98

109
use scarb_build_metadata::{
1110
CommitHash, CAIRO_COMMIT_HASH, CAIRO_VERSION, SCARB_COMMIT_DATE, SCARB_COMMIT_HASH,
@@ -131,7 +130,7 @@ pub fn get() -> VersionInfo {
131130
}
132131
};
133132

134-
static SIERRA_VERSION: Lazy<String> = Lazy::new(|| {
133+
static SIERRA_VERSION: LazyLock<String> = LazyLock::new(|| {
135134
cairo_lang_starknet_classes::compiler_version::current_sierra_version_id().to_string()
136135
});
137136

scarb/tests/build_cairo_plugin.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@ use assert_fs::TempDir;
33
use cairo_lang_sierra::program::VersionedProgram;
44
use camino::Utf8PathBuf;
55
use indoc::{formatdoc, indoc};
6-
use once_cell::sync::Lazy;
7-
use snapbox::assert_matches;
8-
use std::collections::HashMap;
9-
use std::path::PathBuf;
10-
116
use scarb_test_support::command::Scarb;
127
use scarb_test_support::fsx;
138
use scarb_test_support::fsx::ChildPathEx;
149
use scarb_test_support::project_builder::ProjectBuilder;
10+
use snapbox::assert_matches;
11+
use std::collections::HashMap;
12+
use std::path::PathBuf;
13+
use std::sync::LazyLock;
1514

16-
static CAIRO_LANG_MACRO_PATH: Lazy<String> = Lazy::new(|| {
15+
static CAIRO_LANG_MACRO_PATH: LazyLock<String> = LazyLock::new(|| {
1716
let path = fsx::canonicalize(
1817
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
1918
.join("../plugins/")

utils/scarb-test-support/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ dunce.workspace = true
1515
hyper = "0.14"
1616
indoc.workspace = true
1717
itertools.workspace = true
18-
once_cell.workspace = true
1918
scarb = { path = "../../scarb" }
2019
scarb-build-metadata = { path = "../scarb-build-metadata" }
2120
scarb-ui = { path = "../scarb-ui" }

utils/scarb-test-support/src/command.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1+
use assert_fs::prelude::*;
2+
use assert_fs::TempDir;
3+
use serde::de::DeserializeOwned;
4+
use snapbox::cmd::Command as SnapboxCommand;
15
use std::ffi::OsString;
26
use std::io::BufRead;
37
use std::path::{Path, PathBuf};
48
use std::process::Command as StdCommand;
9+
use std::sync::LazyLock;
510
use std::{fs, iter};
611

7-
use assert_fs::prelude::*;
8-
use assert_fs::TempDir;
9-
use once_cell::sync::Lazy;
10-
use serde::de::DeserializeOwned;
11-
use snapbox::cmd::Command as SnapboxCommand;
12-
1312
use crate::cargo::cargo_bin;
1413
use scarb::core::Config;
1514
use scarb_ui::Verbosity;
@@ -60,7 +59,7 @@ impl Scarb {
6059

6160
pub fn isolate_from_extensions(self) -> Self {
6261
// NOTE: We keep TempDir instance in static, so that it'll be dropped when program ends.
63-
static ISOLATE: Lazy<(PathBuf, TempDir)> = Lazy::new(|| {
62+
static ISOLATE: LazyLock<(PathBuf, TempDir)> = LazyLock::new(|| {
6463
let t = TempDir::new().unwrap();
6564
let source_bin = cargo_bin("scarb");
6665
let output_bin = t.child(source_bin.file_name().unwrap()).to_path_buf();

utils/scarb-test-support/src/manifest_edit.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
use std::cell::LazyCell;
12
use std::ffi::OsStr;
23
use std::fs;
34

45
use assert_fs::fixture::ChildPath;
56
use assert_fs::prelude::*;
67
use assert_fs::TempDir;
7-
use once_cell::sync::Lazy;
88
use snapbox::cmd::Command;
99

1010
use crate::command::Scarb;
@@ -35,7 +35,7 @@ impl ManifestEditHarness {
3535
}
3636

3737
pub fn run(self) {
38-
let t = Lazy::new(|| TempDir::new().unwrap());
38+
let t = LazyCell::new(|| TempDir::new().unwrap());
3939
let t = self.path.unwrap_or_else(|| t.child("proj"));
4040

4141
let input_manifest = self.input_manifest.unwrap();

utils/scarb-test-support/src/registry/http.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
use std::fmt;
2-
use std::path::Path;
3-
41
use assert_fs::fixture::ChildPath;
52
use assert_fs::prelude::*;
63
use assert_fs::TempDir;
7-
use once_cell::sync::Lazy;
84
use serde_json::json;
5+
use std::fmt;
6+
use std::path::Path;
7+
use std::sync::LazyLock;
98
use tokio::runtime;
109

1110
use crate::registry::local::LocalRegistry;
1211
use crate::simple_http_server::SimpleHttpServer;
1312

1413
// Keep a global multi-threading runtime to contain all running servers in one shared
1514
// thread pool, while maintaining synchronous nature of tests.
16-
static RUNTIME: Lazy<runtime::Runtime> = Lazy::new(|| {
15+
static RUNTIME: LazyLock<runtime::Runtime> = LazyLock::new(|| {
1716
runtime::Builder::new_multi_thread()
1817
.worker_threads(1)
1918
.enable_all()

0 commit comments

Comments
 (0)