Skip to content

Commit 3fc3a87

Browse files
committed
refactor(duplex): add default impl for build_duplex_stream_raw
Reduces duplex stub code by ~50% across all backends by providing a default implementation in DeviceTrait that returns StreamConfigNotSupported. Changes: - Add default implementation for build_duplex_stream_raw() in DeviceTrait - Remove build_duplex_stream_raw implementations from all backends - Keep minimal DuplexStream wrapper structs for type distinction - Wrapper structs delegate play/pause to inner UnsupportedDuplexStream This maintains the breaking change (backends must provide DuplexStream type) but significantly reduces boilerplate code per backend from ~30 to ~15 lines.
1 parent c92dadd commit 3fc3a87

File tree

14 files changed

+27
-285
lines changed

14 files changed

+27
-285
lines changed

src/host/aaudio/mod.rs

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -115,20 +115,7 @@ pub struct Host;
115115
#[derive(Clone)]
116116
pub struct Device(Option<AudioDeviceInfo>);
117117

118-
/// Duplex stream placeholder for AAudio.
119-
///
120-
/// Duplex streams are not yet implemented for AAudio.
121-
pub struct DuplexStream(crate::duplex::UnsupportedDuplexStream);
122-
123-
impl StreamTrait for DuplexStream {
124-
fn play(&self) -> Result<(), PlayStreamError> {
125-
StreamTrait::play(&self.0)
126-
}
127-
128-
fn pause(&self) -> Result<(), PauseStreamError> {
129-
StreamTrait::pause(&self.0)
130-
}
131-
}
118+
pub struct DuplexStream(pub crate::duplex::UnsupportedDuplexStream);
132119

133120
/// Stream wraps AudioStream in Arc<Mutex<>> to provide Send + Sync semantics.
134121
///
@@ -591,21 +578,6 @@ impl DeviceTrait for Device {
591578
sample_format,
592579
)
593580
}
594-
595-
fn build_duplex_stream_raw<D, E>(
596-
&self,
597-
_config: &crate::duplex::DuplexStreamConfig,
598-
_sample_format: SampleFormat,
599-
_data_callback: D,
600-
_error_callback: E,
601-
_timeout: Option<Duration>,
602-
) -> Result<Self::DuplexStream, BuildStreamError>
603-
where
604-
D: FnMut(&Data, &mut Data, &crate::duplex::DuplexCallbackInfo) + Send + 'static,
605-
E: FnMut(StreamError) + Send + 'static,
606-
{
607-
Err(BuildStreamError::StreamConfigNotSupported)
608-
}
609581
}
610582

611583
impl StreamTrait for Stream {

src/host/alsa/mod.rs

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -88,20 +88,7 @@ const DEFAULT_DEVICE: &str = "default";
8888
// TODO: Not yet defined in rust-lang/libc crate
8989
const LIBC_ENOTSUPP: libc::c_int = 524;
9090

91-
/// Duplex stream placeholder for ALSA.
92-
///
93-
/// Duplex streams are not yet implemented for ALSA.
94-
pub struct DuplexStream(crate::duplex::UnsupportedDuplexStream);
95-
96-
impl crate::traits::StreamTrait for DuplexStream {
97-
fn play(&self) -> Result<(), crate::PlayStreamError> {
98-
crate::traits::StreamTrait::play(&self.0)
99-
}
100-
101-
fn pause(&self) -> Result<(), crate::PauseStreamError> {
102-
crate::traits::StreamTrait::pause(&self.0)
103-
}
104-
}
91+
pub struct DuplexStream(pub crate::duplex::UnsupportedDuplexStream);
10592

10693
/// The default Linux and BSD host type.
10794
#[derive(Debug, Clone)]
@@ -269,23 +256,6 @@ impl DeviceTrait for Device {
269256
);
270257
Ok(stream)
271258
}
272-
273-
fn build_duplex_stream_raw<D, E>(
274-
&self,
275-
_config: &crate::duplex::DuplexStreamConfig,
276-
_sample_format: crate::SampleFormat,
277-
_data_callback: D,
278-
_error_callback: E,
279-
_timeout: Option<std::time::Duration>,
280-
) -> Result<Self::DuplexStream, crate::BuildStreamError>
281-
where
282-
D: FnMut(&crate::Data, &mut crate::Data, &crate::duplex::DuplexCallbackInfo)
283-
+ Send
284-
+ 'static,
285-
E: FnMut(crate::StreamError) + Send + 'static,
286-
{
287-
Err(crate::BuildStreamError::StreamConfigNotSupported)
288-
}
289259
}
290260

