From fbe064253c38fc9a451e881b4a56f565e97fcbb1 Mon Sep 17 00:00:00 2001 From: XiuChengWu <732857315@qq.com> Date: Tue, 31 Mar 2026 23:38:57 +0800 Subject: [PATCH] perf: inline WiFi connect/hotspot args to eliminate Vec allocation --- src/plugins/wifi/mod.rs | 64 +++++------------------------------------ 1 file changed, 7 insertions(+), 57 deletions(-) diff --git a/src/plugins/wifi/mod.rs b/src/plugins/wifi/mod.rs index 68550b4..77535b6 100644 --- a/src/plugins/wifi/mod.rs +++ b/src/plugins/wifi/mod.rs @@ -36,25 +36,6 @@ impl WifiPlugin { Self { ctx: None } } - fn build_connect_args(ssid: &str, password: &str) -> Vec { - let mut args: Vec = ["device", "wifi", "connect", ssid] - .into_iter() - .map(String::from) - .collect(); - if !password.trim().is_empty() { - args.push("password".to_string()); - args.push(password.to_string()); - } - args - } - - fn build_hotspot_args(ssid: &str, password: &str) -> Vec { - ["device", "wifi", "hotspot", "ssid", ssid, "password", password] - .into_iter() - .map(String::from) - .collect() - } - fn run_nmcli(args: &[impl AsRef + std::fmt::Debug]) -> Result { let output = Command::new("nmcli") .args(args) @@ -189,7 +170,12 @@ impl WifiPlugin { } fn connect_network(&self, ssid: &str, password: &str) -> Result { - let output = Self::run_nmcli(&Self::build_connect_args(ssid, password))?; + let mut args = vec!["device", "wifi", "connect", ssid]; + if !password.trim().is_empty() { + args.push("password"); + args.push(password); + } + let output = Self::run_nmcli(&args)?; Ok(json!({ "ok": true, @@ -260,7 +246,7 @@ impl WifiPlugin { } fn ap_start(&self, ssid: &str, password: &str) -> Result { - let output = Self::run_nmcli(&Self::build_hotspot_args(ssid, password))?; + let output = Self::run_nmcli(&["device", "wifi", "hotspot", "ssid", ssid, "password", password])?; Ok(json!({ "ok": true, @@ -362,40 +348,4 @@ mod tests { assert_eq!(fields, vec!["wlan0", "wifi", "connected", "Office:LAN"]); } - - #[test] - fn connect_args_preserve_special_characters() { - let args = - WifiPlugin::build_connect_args(r#"ssid \"qa\" demo"#, r#"p@ss\\word with spaces"#); - - assert_eq!( - args, - vec![ - "device", - "wifi", - "connect", - r#"ssid \"qa\" demo"#, - "password", - r#"p@ss\\word with spaces"#, - ] - ); - } - - #[test] - fn hotspot_args_preserve_special_characters() { - let args = WifiPlugin::build_hotspot_args("Showen AP", r#"\\quoted pass\\"#); - - assert_eq!( - args, - vec![ - "device", - "wifi", - "hotspot", - "ssid", - "Showen AP", - "password", - r#"\\quoted pass\\"#, - ] - ); - } }