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" }
anyhow = "1.0"
win32job = "2"
lazy_static = "1.4"
scopeguard = "1.2"
notify-rust = "4"
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`.
//
pub mod http_api;
pub mod rs_process;
pub mod win32_api;

View File

@ -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<HashMap<u32, RsProcess>> = Mutex::new(HashMap::new());
}
static RS_PROCESS_MAP: Lazy<Mutex<HashMap<u32, RsProcess>>> = 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<R>(
};
stream_sink.add(message).unwrap();
}
}
}

View File

@ -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<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();
if let Some(summary) = summary {
n.summary(&summary);

View File

@ -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<HashMap<String, IpAddr>> = RwLock::from(HashMap::new());
}
pub static MY_HOSTS_MAP: Lazy<RwLock<HashMap<String, IpAddr>>> =
Lazy::new(|| RwLock::from(HashMap::new()));
/// Wrapper around an `AsyncResolver`, which implements the `Resolve` trait.
#[derive(Debug, Default, Clone)]

View File

@ -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<HeaderMap> = RwLock::from(HeaderMap::new());
static ref DNS_CLIENT: Arc<dns::MyHickoryDnsResolver> =
Arc::from(dns::MyHickoryDnsResolver::default());
static ref HTTP_CLIENT: reqwest::Client = new_http_client(true);
}
static DEFAULT_HEADER: Lazy<RwLock<HeaderMap>> = Lazy::new(|| RwLock::from(HeaderMap::new()));
static DNS_CLIENT: Lazy<Arc<dns::MyHickoryDnsResolver>> =
Lazy::new(|| Arc::from(dns::MyHickoryDnsResolver::default()));
static HTTP_CLIENT: Lazy<reqwest::Client> = Lazy::new(|| new_http_client(true));
fn new_http_client(keep_alive: bool) -> reqwest::Client {
let mut c = reqwest::Client::builder()