From f78e5e4d4ae3f054c1b78e44332eb65715ea81c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E8=89=AF=E4=BB=94?= <32487868+cijiugechu@users.noreply.github.com> Date: Wed, 29 Nov 2023 07:12:03 +0800 Subject: [PATCH] refactor: replace `once_cell` with `std::sync::OnceLock` (#8309) Our MSRV is currently 1.70 and these new types were introduced in 1.70 --- core/tauri/Cargo.toml | 2 -- core/tauri/build.rs | 6 ++---- core/tauri/src/async_runtime.rs | 4 ++-- core/tauri/src/lib.rs | 5 ++--- core/tauri/src/plugin/mobile.rs | 9 ++++----- tooling/cli/Cargo.lock | 1 - tooling/cli/Cargo.toml | 1 - tooling/cli/src/dev.rs | 9 ++++----- tooling/cli/src/helpers/app_paths.rs | 9 +++++---- tooling/cli/src/helpers/config.rs | 7 +++---- 10 files changed, 22 insertions(+), 31 deletions(-) diff --git a/core/tauri/Cargo.toml b/core/tauri/Cargo.toml index 7c7f95499..35409c414 100644 --- a/core/tauri/Cargo.toml +++ b/core/tauri/Cargo.toml @@ -49,7 +49,6 @@ uuid = { version = "1", features = [ "v4" ], optional = true } url = { version = "2.4" } anyhow = "1.0" thiserror = "1.0" -once_cell = "1" tauri-runtime = { version = "1.0.0-alpha.5", path = "../tauri-runtime" } tauri-macros = { version = "2.0.0-alpha.11", path = "../tauri-macros" } tauri-utils = { version = "2.0.0-alpha.11", features = [ "resources" ], path = "../tauri-utils" } @@ -109,7 +108,6 @@ swift-rs = "1.0.6" [build-dependencies] heck = "0.4" -once_cell = "1" tauri-build = { path = "../tauri-build/", version = "2.0.0-alpha.12" } [dev-dependencies] diff --git a/core/tauri/build.rs b/core/tauri/build.rs index 669de4270..01a589c86 100644 --- a/core/tauri/build.rs +++ b/core/tauri/build.rs @@ -4,8 +4,6 @@ use heck::AsShoutySnakeCase; -use once_cell::sync::OnceCell; - use std::env::var_os; use std::fs::read_dir; use std::fs::read_to_string; @@ -13,10 +11,10 @@ use std::fs::write; use std::{ env::var, path::{Path, PathBuf}, - sync::Mutex, + sync::{Mutex, OnceLock}, }; -static CHECKED_FEATURES: OnceCell>> = OnceCell::new(); +static CHECKED_FEATURES: OnceLock>> = OnceLock::new(); // checks if the given Cargo feature is enabled. fn has_feature(feature: &str) -> bool { diff --git a/core/tauri/src/async_runtime.rs b/core/tauri/src/async_runtime.rs index 01c7083be..ebc65954b 100644 --- a/core/tauri/src/async_runtime.rs +++ b/core/tauri/src/async_runtime.rs @@ -10,7 +10,6 @@ //! one you need isn't here, you could use types in [`tokio`] directly. //! For custom command handlers, it's recommended to use a plain `async fn` command. -use once_cell::sync::OnceCell; pub use tokio::{ runtime::{Handle as TokioHandle, Runtime as TokioRuntime}, sync::{ @@ -23,10 +22,11 @@ pub use tokio::{ use std::{ future::Future, pin::Pin, + sync::OnceLock, task::{Context, Poll}, }; -static RUNTIME: OnceCell = OnceCell::new(); +static RUNTIME: OnceLock = OnceLock::new(); struct GlobalRuntime { runtime: Option, diff --git a/core/tauri/src/lib.rs b/core/tauri/src/lib.rs index bce7b7596..59a197ff9 100644 --- a/core/tauri/src/lib.rs +++ b/core/tauri/src/lib.rs @@ -887,10 +887,9 @@ pub mod test; #[cfg(test)] mod tests { use cargo_toml::Manifest; - use once_cell::sync::OnceCell; - use std::{env::var, fs::read_to_string, path::PathBuf}; + use std::{env::var, fs::read_to_string, path::PathBuf, sync::OnceLock}; - static MANIFEST: OnceCell = OnceCell::new(); + static MANIFEST: OnceLock = OnceLock::new(); const CHECKED_FEATURES: &str = include_str!(concat!(env!("OUT_DIR"), "/checked_features")); fn get_manifest() -> &'static Manifest { diff --git a/core/tauri/src/plugin/mobile.rs b/core/tauri/src/plugin/mobile.rs index 15f49aa79..d6271ca99 100644 --- a/core/tauri/src/plugin/mobile.rs +++ b/core/tauri/src/plugin/mobile.rs @@ -14,13 +14,12 @@ use crate::{ #[cfg(mobile)] use std::sync::atomic::{AtomicI32, Ordering}; -use once_cell::sync::OnceCell; use serde::{de::DeserializeOwned, Serialize}; use std::{ collections::HashMap, fmt, - sync::{mpsc::channel, Mutex}, + sync::{mpsc::channel, Mutex, OnceLock}, }; type PluginResponse = Result; @@ -29,9 +28,9 @@ type PendingPluginCallHandler = Box #[cfg(mobile)] static PENDING_PLUGIN_CALLS_ID: AtomicI32 = AtomicI32::new(0); -static PENDING_PLUGIN_CALLS: OnceCell>> = - OnceCell::new(); -static CHANNELS: OnceCell>> = OnceCell::new(); +static PENDING_PLUGIN_CALLS: OnceLock>> = + OnceLock::new(); +static CHANNELS: OnceLock>> = OnceLock::new(); /// Possible errors when invoking a plugin. #[derive(Debug, thiserror::Error)] diff --git a/tooling/cli/Cargo.lock b/tooling/cli/Cargo.lock index cfadd1a50..480d6f57d 100644 --- a/tooling/cli/Cargo.lock +++ b/tooling/cli/Cargo.lock @@ -4092,7 +4092,6 @@ dependencies = [ "minisign", "notify", "notify-debouncer-mini", - "once_cell", "os_info", "os_pipe", "plist", diff --git a/tooling/cli/Cargo.toml b/tooling/cli/Cargo.toml index a1c8ade31..c12eef8e3 100644 --- a/tooling/cli/Cargo.toml +++ b/tooling/cli/Cargo.toml @@ -51,7 +51,6 @@ clap = { version = "4.4", features = [ "derive", "env" ] } anyhow = "1.0" tauri-bundler = { version = "2.0.0-alpha.12", default-features = false, path = "../bundler" } colored = "2.0" -once_cell = "1" serde = { version = "1.0", features = [ "derive" ] } serde_json = "1.0" notify = "6.1" diff --git a/tooling/cli/src/dev.rs b/tooling/cli/src/dev.rs index 4a25a1cc5..ecf95713f 100644 --- a/tooling/cli/src/dev.rs +++ b/tooling/cli/src/dev.rs @@ -16,7 +16,6 @@ use crate::{ use anyhow::{bail, Context}; use clap::{ArgAction, Parser}; use log::{error, info, warn}; -use once_cell::sync::OnceCell; use shared_child::SharedChild; use tauri_utils::platform::Target; @@ -26,12 +25,12 @@ use std::{ process::{exit, Command, Stdio}, sync::{ atomic::{AtomicBool, Ordering}, - Arc, Mutex, + Arc, Mutex, OnceLock, }, }; -static BEFORE_DEV: OnceCell>> = OnceCell::new(); -static KILL_BEFORE_DEV_FLAG: OnceCell = OnceCell::new(); +static BEFORE_DEV: OnceLock>> = OnceLock::new(); +static KILL_BEFORE_DEV_FLAG: OnceLock = OnceLock::new(); #[cfg(unix)] const KILL_CHILDREN_SCRIPT: &[u8] = include_bytes!("../scripts/kill-children.sh"); @@ -107,7 +106,7 @@ fn command_internal(mut options: Options) -> Result<()> { } pub fn local_ip_address(force: bool) -> &'static IpAddr { - static LOCAL_IP: OnceCell = OnceCell::new(); + static LOCAL_IP: OnceLock = OnceLock::new(); LOCAL_IP.get_or_init(|| { let prompt_for_ip = || { let addresses: Vec = local_ip_address::list_afinet_netifas() diff --git a/tooling/cli/src/helpers/app_paths.rs b/tooling/cli/src/helpers/app_paths.rs index c183eee28..1e1465f3e 100644 --- a/tooling/cli/src/helpers/app_paths.rs +++ b/tooling/cli/src/helpers/app_paths.rs @@ -7,10 +7,10 @@ use std::{ env::current_dir, ffi::OsStr, path::{Path, PathBuf}, + sync::OnceLock, }; use ignore::WalkBuilder; -use once_cell::sync::Lazy; use tauri_utils::{ config::parse::{folder_has_configuration_file, is_configuration_file, ConfigFormat}, @@ -114,9 +114,10 @@ fn get_app_dir() -> Option { } pub fn app_dir() -> &'static PathBuf { - static APP_DIR: Lazy = - Lazy::new(|| get_app_dir().unwrap_or_else(|| get_tauri_dir().parent().unwrap().to_path_buf())); - &APP_DIR + static APP_DIR: OnceLock = OnceLock::new(); + APP_DIR.get_or_init(|| { + get_app_dir().unwrap_or_else(|| get_tauri_dir().parent().unwrap().to_path_buf()) + }) } pub fn tauri_dir() -> PathBuf { diff --git a/tooling/cli/src/helpers/config.rs b/tooling/cli/src/helpers/config.rs index 9fe845aca..9dfd70e29 100644 --- a/tooling/cli/src/helpers/config.rs +++ b/tooling/cli/src/helpers/config.rs @@ -5,7 +5,6 @@ use anyhow::Context; use json_patch::merge; use log::error; -use once_cell::sync::Lazy; use serde_json::Value as JsonValue; pub use tauri_utils::{config::*, platform::Target}; @@ -15,7 +14,7 @@ use std::{ env::{current_dir, set_current_dir, set_var, var_os}, ffi::OsStr, process::exit, - sync::{Arc, Mutex}, + sync::{Arc, Mutex, OnceLock}, }; pub const MERGE_CONFIG_EXTENSION_NAME: &str = "--config"; @@ -115,8 +114,8 @@ pub fn nsis_settings(config: NsisConfig) -> tauri_bundler::NsisSettings { } fn config_handle() -> &'static ConfigHandle { - static CONFING_HANDLE: Lazy = Lazy::new(Default::default); - &CONFING_HANDLE + static CONFIG_HANDLE: OnceLock = OnceLock::new(); + CONFIG_HANDLE.get_or_init(Default::default) } /// Gets the static parsed config from `tauri.conf.json`.