Skip to content

Commit f36bc48

Browse files
committed
Make post_message generic, so users don't need to Box themselves
1 parent 9b1e30c commit f36bc48

File tree

5 files changed

+19
-24
lines changed

5 files changed

+19
-24
lines changed

src/context/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,11 @@ impl AudioContextRegistration {
105105
///
106106
/// The message will be handled by
107107
/// [`AudioProcessor::onmessage`](crate::render::AudioProcessor::onmessage).
108-
pub fn post_message(&self, msg: Box<dyn std::any::Any + Send + 'static>) {
109-
let wrapped = crate::message::ControlMessage::NodeMessage { id: self.id, msg };
108+
pub fn post_message<M: std::any::Any + Send + 'static>(&self, msg: M) {
109+
let wrapped = crate::message::ControlMessage::NodeMessage {
110+
id: self.id,
111+
msg: Box::new(msg),
112+
};
110113
let _ = self.context.send_control_msg(wrapped);
111114
}
112115
}

src/node/audio_buffer_source.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,7 @@ impl AudioScheduledSourceNode for AudioBufferSourceNode {
139139
panic!("InvalidStateError cannot stop before start");
140140
}
141141

142-
self.registration
143-
.post_message(Box::new(Control::Stop(when)));
142+
self.registration.post_message(Control::Stop(when));
144143
}
145144
}
146145

@@ -239,7 +238,7 @@ impl AudioBufferSourceNode {
239238
}
240239

241240
let control = Control::StartWithOffsetAndDuration(start, offset, duration);
242-
self.registration.post_message(Box::new(control));
241+
self.registration.post_message(control);
243242
}
244243

245244
/// Current buffer value (nullable)
@@ -260,7 +259,7 @@ impl AudioBufferSourceNode {
260259
panic!("InvalidStateError - cannot assign buffer twice");
261260
}
262261

263-
self.registration.post_message(Box::new(clone));
262+
self.registration.post_message(clone);
264263
}
265264

266265
/// K-rate [`AudioParam`] that defines the speed at which the [`AudioBuffer`]
@@ -287,8 +286,7 @@ impl AudioBufferSourceNode {
287286
}
288287

289288
pub fn set_loop(&self, value: bool) {
290-
self.registration
291-
.post_message(Box::new(Control::Loop(value)));
289+
self.registration.post_message(Control::Loop(value));
292290
}
293291

294292
/// Defines the loop start point, in the time reference of the [`AudioBuffer`]
@@ -297,8 +295,7 @@ impl AudioBufferSourceNode {
297295
}
298296

299297
pub fn set_loop_start(&self, value: f64) {
300-
self.registration
301-
.post_message(Box::new(Control::LoopStart(value)));
298+
self.registration.post_message(Control::LoopStart(value));
302299
}
303300

304301
/// Defines the loop end point, in the time reference of the [`AudioBuffer`]
@@ -307,8 +304,7 @@ impl AudioBufferSourceNode {
307304
}
308305

309306
pub fn set_loop_end(&self, value: f64) {
310-
self.registration
311-
.post_message(Box::new(Control::LoopEnd(value)));
307+
self.registration.post_message(Control::LoopEnd(value));
312308
}
313309
}
314310

src/node/constant_source.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ impl AudioScheduledSourceNode for ConstantSourceNode {
9292
}
9393

9494
fn start_at(&self, when: f64) {
95-
self.registration
96-
.post_message(Box::new(Schedule::Start(when)));
95+
self.registration.post_message(Schedule::Start(when));
9796
}
9897

9998
fn stop(&self) {
@@ -102,8 +101,7 @@ impl AudioScheduledSourceNode for ConstantSourceNode {
102101
}
103102

104103
fn stop_at(&self, when: f64) {
105-
self.registration
106-
.post_message(Box::new(Schedule::Stop(when)));
104+
self.registration.post_message(Schedule::Stop(when));
107105
}
108106
}
109107

src/node/oscillator.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,7 @@ impl AudioScheduledSourceNode for OscillatorNode {
156156
}
157157

158158
fn start_at(&self, when: f64) {
159-
self.registration
160-
.post_message(Box::new(Schedule::Start(when)));
159+
self.registration.post_message(Schedule::Start(when));
161160
}
162161

163162
fn stop(&self) {
@@ -166,8 +165,7 @@ impl AudioScheduledSourceNode for OscillatorNode {
166165
}
167166

168167
fn stop_at(&self, when: f64) {
169-
self.registration
170-
.post_message(Box::new(Schedule::Stop(when)));
168+
self.registration.post_message(Schedule::Stop(when));
171169
}
172170
}
173171

@@ -290,7 +288,7 @@ impl OscillatorNode {
290288
return;
291289
}
292290

293-
self.registration.post_message(Box::new(type_));
291+
self.registration.post_message(type_);
294292
}
295293

296294
/// Sets a `PeriodicWave` which describes a waveform to be used by the oscillator.
@@ -303,7 +301,7 @@ impl OscillatorNode {
303301
self.shared_type
304302
.store(OscillatorType::Custom as u32, Ordering::Release);
305303

306-
self.registration.post_message(Box::new(periodic_wave));
304+
self.registration.post_message(periodic_wave);
307305
}
308306
}
309307

src/param.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ impl AudioParam {
268268
panic!("InvalidStateError: automation rate cannot be changed for this param");
269269
}
270270

271-
self.registration().post_message(Box::new(value));
271+
self.registration().post_message(value);
272272
}
273273

274274
pub(crate) fn set_automation_rate_constrained(&mut self, value: bool) {
@@ -563,7 +563,7 @@ impl AudioParam {
563563
}
564564

565565
fn send_event(&self, event: AudioParamEvent) -> &Self {
566-
self.registration().post_message(Box::new(event));
566+
self.registration().post_message(event);
567567
self
568568
}
569569
}

0 commit comments

Comments
 (0)