Skip to content

Commit a5bd3c9

Browse files
committed
Use associated types instead of magical names in platform_impl
This reduces the exported surface area of each host implementation, and makes the impl_platform_host macro more robust.
1 parent 33b8919 commit a5bd3c9

File tree

1 file changed

+41
-75
lines changed

1 file changed

+41
-75
lines changed

src/platform/mod.rs

+41-75
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub use self::platform_impl::*;
3333
/// SupportedOutputConfigs and all their necessary trait implementations.
3434
///
3535
macro_rules! impl_platform_host {
36-
($($(#[cfg($feat: meta)])? $HostVariant:ident $host_mod:ident $host_name:literal),*) => {
36+
($($(#[cfg($feat: meta)])? $HostVariant:ident => $Host:ty),* $(,)?) => {
3737
/// All hosts supported by CPAL on this platform.
3838
pub const ALL_HOSTS: &'static [HostId] = &[
3939
$(
@@ -94,45 +94,45 @@ macro_rules! impl_platform_host {
9494
pub enum DeviceInner {
9595
$(
9696
$(#[cfg($feat)])?
97-
$HostVariant(crate::host::$host_mod::Device),
97+
$HostVariant(<$Host as crate::traits::HostTrait>::Device),
9898
)*
9999
}
100100

101101
/// Contains a platform specific [`Devices`] implementation.
102102
pub enum DevicesInner {
103103
$(
104104
$(#[cfg($feat)])?
105-
$HostVariant(crate::host::$host_mod::Devices),
105+
$HostVariant(<$Host as crate::traits::HostTrait>::Devices),
106106
)*
107107
}
108108

109109
/// Contains a platform specific [`Host`] implementation.
110110
pub enum HostInner {
111111
$(
112112
$(#[cfg($feat)])?
113-
$HostVariant(crate::host::$host_mod::Host),
113+
$HostVariant($Host),
114114
)*
115115
}
116116

117117
/// Contains a platform specific [`Stream`] implementation.
118118
pub enum StreamInner {
119119
$(
120120
$(#[cfg($feat)])?
121-
$HostVariant(crate::host::$host_mod::Stream),
121+
$HostVariant(<<$Host as crate::traits::HostTrait>::Device as crate::traits::DeviceTrait>::Stream),
122122
)*
123123
}
124124

125125
enum SupportedInputConfigsInner {
126126
$(
127127
$(#[cfg($feat)])?
128-
$HostVariant(crate::host::$host_mod::SupportedInputConfigs),
128+
$HostVariant(<<$Host as crate::traits::HostTrait>::Device as crate::traits::DeviceTrait>::SupportedInputConfigs),
129129
)*
130130
}
131131

132132
enum SupportedOutputConfigsInner {
133133
$(
134134
$(#[cfg($feat)])?
135-
$HostVariant(crate::host::$host_mod::SupportedOutputConfigs),
135+
$HostVariant(<<$Host as crate::traits::HostTrait>::Device as crate::traits::DeviceTrait>::SupportedInputConfigs),
136136
)*
137137
}
138138

@@ -141,7 +141,7 @@ macro_rules! impl_platform_host {
141141
match self {
142142
$(
143143
$(#[cfg($feat)])?
144-
HostId::$HostVariant => $host_name,
144+
HostId::$HostVariant => stringify!($HostVariant),
145145
)*
146146
}
147147
}
@@ -443,7 +443,7 @@ macro_rules! impl_platform_host {
443443
fn is_available() -> bool {
444444
$(
445445
$(#[cfg($feat)])?
446-
if crate::host::$host_mod::Host::is_available() { return true; }
446+
if <$Host>::is_available() { return true; }
447447
)*
448448
false
449449
}
@@ -532,29 +532,29 @@ macro_rules! impl_platform_host {
532532

533533
$(
534534
$(#[cfg($feat)])?
535-
impl From<crate::host::$host_mod::Device> for Device {
536-
fn from(h: crate::host::$host_mod::Device) -> Self {
535+
impl From<<$Host as crate::traits::HostTrait>::Device> for Device {
536+
fn from(h: <$Host as crate::traits::HostTrait>::Device) -> Self {
537537
DeviceInner::$HostVariant(h).into()
538538
}
539539
}
540540

541541
$(#[cfg($feat)])?
542-
impl From<crate::host::$host_mod::Devices> for Devices {
543-
fn from(h: crate::host::$host_mod::Devices) -> Self {
542+
impl From<<$Host as crate::traits::HostTrait>::Devices> for Devices {
543+
fn from(h: <$Host as crate::traits::HostTrait>::Devices) -> Self {
544544
DevicesInner::$HostVariant(h).into()
545545
}
546546
}
547547

548548
$(#[cfg($feat)])?
549-
impl From<crate::host::$host_mod::Host> for Host {
550-
fn from(h: crate::host::$host_mod::Host) -> Self {
549+
impl From<$Host> for Host {
550+
fn from(h: $Host) -> Self {
551551
HostInner::$HostVariant(h).into()
552552
}
553553
}
554554

555555
$(#[cfg($feat)])?
556-
impl From<crate::host::$host_mod::Stream> for Stream {
557-
fn from(h: crate::host::$host_mod::Stream) -> Self {
556+
impl From<<<$Host as crate::traits::HostTrait>::Device as crate::traits::DeviceTrait>::Stream> for Stream {
557+
fn from(h: <<$Host as crate::traits::HostTrait>::Device as crate::traits::DeviceTrait>::Stream) -> Self {
558558
StreamInner::$HostVariant(h).into()
559559
}
560560
}
@@ -565,7 +565,7 @@ macro_rules! impl_platform_host {
565565
let mut host_ids = vec![];
566566
$(
567567
$(#[cfg($feat)])?
568-
if <crate::host::$host_mod::Host as crate::traits::HostTrait>::is_available() {
568+
if <$Host as crate::traits::HostTrait>::is_available() {
569569
host_ids.push(HostId::$HostVariant);
570570
}
571571
)*
@@ -578,7 +578,7 @@ macro_rules! impl_platform_host {
578578
$(
579579
$(#[cfg($feat)])?
580580
HostId::$HostVariant => {
581-
crate::host::$host_mod::Host::new()
581+
<$Host>::new()
582582
.map(HostInner::$HostVariant)
583583
.map(Host::from)
584584
}
@@ -596,19 +596,14 @@ macro_rules! impl_platform_host {
596596
target_os = "netbsd"
597597
))]
598598
mod platform_impl {
599-
pub use crate::host::alsa::{
600-
Device as AlsaDevice, Devices as AlsaDevices, Host as AlsaHost, Stream as AlsaStream,
601-
SupportedInputConfigs as AlsaSupportedInputConfigs,
602-
SupportedOutputConfigs as AlsaSupportedOutputConfigs,
603-
};
599+
pub use crate::host::alsa::Host as AlsaHost;
604600
#[cfg(feature = "jack")]
605-
pub use crate::host::jack::{
606-
Device as JackDevice, Devices as JackDevices, Host as JackHost, Stream as JackStream,
607-
SupportedInputConfigs as JackSupportedInputConfigs,
608-
SupportedOutputConfigs as JackSupportedOutputConfigs,
609-
};
601+
pub use crate::host::jack::Host as JackHost;
610602

611-
impl_platform_host!(#[cfg(feature = "jack")] Jack jack "JACK", Alsa alsa "ALSA");
603+
impl_platform_host!(
604+
#[cfg(feature = "jack")] Jack => JackHost,
605+
Alsa => AlsaHost,
606+
);
612607

613608
/// The default host for the current compilation target platform.
614609
pub fn default_host() -> Host {
@@ -620,13 +615,8 @@ mod platform_impl {
620615

621616
#[cfg(any(target_os = "macos", target_os = "ios"))]
622617
mod platform_impl {
623-
pub use crate::host::coreaudio::{
624-
Device as CoreAudioDevice, Devices as CoreAudioDevices, Host as CoreAudioHost,
625-
Stream as CoreAudioStream, SupportedInputConfigs as CoreAudioSupportedInputConfigs,
626-
SupportedOutputConfigs as CoreAudioSupportedOutputConfigs,
627-
};
628-
629-
impl_platform_host!(CoreAudio coreaudio "CoreAudio");
618+
pub use crate::host::coreaudio::Host as CoreAudioHost;
619+
impl_platform_host!(CoreAudio => CoreAudioHost);
630620

631621
/// The default host for the current compilation target platform.
632622
pub fn default_host() -> Host {
@@ -638,13 +628,8 @@ mod platform_impl {
638628

639629
#[cfg(target_os = "emscripten")]
640630
mod platform_impl {
641-
pub use crate::host::emscripten::{
642-
Device as EmscriptenDevice, Devices as EmscriptenDevices, Host as EmscriptenHost,
643-
Stream as EmscriptenStream, SupportedInputConfigs as EmscriptenSupportedInputConfigs,
644-
SupportedOutputConfigs as EmscriptenSupportedOutputConfigs,
645-
};
646-
647-
impl_platform_host!(Emscripten emscripten "Emscripten");
631+
pub use crate::host::emscripten::Host as EmscriptenHost;
632+
impl_platform_host!(Emscripten => EmscriptenHost);
648633

649634
/// The default host for the current compilation target platform.
650635
pub fn default_host() -> Host {
@@ -656,13 +641,8 @@ mod platform_impl {
656641

657642
#[cfg(all(target_arch = "wasm32", feature = "wasm-bindgen"))]
658643
mod platform_impl {
659-
pub use crate::host::webaudio::{
660-
Device as WebAudioDevice, Devices as WebAudioDevices, Host as WebAudioHost,
661-
Stream as WebAudioStream, SupportedInputConfigs as WebAudioSupportedInputConfigs,
662-
SupportedOutputConfigs as WebAudioSupportedOutputConfigs,
663-
};
664-
665-
impl_platform_host!(WebAudio webaudio "WebAudio");
644+
pub use crate::host::webaudio::Host as WebAudioHost;
645+
impl_platform_host!(WebAudio => WebAudioHost);
666646

667647
/// The default host for the current compilation target platform.
668648
pub fn default_host() -> Host {
@@ -675,18 +655,13 @@ mod platform_impl {
675655
#[cfg(windows)]
676656
mod platform_impl {
677657
#[cfg(feature = "asio")]
678-
pub use crate::host::asio::{
679-
Device as AsioDevice, Devices as AsioDevices, Host as AsioHost, Stream as AsioStream,
680-
SupportedInputConfigs as AsioSupportedInputConfigs,
681-
SupportedOutputConfigs as AsioSupportedOutputConfigs,
682-
};
683-
pub use crate::host::wasapi::{
684-
Device as WasapiDevice, Devices as WasapiDevices, Host as WasapiHost,
685-
Stream as WasapiStream, SupportedInputConfigs as WasapiSupportedInputConfigs,
686-
SupportedOutputConfigs as WasapiSupportedOutputConfigs,
687-
};
658+
pub use crate::host::asio::Host as AsioHost;
659+
pub use crate::host::wasapi::Host as WasapiHost;
688660

689-
impl_platform_host!(#[cfg(feature = "asio")] Asio asio "ASIO", Wasapi wasapi "WASAPI");
661+
impl_platform_host!(
662+
#[cfg(feature = "asio")] Asio => AsioHost,
663+
Wasapi => WasapiHost,
664+
);
690665

691666
/// The default host for the current compilation target platform.
692667
pub fn default_host() -> Host {
@@ -698,13 +673,8 @@ mod platform_impl {
698673

699674
#[cfg(target_os = "android")]
700675
mod platform_impl {
701-
pub use crate::host::oboe::{
702-
Device as OboeDevice, Devices as OboeDevices, Host as OboeHost, Stream as OboeStream,
703-
SupportedInputConfigs as OboeSupportedInputConfigs,
704-
SupportedOutputConfigs as OboeSupportedOutputConfigs,
705-
};
706-
707-
impl_platform_host!(Oboe oboe "Oboe");
676+
pub use crate::host::oboe::Host as OboeHost;
677+
impl_platform_host!(Oboe => OboeHost);
708678

709679
/// The default host for the current compilation target platform.
710680
pub fn default_host() -> Host {
@@ -727,13 +697,9 @@ mod platform_impl {
727697
all(target_arch = "wasm32", feature = "wasm-bindgen"),
728698
)))]
729699
mod platform_impl {
730-
pub use crate::host::null::{
731-
Device as NullDevice, Devices as NullDevices, Host as NullHost,
732-
SupportedInputConfigs as NullSupportedInputConfigs,
733-
SupportedOutputConfigs as NullSupportedOutputConfigs,
734-
};
700+
pub use crate::host::null::Host as NullHost;
735701

736-
impl_platform_host!(Null null "Null");
702+
impl_platform_host!(Null => NullHost);
737703

738704
/// The default host for the current compilation target platform.
739705
pub fn default_host() -> Host {

0 commit comments

Comments
 (0)