perf: 减少 WiFi 模块不必要的 String 分配

- run_nmcli: 改为泛型 &[impl AsRef<OsStr>],直接接受 &[&str]
- 移除 nmcli_args() 中间函数,消除每次 nmcli 调用的 Vec<String> 分配
- build_connect_args/build_hotspot_args: 使用 array.into_iter() (Rust 2021)
This commit is contained in:
2026-03-31 23:24:50 +08:00
parent 1bf9e055e0
commit b570362546

View File

@@ -36,12 +36,11 @@ impl WifiPlugin {
Self { ctx: None } Self { ctx: None }
} }
fn nmcli_args(parts: &[&str]) -> Vec<String> {
parts.iter().map(|part| (*part).to_string()).collect()
}
fn build_connect_args(ssid: &str, password: &str) -> Vec<String> { fn build_connect_args(ssid: &str, password: &str) -> Vec<String> {
let mut args = Self::nmcli_args(&["device", "wifi", "connect", ssid]); let mut args: Vec<String> = ["device", "wifi", "connect", ssid]
.into_iter()
.map(String::from)
.collect();
if !password.trim().is_empty() { if !password.trim().is_empty() {
args.push("password".to_string()); args.push("password".to_string());
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<String> { fn build_hotspot_args(ssid: &str, password: &str) -> Vec<String> {
vec![ ["device", "wifi", "hotspot", "ssid", ssid, "password", password]
"device".to_string(), .into_iter()
"wifi".to_string(), .map(String::from)
"hotspot".to_string(), .collect()
"ssid".to_string(),
ssid.to_string(),
"password".to_string(),
password.to_string(),
]
} }
fn run_nmcli(args: &[String]) -> Result<String> { fn run_nmcli(args: &[impl AsRef<std::ffi::OsStr> + std::fmt::Debug]) -> Result<String> {
let output = Command::new("nmcli") let output = Command::new("nmcli")
.args(args) .args(args)
.output() .output()
@@ -145,9 +139,9 @@ impl WifiPlugin {
} }
fn scan_networks(&self) -> Result<serde_json::Value> { fn scan_networks(&self) -> Result<serde_json::Value> {
Self::run_nmcli(&Self::nmcli_args(&["device", "wifi", "rescan"]))?; Self::run_nmcli(&["device", "wifi", "rescan"])?;
thread::sleep(Duration::from_secs(2)); thread::sleep(Duration::from_secs(2));
let output = Self::run_nmcli(&Self::nmcli_args(&[ let output = Self::run_nmcli(&[
"--terse", "--terse",
"--escape", "--escape",
"yes", "yes",
@@ -156,7 +150,7 @@ impl WifiPlugin {
"device", "device",
"wifi", "wifi",
"list", "list",
]))?; ])?;
let networks = output let networks = output
.lines() .lines()
@@ -206,7 +200,7 @@ impl WifiPlugin {
} }
fn status(&self) -> Result<serde_json::Value> { fn status(&self) -> Result<serde_json::Value> {
let device_output = Self::run_nmcli(&Self::nmcli_args(&[ let device_output = Self::run_nmcli(&[
"--terse", "--terse",
"--escape", "--escape",
"yes", "yes",
@@ -214,8 +208,8 @@ impl WifiPlugin {
"DEVICE,TYPE,STATE,CONNECTION", "DEVICE,TYPE,STATE,CONNECTION",
"device", "device",
"status", "status",
]))?; ])?;
let ip_output = Self::run_nmcli(&Self::nmcli_args(&[ let ip_output = Self::run_nmcli(&[
"--terse", "--terse",
"--escape", "--escape",
"yes", "yes",
@@ -223,7 +217,7 @@ impl WifiPlugin {
"DEVICE,IP4.ADDRESS", "DEVICE,IP4.ADDRESS",
"device", "device",
"show", "show",
]))?; ])?;
let mut ip_map: HashMap<String, Vec<String>> = HashMap::new(); let mut ip_map: HashMap<String, Vec<String>> = HashMap::new();
for line in ip_output.lines().filter(|line| !line.trim().is_empty()) { for line in ip_output.lines().filter(|line| !line.trim().is_empty()) {
@@ -277,7 +271,7 @@ impl WifiPlugin {
} }
fn ap_stop(&self) -> Result<serde_json::Value> { fn ap_stop(&self) -> Result<serde_json::Value> {
let active = Self::run_nmcli(&Self::nmcli_args(&[ let active = Self::run_nmcli(&[
"--terse", "--terse",
"--escape", "--escape",
"yes", "yes",
@@ -286,14 +280,14 @@ impl WifiPlugin {
"connection", "connection",
"show", "show",
"--active", "--active",
]))?; ])?;;
let hotspot_name = active let hotspot_name = active
.lines() .lines()
.map(str::trim) .map(str::trim)
.find(|name| *name == "hotspot") .find(|name| *name == "hotspot")
.ok_or_else(|| anyhow!("active hotspot connection 'hotspot' not found"))?; .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(json!({
"ok": true, "ok": true,