291261
#[derive(Debug)]

src/host/asio/mod.rs

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,7 @@ use std::time::Duration;
2121
mod device;
2222
mod stream;
2323

24-
/// Duplex stream placeholder for ASIO.
25-
///
26-
/// Duplex streams are not yet implemented for ASIO.
27-
pub struct DuplexStream(crate::duplex::UnsupportedDuplexStream);
28-
29-
impl StreamTrait for DuplexStream {
30-
fn play(&self) -> Result<(), PlayStreamError> {
31-
StreamTrait::play(&self.0)
32-
}
33-
34-
fn pause(&self) -> Result<(), PauseStreamError> {
35-
StreamTrait::pause(&self.0)
36-
}
37-
}
24+
pub struct DuplexStream(pub crate::duplex::UnsupportedDuplexStream);
3825

3926
/// Global ASIO instance shared across all Host instances.
4027
///
@@ -159,21 +146,6 @@ impl DeviceTrait for Device {
159146
timeout,
160147
)
161148
}
162-
163-
fn build_duplex_stream_raw<D, E>(
164-
&self,
165-
_config: &crate::duplex::DuplexStreamConfig,
166-
_sample_format: SampleFormat,
167-
_data_callback: D,
168-
_error_callback: E,
169-
_timeout: Option<Duration>,
170-
) -> Result<Self::DuplexStream, BuildStreamError>
171-
where
172-
D: FnMut(&Data, &mut Data, &crate::duplex::DuplexCallbackInfo) + Send + 'static,
173-
E: FnMut(StreamError) + Send + 'static,
174-
{
175-
Err(BuildStreamError::StreamConfigNotSupported)
176-
}
177149
}
178150

