From 5d0c3f5fd43782682408e2b9f4fe045a37f6f31e Mon Sep 17 00:00:00 2001 From: xkeyC <3334969096@qq.com> Date: Tue, 30 Apr 2024 20:03:20 +0800 Subject: [PATCH] feat: rust/ deprecate lazy_static , use once_cell --- rust/Cargo.toml | 1 - rust/src/api/mod.rs | 1 - rust/src/api/rs_process.rs | 28 +++++++++++++++++++--------- rust/src/api/win32_api.rs | 7 ++++++- rust/src/http_package/dns.rs | 8 +++----- rust/src/http_package/mod.rs | 14 +++++++------- 6 files changed, 35 insertions(+), 24 deletions(-) diff --git a/rust/Cargo.toml b/rust/Cargo.toml index c0f20ba..96a9a19 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -20,7 +20,6 @@ reqwest = { version = "0.12", features = ["rustls-tls-webpki-roots", "cookies", hickory-resolver = { version = "0.24" } anyhow = "1.0" win32job = "2" -lazy_static = "1.4" scopeguard = "1.2" notify-rust = "4" windows = { version = "0.56.0", features = ["Win32_UI_WindowsAndMessaging"] } \ No newline at end of file diff --git a/rust/src/api/mod.rs b/rust/src/api/mod.rs index 4db21a5..398d038 100644 --- a/rust/src/api/mod.rs +++ b/rust/src/api/mod.rs @@ -2,6 +2,5 @@ // Do not put code in `mod.rs`, but put in e.g. `simple.rs`. // pub mod http_api; - pub mod rs_process; pub mod win32_api; diff --git a/rust/src/api/rs_process.rs b/rust/src/api/rs_process.rs index 5aacabf..7adf491 100644 --- a/rust/src/api/rs_process.rs +++ b/rust/src/api/rs_process.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use std::sync::Arc; use async_std::task::block_on; -use lazy_static::lazy_static; +use once_cell::sync::Lazy; use scopeguard::defer; use tokio::io::AsyncBufReadExt; use tokio::io::AsyncWriteExt; @@ -30,9 +30,7 @@ pub struct RsProcess { pub rs_pid: u32, } -lazy_static! { - static ref RS_PROCESS_MAP: Mutex> = Mutex::new(HashMap::new()); -} +static RS_PROCESS_MAP: Lazy>> = Lazy::new(|| Mutex::new(HashMap::new())); pub async fn start( executable: &str, @@ -69,7 +67,10 @@ pub async fn start( } } - let stdin = child.stdin.take().expect("[rs_process] Failed to open stdin"); + let stdin = child + .stdin + .take() + .expect("[rs_process] Failed to open stdin"); let pid = child.id().expect("[rs_process] Failed to get pid"); { let mut map = RS_PROCESS_MAP.lock().await; @@ -88,8 +89,14 @@ pub async fn start( println!("RS_PROCESS_MAP ..defer ..len() = {}", map.len()); } - let stdout = child.stdout.take().expect("[rs_process] Failed to open stdout"); - let stderr = child.stderr.take().expect("[rs_process] Failed to open stderr"); + let stdout = child + .stdout + .take() + .expect("[rs_process] Failed to open stdout"); + let stderr = child + .stderr + .take() + .expect("[rs_process] Failed to open stderr"); let output_task = tokio::spawn(_process_output( stdout, @@ -112,7 +119,10 @@ pub async fn start( .expect("[rs_process] Failed to wait for child process"); if !exit_status.success() { - println!("[rs_process] Child process exited with an error: {:?}", exit_status); + println!( + "[rs_process] Child process exited with an error: {:?}", + exit_status + ); let message = RsProcessStreamData { data_type: RsProcessStreamDataType::Exit, data: "exit".to_string(), @@ -160,4 +170,4 @@ async fn _process_output( }; stream_sink.add(message).unwrap(); } -} \ No newline at end of file +} diff --git a/rust/src/api/win32_api.rs b/rust/src/api/win32_api.rs index 7d3b940..7d4f1bf 100644 --- a/rust/src/api/win32_api.rs +++ b/rust/src/api/win32_api.rs @@ -3,7 +3,12 @@ use windows::core::{HSTRING, PCWSTR}; use windows::Win32::Foundation::HWND; use windows::Win32::UI::WindowsAndMessaging; -pub fn send_notify(summary: Option, body: Option, app_name: Option, app_id: Option) -> anyhow::Result<()> { +pub fn send_notify( + summary: Option, + body: Option, + app_name: Option, + app_id: Option, +) -> anyhow::Result<()> { let mut n = Notification::new(); if let Some(summary) = summary { n.summary(&summary); diff --git a/rust/src/http_package/dns.rs b/rust/src/http_package/dns.rs index 2ff8fcf..adea52e 100644 --- a/rust/src/http_package/dns.rs +++ b/rust/src/http_package/dns.rs @@ -1,16 +1,14 @@ use hickory_resolver::config::{NameServerConfigGroup, ResolverConfig, ResolverOpts}; use hickory_resolver::{lookup_ip::LookupIpIntoIter, TokioAsyncResolver}; -use lazy_static::lazy_static; -use once_cell::sync::OnceCell; +use once_cell::sync::{Lazy, OnceCell}; use reqwest::dns::{Addrs, Name, Resolve, Resolving}; use std::collections::HashMap; use std::io; use std::net::{IpAddr, Ipv4Addr, SocketAddr}; use std::sync::{Arc, RwLock}; -lazy_static! { - pub static ref MY_HOSTS_MAP: RwLock> = RwLock::from(HashMap::new()); -} +pub static MY_HOSTS_MAP: Lazy>> = + Lazy::new(|| RwLock::from(HashMap::new())); /// Wrapper around an `AsyncResolver`, which implements the `Resolve` trait. #[derive(Debug, Default, Clone)] diff --git a/rust/src/http_package/mod.rs b/rust/src/http_package/mod.rs index f6ccb8a..aff9feb 100644 --- a/rust/src/http_package/mod.rs +++ b/rust/src/http_package/mod.rs @@ -1,6 +1,6 @@ pub mod dns; -use lazy_static::lazy_static; +use once_cell::sync::Lazy; use reqwest::header::{HeaderMap, HeaderName, HeaderValue}; use reqwest::{Method, RequestBuilder}; use scopeguard::defer; @@ -43,12 +43,12 @@ fn _hyper_version_to_my_version(v: reqwest::Version) -> MyHttpVersion { } } -lazy_static! { - static ref DEFAULT_HEADER: RwLock = RwLock::from(HeaderMap::new()); - static ref DNS_CLIENT: Arc = - Arc::from(dns::MyHickoryDnsResolver::default()); - static ref HTTP_CLIENT: reqwest::Client = new_http_client(true); -} +static DEFAULT_HEADER: Lazy> = Lazy::new(|| RwLock::from(HeaderMap::new())); + +static DNS_CLIENT: Lazy> = + Lazy::new(|| Arc::from(dns::MyHickoryDnsResolver::default())); + +static HTTP_CLIENT: Lazy = Lazy::new(|| new_http_client(true)); fn new_http_client(keep_alive: bool) -> reqwest::Client { let mut c = reqwest::Client::builder()