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

@ -7,6 +7,7 @@ documentation = "https://docs.rs/cpal"
license = "Apache-2.0" license = "Apache-2.0"
keywords = ["audio", "sound"] keywords = ["audio", "sound"]
edition = "2021" edition = "2021"
rust-version = "1.70"
[features] [features]
asio = ["asio-sys", "num-traits"] # Only available on Windows. See README for setup instructions. asio = ["asio-sys", "num-traits"] # Only available on Windows. See README for setup instructions.
@ -41,7 +42,6 @@ windows = { version = "0.52.0", features = [
asio-sys = { version = "0.2", path = "asio-sys", optional = true } asio-sys = { version = "0.2", path = "asio-sys", optional = true }
num-traits = { version = "0.2.6", optional = true } num-traits = { version = "0.2.6", optional = true }
parking_lot = "0.12" parking_lot = "0.12"
once_cell = "1.12"
[target.'cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd"))'.dependencies] [target.'cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd"))'.dependencies]
alsa = "0.8" alsa = "0.8"

View file

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