feat: rust/ deprecate lazy_static , use once_cell

This commit is contained in:
xkeyC 2024-04-30 20:03:20 +08:00
parent 3dbf993099
commit 5d0c3f5fd4
6 changed files with 35 additions and 24 deletions

View File

@ -20,7 +20,6 @@ reqwest = { version = "0.12", features = ["rustls-tls-webpki-roots", "cookies",
hickory-resolver = { version = "0.24" } hickory-resolver = { version = "0.24" }
anyhow = "1.0" anyhow = "1.0"
win32job = "2" win32job = "2"
lazy_static = "1.4"
scopeguard = "1.2" scopeguard = "1.2"
notify-rust = "4" notify-rust = "4"
windows = { version = "0.56.0", features = ["Win32_UI_WindowsAndMessaging"] } windows = { version = "0.56.0", features = ["Win32_UI_WindowsAndMessaging"] }

View File

@ -2,6 +2,5 @@
// Do not put code in `mod.rs`, but put in e.g. `simple.rs`. // Do not put code in `mod.rs`, but put in e.g. `simple.rs`.
// //
pub mod http_api; pub mod http_api;
pub mod rs_process; pub mod rs_process;
pub mod win32_api; pub mod win32_api;

View File

@ -2,7 +2,7 @@ use std::collections::HashMap;
use std::sync::Arc; use std::sync::Arc;
use async_std::task::block_on; use async_std::task::block_on;
use lazy_static::lazy_static; use once_cell::sync::Lazy;
use scopeguard::defer; use scopeguard::defer;
use tokio::io::AsyncBufReadExt; use tokio::io::AsyncBufReadExt;
use tokio::io::AsyncWriteExt; use tokio::io::AsyncWriteExt;
@ -30,9 +30,7 @@ pub struct RsProcess {
pub rs_pid: u32, pub rs_pid: u32,
} }
lazy_static! { static RS_PROCESS_MAP: Lazy<Mutex<HashMap<u32, RsProcess>>> = Lazy::new(|| Mutex::new(HashMap::new()));
static ref RS_PROCESS_MAP: Mutex<HashMap<u32, RsProcess>> = Mutex::new(HashMap::new());
}
pub async fn start( pub async fn start(
executable: &str, 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 pid = child.id().expect("[rs_process] Failed to get pid");
{ {
let mut map = RS_PROCESS_MAP.lock().await; let mut map = RS_PROCESS_MAP.lock().await;
@ -88,8 +89,14 @@ pub async fn start(
println!("RS_PROCESS_MAP ..defer ..len() = {}", map.len()); println!("RS_PROCESS_MAP ..defer ..len() = {}", map.len());
} }
let stdout = child.stdout.take().expect("[rs_process] Failed to open stdout"); let stdout = child
let stderr = child.stderr.take().expect("[rs_process] Failed to open stderr"); .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( let output_task = tokio::spawn(_process_output(
stdout, stdout,
@ -112,7 +119,10 @@ pub async fn start(
.expect("[rs_process] Failed to wait for child process"); .expect("[rs_process] Failed to wait for child process");
if !exit_status.success() { 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 { let message = RsProcessStreamData {
data_type: RsProcessStreamDataType::Exit, data_type: RsProcessStreamDataType::Exit,
data: "exit".to_string(), data: "exit".to_string(),
@ -160,4 +170,4 @@ async fn _process_output<R>(
}; };
stream_sink.add(message).unwrap(); stream_sink.add(message).unwrap();
} }
} }

View File

@ -3,7 +3,12 @@ use windows::core::{HSTRING, PCWSTR};
use windows::Win32::Foundation::HWND; use windows::Win32::Foundation::HWND;
use windows::Win32::UI::WindowsAndMessaging; use windows::Win32::UI::WindowsAndMessaging;
pub fn send_notify(summary: Option<String>, body: Option<String>, app_name: Option<String>, app_id: Option<String>) -> anyhow::Result<()> { pub fn send_notify(
summary: Option<String>,
body: Option<String>,
app_name: Option<String>,
app_id: Option<String>,
) -> anyhow::Result<()> {
let mut n = Notification::new(); let mut n = Notification::new();
if let Some(summary) = summary { if let Some(summary) = summary {
n.summary(&summary); n.summary(&summary);

View File

@ -1,16 +1,14 @@
use hickory_resolver::config::{NameServerConfigGroup, ResolverConfig, ResolverOpts}; use hickory_resolver::config::{NameServerConfigGroup, ResolverConfig, ResolverOpts};
use hickory_resolver::{lookup_ip::LookupIpIntoIter, TokioAsyncResolver}; use hickory_resolver::{lookup_ip::LookupIpIntoIter, TokioAsyncResolver};
use lazy_static::lazy_static; use once_cell::sync::{Lazy, OnceCell};
use once_cell::sync::OnceCell;
use reqwest::dns::{Addrs, Name, Resolve, Resolving}; use reqwest::dns::{Addrs, Name, Resolve, Resolving};
use std::collections::HashMap; use std::collections::HashMap;
use std::io; use std::io;
use std::net::{IpAddr, Ipv4Addr, SocketAddr}; use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
lazy_static! { pub static MY_HOSTS_MAP: Lazy<RwLock<HashMap<String, IpAddr>>> =
pub static ref MY_HOSTS_MAP: RwLock<HashMap<String, IpAddr>> = RwLock::from(HashMap::new()); Lazy::new(|| RwLock::from(HashMap::new()));
}
/// Wrapper around an `AsyncResolver`, which implements the `Resolve` trait. /// Wrapper around an `AsyncResolver`, which implements the `Resolve` trait.
#[derive(Debug, Default, Clone)] #[derive(Debug, Default, Clone)]

View File

@ -1,6 +1,6 @@
pub mod dns; pub mod dns;
use lazy_static::lazy_static; use once_cell::sync::Lazy;
use reqwest::header::{HeaderMap, HeaderName, HeaderValue}; use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
use reqwest::{Method, RequestBuilder}; use reqwest::{Method, RequestBuilder};
use scopeguard::defer; use scopeguard::defer;
@ -43,12 +43,12 @@ fn _hyper_version_to_my_version(v: reqwest::Version) -> MyHttpVersion {
} }
} }
lazy_static! { static DEFAULT_HEADER: Lazy<RwLock<HeaderMap>> = Lazy::new(|| RwLock::from(HeaderMap::new()));
static ref DEFAULT_HEADER: RwLock<HeaderMap> = RwLock::from(HeaderMap::new());
static ref DNS_CLIENT: Arc<dns::MyHickoryDnsResolver> = static DNS_CLIENT: Lazy<Arc<dns::MyHickoryDnsResolver>> =
Arc::from(dns::MyHickoryDnsResolver::default()); Lazy::new(|| Arc::from(dns::MyHickoryDnsResolver::default()));
static ref HTTP_CLIENT: reqwest::Client = new_http_client(true);
} static HTTP_CLIENT: Lazy<reqwest::Client> = Lazy::new(|| new_http_client(true));
fn new_http_client(keep_alive: bool) -> reqwest::Client { fn new_http_client(keep_alive: bool) -> reqwest::Client {
let mut c = reqwest::Client::builder() let mut c = reqwest::Client::builder()