mirror of
https://github.com/arabianq/pipewire-soundpad.git
synced 2026-04-28 06:21:23 +00:00
⚡ Optimize get_device with iterator chaining (#38)
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>
This commit is contained in:
committed by
GitHub
parent
3c2e943e18
commit
3add499bd7
+11
-13
@@ -208,20 +208,18 @@ pub async fn get_all_devices() -> Result<(Vec<AudioDevice>, Vec<AudioDevice>), B
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_device(device_name: &str) -> Result<AudioDevice, Box<dyn Error>> {
|
pub async fn get_device(device_name: &str) -> Result<AudioDevice, Box<dyn Error>> {
|
||||||
let (mut input_devices, output_devices) = get_all_devices().await?;
|
let (input_devices, output_devices) = get_all_devices().await?;
|
||||||
input_devices.extend(output_devices);
|
|
||||||
|
|
||||||
for device in input_devices {
|
input_devices
|
||||||
if device.name == device_name
|
.into_iter()
|
||||||
|| device.nick == device_name
|
.chain(output_devices)
|
||||||
|| device.name.contains(device_name)
|
.find(|device| {
|
||||||
|| device.nick.contains(device_name)
|
device.name == device_name
|
||||||
{
|
|| device.nick == device_name
|
||||||
return Ok(device);
|
|| device.name.contains(device_name)
|
||||||
}
|
|| device.nick.contains(device_name)
|
||||||
}
|
})
|
||||||
|
.ok_or_else(|| "Device not found".into())
|
||||||
Err("Device not found".into())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_virtual_mic() -> Result<pipewire::channel::Sender<Terminate>, Box<dyn Error>> {
|
pub fn create_virtual_mic() -> Result<pipewire::channel::Sender<Terminate>, Box<dyn Error>> {
|
||||||
|
|||||||
Reference in New Issue
Block a user