From 2679ea845fe0460e1514d9f63c7c8048525e306b Mon Sep 17 00:00:00 2001 From: Chris Morgan Date: Sat, 23 Mar 2019 07:27:17 +1100 Subject: [PATCH] impl Debug for Device (#253) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The internal alsa, null and emscripten Device implementations already implemented Debug; but the coreaudio and wasapi ones, and therefore also the wrapper, did not. I decided to eschew the `Device(…)` wrapping in the outer layer (hence a custom implementation rather than `#[derive(Debug)]`), because `Device(Device)`, `Device(Device { … })` and so forth all look better without the extra `Device(…)` wrapping. On the wasapi and coreaudio implementations I put both the pointer and name. Name because it’s useful, pointer because on Windows at least I believe duplicated names are possible. (e.g. two monitors that include monitors, of the same type; I haven’t strictly confirmed this, because I killed those off harshly on my machine and don’t want to reinstate them.) I do not have access to a macOS device to confirm that the coreaudio implementation is sane, but I think it is. --- src/coreaudio/mod.rs | 10 ++++++++++ src/lib.rs | 6 ++++++ src/wasapi/device.rs | 10 ++++++++++ 3 files changed, 26 insertions(+) diff --git a/src/coreaudio/mod.rs b/src/coreaudio/mod.rs index ed6c32d..11e2861 100644 --- a/src/coreaudio/mod.rs +++ b/src/coreaudio/mod.rs @@ -15,6 +15,7 @@ use UnknownTypeInputBuffer; use UnknownTypeOutputBuffer; use std::ffi::CStr; +use std::fmt; use std::mem; use std::os::raw::c_char; use std::ptr::null; @@ -302,6 +303,15 @@ impl Device { } } +impl fmt::Debug for Device { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_struct("Device") + .field("audio_device_id", &self.audio_device_id) + .field("name", &self.name()) + .finish() + } +} + // The ID of a stream is its index within the `streams` array of the events loop. #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct StreamId(usize); diff --git a/src/lib.rs b/src/lib.rs index e7f8e8b..2303819 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -395,6 +395,12 @@ impl Device { } } +impl fmt::Debug for Device { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Debug::fmt(&self.0, f) + } +} + impl EventLoop { /// Initializes a new events loop. #[inline] diff --git a/src/wasapi/device.rs b/src/wasapi/device.rs index 0ef90eb..7331381 100644 --- a/src/wasapi/device.rs +++ b/src/wasapi/device.rs @@ -1,5 +1,6 @@ use std; use std::ffi::OsString; +use std::fmt; use std::io::Error as IoError; use std::mem; use std::ops::{Deref, DerefMut}; @@ -552,6 +553,15 @@ impl Clone for Device { } } +impl fmt::Debug for Device { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_struct("Device") + .field("device", &self.device) + .field("name", &self.name()) + .finish() + } +} + impl Drop for Device { #[inline] fn drop(&mut self) {