179151
impl StreamTrait for Stream {

src/host/audioworklet/mod.rs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ pub struct Device;
2828

2929
pub struct Host;
3030

31-
/// Duplex stream placeholder for AudioWorklet.
32-
///
33-
/// Duplex streams are not yet implemented for AudioWorklet.
34-
pub struct DuplexStream(crate::duplex::UnsupportedDuplexStream);
31+
pub struct DuplexStream(pub crate::duplex::UnsupportedDuplexStream);
3532

3633
impl StreamTrait for DuplexStream {
3734
fn play(&self) -> Result<(), PlayStreamError> {
@@ -192,21 +189,6 @@ impl DeviceTrait for Device {
192189
Err(BuildStreamError::StreamConfigNotSupported)
193190
}
194191

195-
fn build_duplex_stream_raw<D, E>(
196-
&self,
197-
_config: &crate::duplex::DuplexStreamConfig,
198-
_sample_format: SampleFormat,
199-
_data_callback: D,
200-
_error_callback: E,
201-
_timeout: Option<Duration>,
202-
) -> Result<Self::DuplexStream, BuildStreamError>
203-
where
204-
D: FnMut(&Data, &mut Data, &crate::duplex::DuplexCallbackInfo) + Send + 'static,
205-
E: FnMut(StreamError) + Send + 'static,
206-
{
207-
Err(BuildStreamError::StreamConfigNotSupported)
208-
}
209-
210192
/// Create an output stream.
211193
fn build_output_stream_raw<D, E>(
212194
&self,

src/host/coreaudio/ios/mod.rs

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,7 @@ pub struct Device;
3737

3838
pub struct Host;
3939

40-
/// Duplex stream placeholder for CoreAudio iOS.
41-
///
42-
/// Duplex streams are not yet implemented for iOS.
43-
pub struct DuplexStream(crate::duplex::UnsupportedDuplexStream);
44-
45-
impl StreamTrait for DuplexStream {
46-
fn play(&self) -> Result<(), PlayStreamError> {
47-
StreamTrait::play(&self.0)
48-
}
49-
50-
fn pause(&self) -> Result<(), PauseStreamError> {
51-
StreamTrait::pause(&self.0)
52-
}
53-
}
40+
pub struct DuplexStream(pub crate::duplex::UnsupportedDuplexStream);
5441

5542
impl Host {
5643
pub fn new() -> Result<Self, crate::HostUnavailable> {
@@ -238,21 +225,6 @@ impl DeviceTrait for Device {
238225
audio_unit,
239226
}))
240227
}
241-
242-
fn build_duplex_stream_raw<D, E>(
243-
&self,
244-
_config: &crate::duplex::DuplexStreamConfig,
245-
_sample_format: SampleFormat,
246-
_data_callback: D,
247-
_error_callback: E,
248-
_timeout: Option<Duration>,
249-
) -> Result<Self::DuplexStream, BuildStreamError>
250-
where
251-
D: FnMut(&Data, &mut Data, &crate::duplex::DuplexCallbackInfo) + Send + 'static,
252-
E: FnMut(StreamError) + Send + 'static,
253-
{
254-
Err(BuildStreamError::StreamConfigNotSupported)
255-
}
256228
}
257229

258230
pub struct Stream {

src/host/custom/mod.rs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,7 @@ impl Stream {
9999
}
100100
}
101101

102-
/// Duplex stream placeholder for custom backends.
103-
///
104-
/// Duplex streams are not yet supported for custom backends.
105-
pub struct DuplexStream(crate::duplex::UnsupportedDuplexStream);
102+
pub struct DuplexStream(pub crate::duplex::UnsupportedDuplexStream);
106103

107104
impl StreamTrait for DuplexStream {
108105
fn play(&self) -> Result<(), PlayStreamError> {
@@ -442,23 +439,6 @@ impl DeviceTrait for Device {
442439
timeout,
443440
)
444441
}
445-
446-
fn build_duplex_stream_raw<D, E>(
447-
&self,
448-
_config: &crate::duplex::DuplexStreamConfig,
449-
_sample_format: SampleFormat,
450-
_data_callback: D,
451-
_error_callback: E,
452-
_timeout: Option<Duration>,
453-
) -> Result<Self::DuplexStream, BuildStreamError>
454-
where
455-
D: FnMut(&crate::Data, &mut crate::Data, &crate::duplex::DuplexCallbackInfo)
456-
+ Send
457-
+ 'static,
458-
E: FnMut(StreamError) + Send + 'static,
459-
{
460-
Err(BuildStreamError::StreamConfigNotSupported)
461-
}
462442
}
463443

464444
impl StreamTrait for Stream {

src/host/emscripten/mod.rs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ pub struct Devices(bool);
3232
#[derive(Clone, Debug, PartialEq, Eq)]
3333
pub struct Device;
3434

35-
/// Duplex stream placeholder for Emscripten.
36-
///
37-
/// Duplex streams are not yet implemented for Emscripten.
38-
pub struct DuplexStream(crate::duplex::UnsupportedDuplexStream);
35+
pub struct DuplexStream(pub crate::duplex::UnsupportedDuplexStream);
3936

4037
impl StreamTrait for DuplexStream {
4138
fn play(&self) -> Result<(), PlayStreamError> {
@@ -260,21 +257,6 @@ impl DeviceTrait for Device {
260257

261258
Ok(stream)
262259
}
263-
264-
fn build_duplex_stream_raw<D, E>(
265-
&self,
266-
_config: &crate::duplex::DuplexStreamConfig,
267-
_sample_format: SampleFormat,
268-
_data_callback: D,
269-
_error_callback: E,
270-
_timeout: Option<Duration>,
271-
) -> Result<Self::DuplexStream, BuildStreamError>
272-
where
273-
D: FnMut(&Data, &mut Data, &crate::duplex::DuplexCallbackInfo) + Send + 'static,
274-
E: FnMut(StreamError) + Send + 'static,
275-
{
276-
Err(BuildStreamError::StreamConfigNotSupported)
277-
}
278260
}
279261

280262
impl StreamTrait for Stream {

src/host/jack/device.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -271,23 +271,6 @@ impl DeviceTrait for Device {
271271

272272
Ok(stream)
273273
}
274-
275-
fn build_duplex_stream_raw<D, E>(
276-
&self,
277-
_config: &crate::duplex::DuplexStreamConfig,
278-
_sample_format: crate::SampleFormat,
279-
_data_callback: D,
280-
_error_callback: E,
281-
_timeout: Option<std::time::Duration>,
282-
) -> Result<Self::DuplexStream, crate::BuildStreamError>
283-
where
284-
D: FnMut(&crate::Data, &mut crate::Data, &crate::duplex::DuplexCallbackInfo)
285-
+ Send
286-
+ 'static,
287-
E: FnMut(crate::StreamError) + Send + 'static,
288-
{
289-
Err(crate::BuildStreamError::StreamConfigNotSupported)
290-
}
291274
}
292275

293276
impl PartialEq for Device {

src/host/jack/mod.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
55
extern crate jack;
66

7-
use crate::traits::HostTrait;
8-
use crate::{DevicesError, SampleFormat};
7+
use crate::traits::{HostTrait, StreamTrait};
8+
use crate::{DevicesError, PauseStreamError, PlayStreamError, SampleFormat};
99

1010
mod device;
1111
mod stream;
@@ -16,18 +16,15 @@ pub use self::{
1616
stream::Stream,
1717
};
1818

19-
/// Duplex stream placeholder for JACK.
20-
///
21-
/// Duplex streams are not yet implemented for JACK.
22-
pub struct DuplexStream(crate::duplex::UnsupportedDuplexStream);
19+
pub struct DuplexStream(pub crate::duplex::UnsupportedDuplexStream);
2320

24-
impl crate::traits::StreamTrait for DuplexStream {
25-
fn play(&self) -> Result<(), crate::PlayStreamError> {
26-
self.0.play()
21+
impl StreamTrait for DuplexStream {
22+
fn play(&self) -> Result<(), PlayStreamError> {
23+
StreamTrait::play(&self.0)
2724
}
2825

29-
fn pause(&self) -> Result<(), crate::PauseStreamError> {
30-
self.0.pause()
26+
fn pause(&self) -> Result<(), PauseStreamError> {
27+
StreamTrait::pause(&self.0)
3128
}
3229
}
3330

src/host/null/mod.rs

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub struct Stream;
2727
crate::assert_stream_send!(Stream);
2828
crate::assert_stream_sync!(Stream);
2929

30-
pub struct DuplexStream(crate::duplex::UnsupportedDuplexStream);
30+
pub struct DuplexStream(pub crate::duplex::UnsupportedDuplexStream);
3131

3232
#[derive(Clone)]
3333
pub struct SupportedInputConfigs;
@@ -115,21 +115,6 @@ impl DeviceTrait for Device {
115115
{
116116
unimplemented!()
117117
}
118-
119-
fn build_duplex_stream_raw<D, E>(
120-
&self,
121-
_config: &crate::duplex::DuplexStreamConfig,
122-
_sample_format: SampleFormat,
123-
_data_callback: D,
124-
_error_callback: E,
125-
_timeout: Option<Duration>,
126-
) -> Result<Self::DuplexStream, BuildStreamError>
127-
where
128-
D: FnMut(&Data, &mut Data, &crate::duplex::DuplexCallbackInfo) + Send + 'static,
129-
E: FnMut(StreamError) + Send + 'static,
130-
{
131-
unimplemented!()
132-
}
133118
}
134119

135120
impl HostTrait for Host {
@@ -163,16 +148,6 @@ impl StreamTrait for Stream {
163148
}
164149
}
165150

166-
impl StreamTrait for DuplexStream {
167-
fn play(&self) -> Result<(), PlayStreamError> {
168-
StreamTrait::play(&self.0)
169-
}
170-
171-
fn pause(&self) -> Result<(), PauseStreamError> {
172-
StreamTrait::pause(&self.0)
173-
}
174-
}
175-
176151
impl Iterator for Devices {
177152
type Item = Device;
178153

0 commit comments

Comments
 (0)