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

Our MSRV is currently 1.70 and these new types were introduced in 1.70
This commit is contained in:
阿良仔 2023-11-29 07:12:03 +08:00 committed by GitHub
parent 5ff6a74430
commit f78e5e4d4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 22 additions and 31 deletions

View File

@ -49,7 +49,6 @@ uuid = { version = "1", features = [ "v4" ], optional = true }
url = { version = "2.4" } url = { version = "2.4" }
anyhow = "1.0" anyhow = "1.0"
thiserror = "1.0" thiserror = "1.0"
once_cell = "1"
tauri-runtime = { version = "1.0.0-alpha.5", path = "../tauri-runtime" } tauri-runtime = { version = "1.0.0-alpha.5", path = "../tauri-runtime" }
tauri-macros = { version = "2.0.0-alpha.11", path = "../tauri-macros" } tauri-macros = { version = "2.0.0-alpha.11", path = "../tauri-macros" }
tauri-utils = { version = "2.0.0-alpha.11", features = [ "resources" ], path = "../tauri-utils" } tauri-utils = { version = "2.0.0-alpha.11", features = [ "resources" ], path = "../tauri-utils" }
@ -109,7 +108,6 @@ swift-rs = "1.0.6"
[build-dependencies] [build-dependencies]
heck = "0.4" heck = "0.4"
once_cell = "1"
tauri-build = { path = "../tauri-build/", version = "2.0.0-alpha.12" } tauri-build = { path = "../tauri-build/", version = "2.0.0-alpha.12" }
[dev-dependencies] [dev-dependencies]

View File

