From 3add499bd7d54b10ab44838e8c8f8dbe43387bcd Mon Sep 17 00:00:00 2001 From: Tarasov Aleksandr <55220741+arabianq@users.noreply.github.com> Date: Sun, 8 Mar 2026 02:40:25 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Optimize=20`get=5Fdevice`=20with=20?= =?UTF-8?q?iterator=20chaining=20(#38)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaces manual vector extension and linear search with an iterator chain. This avoids an unnecessary allocation and potential reallocation of the `input_devices` vector and allows for short-circuiting if the device is found early. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> --- src/utils/pipewire.rs | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/utils/pipewire.rs b/src/utils/pipewire.rs index 6e47df2..fe0b21c 100644 --- a/src/utils/pipewire.rs +++ b/src/utils/pipewire.rs @@ -208,20 +208,18 @@ pub async fn get_all_devices() -> Result<(Vec, Vec), B } pub async fn get_device(device_name: &str) -> Result> { - let (mut input_devices, output_devices) = get_all_devices().await?; - input_devices.extend(output_devices); + let (input_devices, output_devices) = get_all_devices().await?; - for device in input_devices { - if device.name == device_name - || device.nick == device_name - || device.name.contains(device_name) - || device.nick.contains(device_name) - { - return Ok(device); - } - } - - Err("Device not found".into()) + input_devices + .into_iter() + .chain(output_devices) + .find(|device| { + device.name == device_name + || device.nick == device_name + || device.name.contains(device_name) + || device.nick.contains(device_name) + }) + .ok_or_else(|| "Device not found".into()) } pub fn create_virtual_mic() -> Result, Box> {