From b5703625462a83b649a4744c17e02937f02ea683 Mon Sep 17 00:00:00 2001 From: XiuChengWu <732857315@qq.com> Date: Tue, 31 Mar 2026 23:24:50 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E5=87=8F=E5=B0=91=20WiFi=20=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E4=B8=8D=E5=BF=85=E8=A6=81=E7=9A=84=20String=20?= =?UTF-8?q?=E5=88=86=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - run_nmcli: 改为泛型 &[impl AsRef],直接接受 &[&str] - 移除 nmcli_args() 中间函数,消除每次 nmcli 调用的 Vec 分配 - build_connect_args/build_hotspot_args: 使用 array.into_iter() (Rust 2021) --- src/plugins/wifi/mod.rs | 44 ++++++++++++++++++----------------------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/src/plugins/wifi/mod.rs b/src/plugins/wifi/mod.rs index d569753..b3582f8 100644 --- a/src/plugins/wifi/mod.rs +++ b/src/plugins/wifi/mod.rs @@ -36,12 +36,11 @@ impl WifiPlugin { Self { ctx: None } } - fn nmcli_args(parts: &[&str]) -> Vec { - parts.iter().map(|part| (*part).to_string()).collect() - } - fn build_connect_args(ssid: &str, password: &str) -> Vec { - let mut args = Self::nmcli_args(&["device", "wifi", "connect", ssid]); + 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()); @@ -50,18 +49,13 @@ impl WifiPlugin { } fn build_hotspot_args(ssid: &str, password: &str) -> Vec { - vec![ - "device".to_string(), - "wifi".to_string(), - "hotspot".to_string(), - "ssid".to_string(), - ssid.to_string(), - "password".to_string(), - password.to_string(), - ] + ["device", "wifi", "hotspot", "ssid", ssid, "password", password] + .into_iter() + .map(String::from) + .collect() } - fn run_nmcli(args: &[String]) -> Result { + fn run_nmcli(args: &[impl AsRef + std::fmt::Debug]) -> Result { let output = Command::new("nmcli") .args(args) .output() @@ -145,9 +139,9 @@ impl WifiPlugin { } fn scan_networks(&self) -> Result { - Self::run_nmcli(&Self::nmcli_args(&["device", "wifi", "rescan"]))?; + Self::run_nmcli(&["device", "wifi", "rescan"])?; thread::sleep(Duration::from_secs(2)); - let output = Self::run_nmcli(&Self::nmcli_args(&[ + let output = Self::run_nmcli(&[ "--terse", "--escape", "yes", @@ -156,7 +150,7 @@ impl WifiPlugin { "device", "wifi", "list", - ]))?; + ])?; let networks = output .lines() @@ -206,7 +200,7 @@ impl WifiPlugin { } fn status(&self) -> Result { - let device_output = Self::run_nmcli(&Self::nmcli_args(&[ + let device_output = Self::run_nmcli(&[ "--terse", "--escape", "yes", @@ -214,8 +208,8 @@ impl WifiPlugin { "DEVICE,TYPE,STATE,CONNECTION", "device", "status", - ]))?; - let ip_output = Self::run_nmcli(&Self::nmcli_args(&[ + ])?; + let ip_output = Self::run_nmcli(&[ "--terse", "--escape", "yes", @@ -223,7 +217,7 @@ impl WifiPlugin { "DEVICE,IP4.ADDRESS", "device", "show", - ]))?; + ])?; let mut ip_map: HashMap> = HashMap::new(); for line in ip_output.lines().filter(|line| !line.trim().is_empty()) { @@ -277,7 +271,7 @@ impl WifiPlugin { } fn ap_stop(&self) -> Result { - let active = Self::run_nmcli(&Self::nmcli_args(&[ + let active = Self::run_nmcli(&[ "--terse", "--escape", "yes", @@ -286,14 +280,14 @@ impl WifiPlugin { "connection", "show", "--active", - ]))?; + ])?;; let hotspot_name = active .lines() .map(str::trim) .find(|name| *name == "hotspot") .ok_or_else(|| anyhow!("active hotspot connection 'hotspot' not found"))?; - let output = Self::run_nmcli(&Self::nmcli_args(&["connection", "down", hotspot_name]))?; + let output = Self::run_nmcli(&["connection", "down", hotspot_name])?; Ok(json!({ "ok": true,