Skip to content

Commit 611c23c

Browse files
committed
refactor: use private struct to create device id hash
1 parent 94212ad commit 611c23c

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/media_devices/mod.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
//!
55
//! <https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices>
66
7-
use std::hash::Hasher;
8-
97
use rustc_hash::FxHasher;
8+
use std::hash::{Hash, Hasher};
109

1110
use crate::context::{AudioContextLatencyCategory, AudioContextOptions};
1211
use crate::media_streams::MediaStream;
@@ -36,16 +35,32 @@ pub enum MediaDeviceInfoKind {
3635
AudioOutput,
3736
}
3837

38+
#[derive(Hash)]
39+
struct DeviceIdInfo {
40+
kind: u8,
41+
host: String,
42+
device_name: String,
43+
num_channels: u16,
44+
index: u8,
45+
}
46+
3947
pub(crate) fn create_device_id(
4048
kind: u8,
4149
host: String,
4250
device_name: String,
4351
num_channels: u16,
4452
index: u8,
4553
) -> String {
54+
let device_info = DeviceIdInfo {
55+
kind,
56+
host,
57+
device_name,
58+
num_channels,
59+
index,
60+
};
61+
4662
let mut hasher = FxHasher::default();
47-
let id_string = format!("{}{}{}{}{}", kind, host, device_name, num_channels, index);
48-
hasher.write(id_string.as_bytes());
63+
device_info.hash(&mut hasher);
4964
format!("{}", hasher.finish())
5065
}
5166

0 commit comments

Comments
 (0)