From 984ff0fd30fe77e9644e85d74e2c38445e1d963c Mon Sep 17 00:00:00 2001 From: Tatsuyuki Ishi Date: Thu, 18 Jul 2019 09:46:50 +0900 Subject: [PATCH] Add name to HostId --- examples/enumerate.rs | 2 +- src/platform/mod.rs | 28 +++++++++++++++++++--------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/examples/enumerate.rs b/examples/enumerate.rs index eddf2f4..6b62272 100644 --- a/examples/enumerate.rs +++ b/examples/enumerate.rs @@ -9,7 +9,7 @@ fn main() -> Result<(), failure::Error> { println!("Available hosts:\n {:?}", available_hosts); for host_id in available_hosts { - println!("{:?}", host_id); + println!("{}", host_id.name()); let host = cpal::host_from_id(host_id)?; let default_in = host.default_input_device().map(|e| e.name().unwrap()); let default_out = host.default_output_device().map(|e| e.name().unwrap()); diff --git a/src/platform/mod.rs b/src/platform/mod.rs index 3957168..6e9ee28 100644 --- a/src/platform/mod.rs +++ b/src/platform/mod.rs @@ -12,8 +12,8 @@ pub use self::platform_impl::*; // These dynamically dispatched types are necessary to allow for users to switch between hosts at // runtime. // -// For example the invocation `impl_platform_host(Wasapi wasapi, Asio asio)`, this macro should -// expand to: +// For example the invocation `impl_platform_host(Wasapi wasapi "WASAPI", Asio asio "ASIO")`, +// this macro should expand to: // // ``` // pub enum HostId { @@ -31,7 +31,7 @@ pub use self::platform_impl::*; // SupportedOutputFormats and all their necessary trait implementations. // ``` macro_rules! impl_platform_host { - ($($HostVariant:ident $host_mod:ident),*) => { + ($($HostVariant:ident $host_mod:ident $host_name:literal),*) => { /// All hosts supported by CPAL on this platform. pub const ALL_HOSTS: &'static [HostId] = &[ $( @@ -126,6 +126,16 @@ macro_rules! impl_platform_host { )* } + impl HostId { + pub fn name(&self) -> &'static str { + match self { + $( + HostId::$HostVariant => $host_name, + )* + } + } + } + impl Host { /// The unique identifier associated with this host. pub fn id(&self) -> HostId { @@ -468,7 +478,7 @@ mod platform_impl { SupportedOutputFormats as AlsaSupportedOutputFormats, }; - impl_platform_host!(Alsa alsa); + impl_platform_host!(Alsa alsa "ALSA"); /// The default host for the current compilation target platform. pub fn default_host() -> Host { @@ -491,7 +501,7 @@ mod platform_impl { SupportedOutputFormats as CoreAudioSupportedOutputFormats, }; - impl_platform_host!(CoreAudio coreaudio); + impl_platform_host!(CoreAudio coreaudio "CoreAudio"); /// The default host for the current compilation target platform. pub fn default_host() -> Host { @@ -513,7 +523,7 @@ mod platform_impl { SupportedOutputFormats as EmscriptenSupportedOutputFormats, }; - impl_platform_host!(Emscripten emscripten); + impl_platform_host!(Emscripten emscripten "Emscripten"); /// The default host for the current compilation target platform. pub fn default_host() -> Host { @@ -546,10 +556,10 @@ mod platform_impl { }; #[cfg(feature = "asio")] - impl_platform_host!(Asio asio, Wasapi wasapi); + impl_platform_host!(Asio asio "ASIO", Wasapi wasapi "WASAPI"); #[cfg(not(feature = "asio"))] - impl_platform_host!(Wasapi wasapi); + impl_platform_host!(Wasapi wasapi "WASAPI"); /// The default host for the current compilation target platform. pub fn default_host() -> Host { @@ -572,7 +582,7 @@ mod platform_impl { SupportedOutputFormats as NullSupportedOutputFormats, }; - impl_platform_host!(Null null); + impl_platform_host!(Null null "Null"); /// The default host for the current compilation target platform. pub fn default_host() -> Host {