Skip to content

Commit 65fab0b

Browse files
committed
perf: borrowed slice adjusted for empty ssid check
1 parent 88f0eaf commit 65fab0b

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

nmrs/src/monitoring/info.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ pub(crate) async fn current_ssid(conn: &Connection) -> Option<String> {
153153
);
154154
let ssid_bytes = try_log!(ap.ssid().await, "Failed to get SSID bytes");
155155
let ssid = decode_ssid_or_empty(&ssid_bytes);
156-
return Some(ssid);
156+
return Some(ssid.to_string());
157157
}
158158
}
159159
}
@@ -199,7 +199,7 @@ pub(crate) async fn current_connection_info(conn: &Connection) -> Option<(String
199199
let ssid_bytes = try_log!(ap.ssid().await, "Failed to get SSID bytes");
200200
let ssid = decode_ssid_or_empty(&ssid_bytes);
201201
let frequency = ap.frequency().await.ok();
202-
return Some((ssid, frequency));
202+
return Some((ssid.to_string(), frequency));
203203
}
204204
}
205205
}

nmrs/src/util/utils.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,18 @@ pub(crate) fn decode_ssid_or_hidden(bytes: &[u8]) -> Cow<'static, str> {
7777
}
7878

7979
/// Decode SSID bytes for comparison purposes, defaulting to empty string if invalid.
80-
pub(crate) fn decode_ssid_or_empty(bytes: &[u8]) -> String {
80+
pub(crate) fn decode_ssid_or_empty(bytes: &[u8]) -> Cow<'static, str> {
8181
if bytes.is_empty() {
82-
return String::new();
82+
return Cow::Borrowed("");
8383
}
84-
str::from_utf8(bytes)
85-
.map(|s| s.to_string())
86-
.unwrap_or_else(|e| {
84+
85+
match str::from_utf8(bytes) {
86+
Ok(s) => Cow::Owned(s.to_owned()),
87+
Err(e) => {
8788
warn!("Invalid UTF-8 in SSID during comparison: {e}");
88-
String::new()
89-
})
89+
Cow::Borrowed("")
90+
}
91+
}
9092
}
9193

9294
/// Safely get signal strength with a default value.

0 commit comments

Comments
 (0)