Skip to content

Commit 340a9bb

Browse files
authored
Merge pull request #449 from orottier/feature/impl-debug-public-types
Implement `Debug` for all public facing types
2 parents 2544ef1 + db3e08e commit 340a9bb

38 files changed

+218
-12
lines changed

src/analysis.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,17 @@ pub(crate) struct Analyser {
156156
blackman: Vec<f32>,
157157
}
158158

159+
impl std::fmt::Debug for Analyser {
160+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
161+
f.debug_struct("Analyser")
162+
.field("fft_size", &self.fft_size())
163+
.field("smoothing_time_constant", &self.smoothing_time_constant())
164+
.field("min_decibels", &self.min_decibels())
165+
.field("max_decibels", &self.max_decibels())
166+
.finish_non_exhaustive()
167+
}
168+
}
169+
159170
impl Analyser {
160171
pub fn new() -> Self {
161172
let ring_buffer = AnalyserRingBuffer::new();

src/buffer.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,11 +364,19 @@ impl AudioBuffer {
364364
/// Single channel audio samples, basically wraps a `Arc<Vec<f32>>`
365365
///
366366
/// ChannelData has copy-on-write semantics, so it is cheap to clone.
367-
#[derive(Clone, Debug, PartialEq)]
367+
#[derive(Clone, PartialEq)]
368368
pub(crate) struct ChannelData {
369369
data: Arc<Vec<f32>>,
370370
}
371371

372+
impl std::fmt::Debug for ChannelData {
373+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
374+
f.debug_struct("ChannelData")
375+
.field("len", &self.len())
376+
.finish_non_exhaustive()
377+
}
378+
}
379+
372380
impl ChannelData {
373381
pub fn new(length: usize) -> Self {
374382
let buffer = vec![0.; length];

src/capacity.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ use crate::context::{BaseAudioContext, ConcreteBaseAudioContext};
55
use crate::events::{EventDispatch, EventHandler, EventPayload, EventType};
66
use crate::Event;
77

8-
#[derive(Copy, Clone)]
8+
#[derive(Copy, Clone, Debug)]
99
pub(crate) struct AudioRenderCapacityLoad {
1010
pub render_timestamp: f64,
1111
pub load_value: f64,
1212
}
1313

1414
/// Options for constructing an `AudioRenderCapacity`
15+
#[derive(Clone, Debug)]
1516
pub struct AudioRenderCapacityOptions {
1617
/// An update interval (in seconds) for dispatching [`AudioRenderCapacityEvent`]s
1718
pub update_interval: f64,
@@ -70,6 +71,17 @@ pub struct AudioRenderCapacity {
7071
stop_send: Arc<Mutex<Option<Sender<()>>>>,
7172
}
7273

74+
impl std::fmt::Debug for AudioRenderCapacity {
75+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
76+
f.debug_struct("AudioRenderCapacity")
77+
.field(
78+
"context",
79+
&format!("BaseAudioContext@{}", self.context.address()),
80+
)
81+
.finish_non_exhaustive()
82+
}
83+
}
84+
7385
impl AudioRenderCapacity {
7486
pub(crate) fn new(
7587
context: ConcreteBaseAudioContext,

src/context/concrete_base.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,11 @@ impl AudioNodeIdProvider {
5252
/// [`OfflineAudioContext`](crate::context::OfflineAudioContext), and the `context()` method on
5353
/// `AudioNode`s.
5454
///
55-
/// The `ConcreteBaseAudioContext` allows for cheap cloning (using an `Arc` internally).
55+
/// The `ConcreteBaseAudioContext` allows for shallow cloning (using an `Arc` internally).
5656
#[allow(clippy::module_name_repetitions)]
5757
#[derive(Clone)]
5858
#[doc(hidden)]
5959
pub struct ConcreteBaseAudioContext {
60-
/// inner makes `ConcreteBaseAudioContext` cheap to clone
6160
inner: Arc<ConcreteBaseAudioContextInner>,
6261
}
6362

@@ -67,6 +66,19 @@ impl PartialEq for ConcreteBaseAudioContext {
6766
}
6867
}
6968

69+
impl std::fmt::Debug for ConcreteBaseAudioContext {
70+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
71+
f.debug_struct("BaseAudioContext")
72+
.field("id", &self.address())
73+
.field("state", &self.state())
74+
.field("sample_rate", &self.sample_rate())
75+
.field("current_time", &self.current_time())
76+
.field("max_channel_count", &self.max_channel_count())
77+
.field("offline", &self.offline())
78+
.finish_non_exhaustive()
79+
}
80+
}
81+
7082
/// Inner representation of the `ConcreteBaseAudioContext`
7183
///
7284
/// These fields are wrapped inside an `Arc` in the actual `ConcreteBaseAudioContext`.
@@ -257,6 +269,10 @@ impl ConcreteBaseAudioContext {
257269
base
258270
}
259271

272+
pub(crate) fn address(&self) -> usize {
273+
Arc::as_ptr(&self.inner) as usize
274+
}
275+
260276
/// Send a control message to the render thread
261277
///
262278
/// When the render thread is closed or crashed, the message is discarded and a log warning is

src/context/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//! The `BaseAudioContext` interface and the `AudioContext` and `OfflineAudioContext` types
2+
23
use std::{any::Any, ops::Range};
34

45
mod base;
@@ -94,6 +95,18 @@ pub struct AudioContextRegistration {
9495
id: AudioNodeId,
9596
}
9697

98+
impl std::fmt::Debug for AudioContextRegistration {
99+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
100+
f.debug_struct("AudioContextRegistration")
101+
.field("id", &self.id)
102+
.field(
103+
"context",
104+
&format!("BaseAudioContext@{}", self.context.address()),
105+
)
106+
.finish()
107+
}
108+
}
109+
97110
impl AudioContextRegistration {
98111
/// Get the audio node id of the registration
99112
#[must_use]

src/context/offline.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ pub struct OfflineAudioContext {
2929
resume_sender: mpsc::Sender<()>,
3030
}
3131

32+
impl std::fmt::Debug for OfflineAudioContext {
33+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34+
f.debug_struct("OfflineAudioContext")
35+
.field("length", &self.length())
36+
.field("base", &self.base())
37+
.finish_non_exhaustive()
38+
}
39+
}
40+
3241
struct OfflineAudioContextRenderer {
3342
/// the rendering 'thread', fully controlled by the offline context
3443
renderer: RenderThread,

src/context/online.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,17 @@ pub struct AudioContext {
118118
render_thread_init: RenderThreadInit,
119119
}
120120

121+
impl std::fmt::Debug for AudioContext {
122+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
123+
f.debug_struct("AudioContext")
124+
.field("sink_id", &self.sink_id())
125+
.field("base_latency", &self.base_latency())
126+
.field("output_latency", &self.output_latency())
127+
.field("base", &self.base())
128+
.finish_non_exhaustive()
129+
}
130+
}
131+
121132
impl BaseAudioContext for AudioContext {
122133
fn base(&self) -> &ConcreteBaseAudioContext {
123134
&self.base

src/lib.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![warn(clippy::missing_panics_doc)]
55
#![warn(clippy::clone_on_ref_ptr)]
66
#![deny(trivial_numeric_casts)]
7+
#![deny(missing_debug_implementations)]
78

89
use std::error::Error;
910
use std::sync::atomic::{AtomicU32, AtomicU64, Ordering};
@@ -56,12 +57,17 @@ pub use media_element::MediaElement;
5657
mod resampling;
5758
pub mod worklet;
5859

59-
#[derive(Debug)]
6060
#[repr(transparent)]
6161
pub(crate) struct AtomicF32 {
6262
bits: AtomicU32,
6363
}
6464

65+
impl std::fmt::Debug for AtomicF32 {
66+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67+
f.write_fmt(format_args!("{}", self.load(Ordering::Relaxed)))
68+
}
69+
}
70+
6571
impl AtomicF32 {
6672
#[must_use]
6773
pub fn new(value: f32) -> Self {
@@ -81,12 +87,17 @@ impl AtomicF32 {
8187
}
8288

8389
/// Atomic float 64, only `load` and `store` are supported, no arithmetic
84-
#[derive(Debug)]
8590
#[repr(transparent)]
8691
pub(crate) struct AtomicF64 {
8792
bits: AtomicU64,
8893
}
8994

95+
impl std::fmt::Debug for AtomicF64 {
96+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
97+
f.write_fmt(format_args!("{}", self.load(Ordering::Relaxed)))
98+
}
99+
}
100+
90101
impl AtomicF64 {
91102
#[must_use]
92103
pub fn new(value: f64) -> Self {

src/media_devices/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,14 @@ impl MediaDeviceInfo {
127127

128128
/// Dictionary used to instruct what sort of tracks to include in the [`MediaStream`] returned by
129129
/// [`get_user_media_sync`]
130+
#[derive(Clone, Debug)]
130131
pub enum MediaStreamConstraints {
131132
Audio,
132133
AudioWithConstraints(MediaTrackConstraints),
133134
}
134135

135136
/// Desired media stream track settings for [`MediaTrackConstraints`]
136-
#[derive(Default)]
137+
#[derive(Default, Debug, Clone)]
137138
#[non_exhaustive]
138139
pub struct MediaTrackConstraints {
139140
// ConstrainULong width;

src/media_element.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ pub(crate) struct RTSStream {
1919
playback_rate: Arc<AtomicF64>,
2020
}
2121

22+
impl std::fmt::Debug for RTSStream {
23+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24+
f.debug_struct("RTSStream")
25+
.field("number_of_channels", &self.number_of_channels)
26+
.finish_non_exhaustive()
27+
}
28+
}
29+
2230
/// Controller actions for a media element
2331
pub(crate) enum MediaElementAction {
2432
/// Seek to the given timestamp
@@ -46,6 +54,18 @@ pub struct MediaElement {
4654
playback_rate: Arc<AtomicF64>,
4755
}
4856

57+
impl std::fmt::Debug for MediaElement {
58+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59+
f.debug_struct("MediaElement")
60+
.field("stream", &self.stream)
61+
.field("current_time", &self.current_time())
62+
.field("loop", &self.loop_())
63+
.field("paused", &self.paused())
64+
.field("playback_rate", &self.playback_rate())
65+
.finish_non_exhaustive()
66+
}
67+
}
68+
4969
impl MediaElement {
5070
/// Create a new instance for a given file path
5171
pub fn new<P: Into<PathBuf>>(file: P) -> Result<Self, Box<dyn Error>> {

0 commit comments

Comments
 (0)