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:
@@ -36,12 +36,11 @@ impl WifiPlugin {
|
||||
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> {
|
||||
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() {
|
||||
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> {
|
||||
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<String> {
|
||||
fn run_nmcli(args: &[impl AsRef<std::ffi::OsStr> + std::fmt::Debug]) -> Result<String> {
|
||||
let output = Command::new("nmcli")
|
||||
.args(args)
|
||||
.output()
|
||||
@@ -145,9 +139,9 @@ impl WifiPlugin {
|
||||
}
|
||||
|
||||
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));
|
||||
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<serde_json::Value> {
|
||||
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<String, Vec<String>> = 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<serde_json::Value> {
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user