Remove once_cell dependency (#800)

* Remove once_cell dependency.

* Add rust-version and set it to 1.70
This commit is contained in:
James Liu 2023-12-16 17:05:24 -08:00 • committed by GitHub
parent 76b04065cf
commit 310160fbbf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 19 deletions

View file

@ -5,7 +5,6 @@ use crate::{
SupportedBufferSize, SupportedStreamConfig, SupportedStreamConfigRange,
SupportedStreamConfigsError, COMMON_SAMPLE_RATES,
};
use once_cell::sync::Lazy;
use std::ffi::OsString;
use std::fmt;
use std::mem;
@ -13,6 +12,7 @@ use std::ops::{Deref, DerefMut};
use std::os::windows::ffi::OsStringExt;
use std::ptr;
use std::slice;
use std::sync::OnceLock;
use std::sync::{Arc, Mutex, MutexGuard};
use std::time::Duration;
@ -881,23 +881,27 @@ impl Endpoint {
}
}
static ENUMERATOR: Lazy<Enumerator> = Lazy::new(|| {
// COM initialization is thread local, but we only need to have COM initialized in the
// thread we create the objects in
com::com_initialized();
static ENUMERATOR: OnceLock<Enumerator> = OnceLock::new();
// building the devices enumerator object
unsafe {
let enumerator = Com::CoCreateInstance::<_, Audio::IMMDeviceEnumerator>(
&Audio::MMDeviceEnumerator,
None,
Com::CLSCTX_ALL,
)
.unwrap();
fn get_enumerator() -> &'static Enumerator {
ENUMERATOR.get_or_init(|| {
// COM initialization is thread local, but we only need to have COM initialized in the
// thread we create the objects in
com::com_initialized();
Enumerator(enumerator)
}
});
// building the devices enumerator object
unsafe {
let enumerator = Com::CoCreateInstance::<_, Audio::IMMDeviceEnumerator>(
&Audio::MMDeviceEnumerator,
None,
Com::CLSCTX_ALL,
)
.unwrap();
Enumerator(enumerator)
}
})
}
/// Send/Sync wrapper around `IMMDeviceEnumerator`.
struct Enumerator(Audio::IMMDeviceEnumerator);
@ -916,7 +920,7 @@ impl Devices {
pub fn new() -> Result<Self, DevicesError> {
unsafe {
// can fail because of wrong parameters (should never happen) or out of memory
let collection = ENUMERATOR
let collection = get_enumerator()
.0
.EnumAudioEndpoints(Audio::eAll, Audio::DEVICE_STATE_ACTIVE)
.map_err(BackendSpecificError::from)?;
@ -960,7 +964,7 @@ impl Iterator for Devices {
fn default_device(data_flow: Audio::EDataFlow) -> Option<Device> {
unsafe {
let device = ENUMERATOR
let device = get_enumerator()
.0
.GetDefaultAudioEndpoint(data_flow, Audio::eConsole)
.ok()?;