@ -4,8 +4,6 @@
use heck::AsShoutySnakeCase; use heck::AsShoutySnakeCase;
use once_cell::sync::OnceCell;
use std::env::var_os; use std::env::var_os;
use std::fs::read_dir; use std::fs::read_dir;
use std::fs::read_to_string; use std::fs::read_to_string;
@ -13,10 +11,10 @@ use std::fs::write;
use std::{ use std::{
env::var, env::var,
path::{Path, PathBuf}, 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. // checks if the given Cargo feature is enabled.
fn has_feature(feature: &str) -> bool { fn has_feature(feature: &str) -> bool {

View File

@ -10,7 +10,6 @@
//! one you need isn't here, you could use types in [`tokio`] directly. //! 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. //! For custom command handlers, it's recommended to use a plain `async fn` command.
use once_cell::sync::OnceCell;
pub use tokio::{ pub use tokio::{
runtime::{Handle as TokioHandle, Runtime as TokioRuntime}, runtime::{Handle as TokioHandle, Runtime as TokioRuntime},
sync::{ sync::{
@ -23,10 +22,11 @@ pub use tokio::{
use std::{ use std::{
future::Future, future::Future,
pin::Pin, pin::Pin,
sync::OnceLock,
task::{Context, Poll}, task::{Context, Poll},
}; };
static RUNTIME: OnceCell<GlobalRuntime> = OnceCell::new(); static RUNTIME: OnceLock<GlobalRuntime> = OnceLock::new();
struct GlobalRuntime { struct GlobalRuntime {
runtime: Option<Runtime>, runtime: Option<Runtime>,

View File

@ -887,10 +887,9 @@ pub mod test;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use cargo_toml::Manifest; use cargo_toml::Manifest;
use once_cell::sync::OnceCell; use std::{env::var, fs::read_to_string, path::PathBuf, sync::OnceLock};
use std::{env::var, fs::read_to_string, path::PathBuf};
static MANIFEST: OnceCell<Manifest> = OnceCell::new(); static MANIFEST: OnceLock<Manifest> = OnceLock::new();
const CHECKED_FEATURES: &str = include_str!(concat!(env!("OUT_DIR"), "/checked_features")); const CHECKED_FEATURES: &str = include_str!(concat!(env!("OUT_DIR"), "/checked_features"));
fn get_manifest() -> &'static Manifest { fn get_manifest() -> &'static Manifest {

View File

@ -14,13 +14,12 @@ use crate::{
#[cfg(mobile)] #[cfg(mobile)]
use std::sync::atomic::{AtomicI32, Ordering}; use std::sync::atomic::{AtomicI32, Ordering};
use once_cell::sync::OnceCell;
use serde::{de::DeserializeOwned, Serialize}; use serde::{de::DeserializeOwned, Serialize};
use std::{ use std::{
collections::HashMap, collections::HashMap,
fmt, fmt,
sync::{mpsc::channel, Mutex}, sync::{mpsc::channel, Mutex, OnceLock},
}; };
type PluginResponse = Result<serde_json::Value, serde_json::Value>; type PluginResponse = Result<serde_json::Value, serde_json::Value>;
@ -29,9 +28,9 @@ type PendingPluginCallHandler = Box<dyn FnOnce(PluginResponse) + Send + 'static>
#[cfg(mobile)] #[cfg(mobile)]
static PENDING_PLUGIN_CALLS_ID: AtomicI32 = AtomicI32::new(0); static PENDING_PLUGIN_CALLS_ID: AtomicI32 = AtomicI32::new(0);
static PENDING_PLUGIN_CALLS: OnceCell<Mutex<HashMap<i32, PendingPluginCallHandler>>> = static PENDING_PLUGIN_CALLS: OnceLock<Mutex<HashMap<i32, PendingPluginCallHandler>>> =
OnceCell::new(); OnceLock::new();
static CHANNELS: OnceCell<Mutex<HashMap<u32, Channel>>> = OnceCell::new(); static CHANNELS: OnceLock<Mutex<HashMap<u32, Channel>>> = OnceLock::new();
/// Possible errors when invoking a plugin. /// Possible errors when invoking a plugin.
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error)]

View File

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

View File

@ -51,7 +51,6 @@ clap = { version = "4.4", features = [ "derive", "env" ] }
anyhow = "1.0" anyhow = "1.0"
tauri-bundler = { version = "2.0.0-alpha.12", default-features = false, path = "../bundler" } tauri-bundler = { version = "2.0.0-alpha.12", default-features = false, path = "../bundler" }
colored = "2.0" colored = "2.0"
once_cell = "1"
serde = { version = "1.0", features = [ "derive" ] } serde = { version = "1.0", features = [ "derive" ] }
serde_json = "1.0" serde_json = "1.0"
notify = "6.1" notify = "6.1"

View File

@ -16,7 +16,6 @@ use crate::{
use anyhow::{bail, Context}; use anyhow::{bail, Context};
use clap::{ArgAction, Parser}; use clap::{ArgAction, Parser};
use log::{error, info, warn}; use log::{error, info, warn};
use once_cell::sync::OnceCell;
use shared_child::SharedChild; use shared_child::SharedChild;
use tauri_utils::platform::Target; use tauri_utils::platform::Target;
@ -26,12 +25,12 @@ use std::{
process::{exit, Command, Stdio}, process::{exit, Command, Stdio},
sync::{ sync::{
atomic::{AtomicBool, Ordering}, atomic::{AtomicBool, Ordering},
Arc, Mutex, Arc, Mutex, OnceLock,
}, },
}; };
static BEFORE_DEV: OnceCell<Mutex<Arc<SharedChild>>> = OnceCell::new(); static BEFORE_DEV: OnceLock<Mutex<Arc<SharedChild>>> = OnceLock::new();
static KILL_BEFORE_DEV_FLAG: OnceCell<AtomicBool> = OnceCell::new(); static KILL_BEFORE_DEV_FLAG: OnceLock<AtomicBool> = OnceLock::new();
#[cfg(unix)] #[cfg(unix)]
const KILL_CHILDREN_SCRIPT: &[u8] = include_bytes!("../scripts/kill-children.sh"); 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 { 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(|| { LOCAL_IP.get_or_init(|| {
let prompt_for_ip = || { let prompt_for_ip = || {
let addresses: Vec<IpAddr> = local_ip_address::list_afinet_netifas() let addresses: Vec<IpAddr> = local_ip_address::list_afinet_netifas()

View File

@ -7,10 +7,10 @@ use std::{
env::current_dir, env::current_dir,
ffi::OsStr, ffi::OsStr,
path::{Path, PathBuf}, path::{Path, PathBuf},
sync::OnceLock,
}; };
use ignore::WalkBuilder; use ignore::WalkBuilder;
use once_cell::sync::Lazy;
use tauri_utils::{ use tauri_utils::{
config::parse::{folder_has_configuration_file, is_configuration_file, ConfigFormat}, 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 { pub fn app_dir() -> &'static PathBuf {
static APP_DIR: Lazy<PathBuf> = static APP_DIR: OnceLock<PathBuf> = OnceLock::new();
Lazy::new(|| get_app_dir().unwrap_or_else(|| get_tauri_dir().parent().unwrap().to_path_buf())); APP_DIR.get_or_init(|| {
&APP_DIR get_app_dir().unwrap_or_else(|| get_tauri_dir().parent().unwrap().to_path_buf())
})
} }
pub fn tauri_dir() -> PathBuf { pub fn tauri_dir() -> PathBuf {

View File

@ -5,7 +5,6 @@
use anyhow::Context; use anyhow::Context;
use json_patch::merge; use json_patch::merge;
use log::error; use log::error;
use once_cell::sync::Lazy;
use serde_json::Value as JsonValue; use serde_json::Value as JsonValue;
pub use tauri_utils::{config::*, platform::Target}; pub use tauri_utils::{config::*, platform::Target};
@ -15,7 +14,7 @@ use std::{
env::{current_dir, set_current_dir, set_var, var_os}, env::{current_dir, set_current_dir, set_var, var_os},
ffi::OsStr, ffi::OsStr,
process::exit, process::exit,
sync::{Arc, Mutex}, sync::{Arc, Mutex, OnceLock},
}; };
pub const MERGE_CONFIG_EXTENSION_NAME: &str = "--config"; 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 { fn config_handle() -> &'static ConfigHandle {
static CONFING_HANDLE: Lazy<ConfigHandle> = Lazy::new(Default::default); static CONFIG_HANDLE: OnceLock<ConfigHandle> = OnceLock::new();
&CONFING_HANDLE CONFIG_HANDLE.get_or_init(Default::default)
} }
/// Gets the static parsed config from `tauri.conf.json`. /// Gets the static parsed config from `tauri.conf.json`.