Skip to content

Commit

Permalink
Replace stabilized once_cell api (#1477)
Browse files Browse the repository at this point in the history
- **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**
  • Loading branch information
maciektr committed Jul 26, 2024
1 parent 6890ee2 commit 9d0e0ca
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 42 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

5 changes: 2 additions & 3 deletions scarb/src/core/manifest/summary.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::ops::Deref;
use std::sync::Arc;
use std::sync::{Arc, LazyLock};

use once_cell::sync::Lazy;
use typed_builder::TypedBuilder;

#[cfg(doc)]
Expand Down Expand Up @@ -68,7 +67,7 @@ impl Summary {
}

pub fn implicit_dependencies(&self) -> impl Iterator<Item = &ManifestDependency> {
static CORE_DEPENDENCY: Lazy<ManifestDependency> = Lazy::new(|| {
static CORE_DEPENDENCY: LazyLock<ManifestDependency> = LazyLock::new(|| {
// NOTE: Pin `core` to exact version, because we know that's the only one we have.
let cairo_version = crate::version::get().cairo.version.parse().unwrap();
ManifestDependency::builder()
Expand Down
7 changes: 3 additions & 4 deletions scarb/src/core/source/id.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use std::fmt;
use std::hash::{Hash, Hasher};
use std::ops::Deref;
use std::sync::Arc;
use std::sync::{Arc, LazyLock};

use anyhow::{anyhow, bail, Context, Result};
use camino::{Utf8Path, Utf8PathBuf};
use once_cell::sync::Lazy;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use smol_str::SmolStr;
use url::Url;
Expand Down Expand Up @@ -228,15 +227,15 @@ impl SourceId {
}

pub fn for_std() -> Self {
static CACHE: Lazy<SourceId> = Lazy::new(|| {
static CACHE: LazyLock<SourceId> = LazyLock::new(|| {
let url = Url::parse("scarb:/std").unwrap();
SourceId::new(url, SourceKind::Std).unwrap()
});
*CACHE
}

pub fn default_registry() -> Self {
static CACHE: Lazy<SourceId> = Lazy::new(|| {
static CACHE: LazyLock<SourceId> = LazyLock::new(|| {
let url = Url::parse(DEFAULT_REGISTRY_INDEX).unwrap();
SourceId::new(url, SourceKind::Registry).unwrap()
});
Expand Down
8 changes: 3 additions & 5 deletions scarb/src/internal/static_hash_cache.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use std::collections::HashSet;
use std::hash::Hash;
use std::sync::Mutex;
use std::sync::{Mutex, OnceLock};

use once_cell::sync::OnceCell;

pub struct StaticHashCache<T: 'static + Eq + Hash>(OnceCell<Mutex<HashSet<&'static T>>>);
pub struct StaticHashCache<T: 'static + Eq + Hash>(OnceLock<Mutex<HashSet<&'static T>>>);

impl<T: 'static + Eq + Hash> StaticHashCache<T> {
pub const fn new() -> Self {
Self(OnceCell::new())
Self(OnceLock::new())
}

pub fn intern(&self, value: T) -> &'static T {
Expand Down
6 changes: 3 additions & 3 deletions scarb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#![warn(rust_2018_idioms)]

use camino::Utf8PathBuf;
use once_cell::sync::Lazy;
use std::sync::LazyLock;
pub use subcommands::EXTERNAL_CMD_PREFIX;

pub mod compiler;
Expand All @@ -32,8 +32,8 @@ pub const DEFAULT_MODULE_MAIN_FILE: &str = "lib.cairo";
pub const DEFAULT_TESTS_PATH: &str = "tests";
pub const DEFAULT_TARGET_DIR_NAME: &str = "target";
pub const SCARB_IGNORE_FILE_NAME: &str = ".scarbignore";
pub static DEFAULT_SOURCE_PATH: Lazy<Utf8PathBuf> =
Lazy::new(|| ["src", "lib.cairo"].iter().collect());
pub static DEFAULT_SOURCE_PATH: LazyLock<Utf8PathBuf> =
LazyLock::new(|| ["src", "lib.cairo"].iter().collect());
pub const DEFAULT_README_FILE_NAME: &str = "README.md";
pub const DEFAULT_LICENSE_FILE_NAME: &str = "LICENSE";
pub const STARKNET_PLUGIN_NAME: &str = "starknet";
Expand Down
9 changes: 4 additions & 5 deletions scarb/src/version.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
//! Version information about Scarb and Cairo.

use std::fmt;
use std::fmt::Write;

use indoc::formatdoc;
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
use std::fmt;
use std::fmt::Write;
use std::sync::LazyLock;

use scarb_build_metadata::{
CommitHash, CAIRO_COMMIT_HASH, CAIRO_VERSION, SCARB_COMMIT_DATE, SCARB_COMMIT_HASH,
Expand Down Expand Up @@ -131,7 +130,7 @@ pub fn get() -> VersionInfo {
}
};

static SIERRA_VERSION: Lazy<String> = Lazy::new(|| {
static SIERRA_VERSION: LazyLock<String> = LazyLock::new(|| {
cairo_lang_starknet_classes::compiler_version::current_sierra_version_id().to_string()
});

Expand Down
11 changes: 5 additions & 6 deletions scarb/tests/build_cairo_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ use assert_fs::TempDir;
use cairo_lang_sierra::program::VersionedProgram;
use camino::Utf8PathBuf;
use indoc::{formatdoc, indoc};
use once_cell::sync::Lazy;
use snapbox::assert_matches;
use std::collections::HashMap;
use std::path::PathBuf;

use scarb_test_support::command::Scarb;
use scarb_test_support::fsx;
use scarb_test_support::fsx::ChildPathEx;
use scarb_test_support::project_builder::ProjectBuilder;
use snapbox::assert_matches;
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::LazyLock;

static CAIRO_LANG_MACRO_PATH: Lazy<String> = Lazy::new(|| {
static CAIRO_LANG_MACRO_PATH: LazyLock<String> = LazyLock::new(|| {
let path = fsx::canonicalize(
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("../plugins/")
Expand Down
1 change: 0 additions & 1 deletion utils/scarb-test-support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ dunce.workspace = true
hyper = "0.14"
indoc.workspace = true
itertools.workspace = true
once_cell.workspace = true
scarb = { path = "../../scarb" }
scarb-build-metadata = { path = "../scarb-build-metadata" }
scarb-ui = { path = "../scarb-ui" }
Expand Down
13 changes: 6 additions & 7 deletions utils/scarb-test-support/src/command.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use assert_fs::prelude::*;
use assert_fs::TempDir;
use serde::de::DeserializeOwned;
use snapbox::cmd::Command as SnapboxCommand;
use std::ffi::OsString;
use std::io::BufRead;
use std::path::{Path, PathBuf};
use std::process::Command as StdCommand;
use std::sync::LazyLock;
use std::{fs, iter};

use assert_fs::prelude::*;
use assert_fs::TempDir;
use once_cell::sync::Lazy;
use serde::de::DeserializeOwned;
use snapbox::cmd::Command as SnapboxCommand;

use crate::cargo::cargo_bin;
use scarb::core::Config;
use scarb_ui::Verbosity;
Expand Down Expand Up @@ -60,7 +59,7 @@ impl Scarb {

pub fn isolate_from_extensions(self) -> Self {
// NOTE: We keep TempDir instance in static, so that it'll be dropped when program ends.
static ISOLATE: Lazy<(PathBuf, TempDir)> = Lazy::new(|| {
static ISOLATE: LazyLock<(PathBuf, TempDir)> = LazyLock::new(|| {
let t = TempDir::new().unwrap();
let source_bin = cargo_bin("scarb");
let output_bin = t.child(source_bin.file_name().unwrap()).to_path_buf();
Expand Down
4 changes: 2 additions & 2 deletions utils/scarb-test-support/src/manifest_edit.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::cell::LazyCell;
use std::ffi::OsStr;
use std::fs;

use assert_fs::fixture::ChildPath;
use assert_fs::prelude::*;
use assert_fs::TempDir;
use once_cell::sync::Lazy;
use snapbox::cmd::Command;

use crate::command::Scarb;
Expand Down Expand Up @@ -35,7 +35,7 @@ impl ManifestEditHarness {
}

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

let input_manifest = self.input_manifest.unwrap();
Expand Down
9 changes: 4 additions & 5 deletions utils/scarb-test-support/src/registry/http.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
use std::fmt;
use std::path::Path;

use assert_fs::fixture::ChildPath;
use assert_fs::prelude::*;
use assert_fs::TempDir;
use once_cell::sync::Lazy;
use serde_json::json;
use std::fmt;
use std::path::Path;
use std::sync::LazyLock;
use tokio::runtime;

use crate::registry::local::LocalRegistry;
use crate::simple_http_server::SimpleHttpServer;

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

0 comments on commit 9d0e0ca

Please sign in to comment.