ソースを参照

refactor: replace `once_cell` with `std::sync::OnceLock` (#8309)

Our MSRV is currently 1.70 and these new types were introduced in 1.70
阿良仔 1 年間 前
コミット
f78e5e4d4a

+ 0 - 2
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]

+ 2 - 4
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<Mutex<Vec<String>>> = OnceCell::new();
+static CHECKED_FEATURES: OnceLock<Mutex<Vec<String>>> = OnceLock::new();
 
 // checks if the given Cargo feature is enabled.
 fn has_feature(feature: &str) -> bool {

+ 2 - 2
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<GlobalRuntime> = OnceCell::new();
+static RUNTIME: OnceLock<GlobalRuntime> = OnceLock::new();
 
 struct GlobalRuntime {
   runtime: Option<Runtime>,

+ 2 - 3
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<Manifest> = OnceCell::new();
+  static MANIFEST: OnceLock<Manifest> = OnceLock::new();
   const CHECKED_FEATURES: &str = include_str!(concat!(env!("OUT_DIR"), "/checked_features"));
 
   fn get_manifest() -> &'static Manifest {

+ 4 - 5
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<serde_json::Value, serde_json::Value>;
@@ -29,9 +28,9 @@ type PendingPluginCallHandler = Box<dyn FnOnce(PluginResponse) + Send + 'static>
 
 #[cfg(mobile)]
 static PENDING_PLUGIN_CALLS_ID: AtomicI32 = AtomicI32::new(0);
-static PENDING_PLUGIN_CALLS: OnceCell<Mutex<HashMap<i32, PendingPluginCallHandler>>> =
-  OnceCell::new();
-static CHANNELS: OnceCell<Mutex<HashMap<u32, Channel>>> = OnceCell::new();
+static PENDING_PLUGIN_CALLS: OnceLock<Mutex<HashMap<i32, PendingPluginCallHandler>>> =
+  OnceLock::new();
+static CHANNELS: OnceLock<Mutex<HashMap<u32, Channel>>> = OnceLock::new();
 
 /// Possible errors when invoking a plugin.
 #[derive(Debug, thiserror::Error)]

+ 0 - 1
tooling/cli/Cargo.lock

@@ -4092,7 +4092,6 @@ dependencies = [
  "minisign",
  "notify",
  "notify-debouncer-mini",
- "once_cell",
  "os_info",
  "os_pipe",
  "plist",

+ 0 - 1
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"

+ 4 - 5
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<Mutex<Arc<SharedChild>>> = OnceCell::new();
-static KILL_BEFORE_DEV_FLAG: OnceCell<AtomicBool> = OnceCell::new();
+static BEFORE_DEV: OnceLock<Mutex<Arc<SharedChild>>> = OnceLock::new();
+static KILL_BEFORE_DEV_FLAG: OnceLock<AtomicBool> = 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<IpAddr> = OnceCell::new();
+  static LOCAL_IP: OnceLock<IpAddr> = OnceLock::new();
   LOCAL_IP.get_or_init(|| {
     let prompt_for_ip = || {
       let addresses: Vec<IpAddr> = local_ip_address::list_afinet_netifas()

+ 5 - 4
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<PathBuf> {
 }
 
 pub fn app_dir() -> &'static PathBuf {
-  static APP_DIR: Lazy<PathBuf> =
-    Lazy::new(|| get_app_dir().unwrap_or_else(|| get_tauri_dir().parent().unwrap().to_path_buf()));
-  &APP_DIR
+  static APP_DIR: OnceLock<PathBuf> = 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 {

+ 3 - 4
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<ConfigHandle> = Lazy::new(Default::default);
-  &CONFING_HANDLE
+  static CONFIG_HANDLE: OnceLock<ConfigHandle> = OnceLock::new();
+  CONFIG_HANDLE.get_or_init(Default::default)
 }
 
 /// Gets the static parsed config from `tauri.conf.json`.