Skip to content

Commit 49e3670

Browse files
Merge pull request RustAudio#69 from mitchmindtree/inspect
Resolve conflicts with @andrewcsmith's Inspect Signal adaptor
2 parents 2c661f9 + 677aa6b commit 49e3670

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

src/signal.rs

+55
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,39 @@ pub trait Signal {
394394
}
395395
}
396396

397+
/// Create a new `Signal` that calls the enclosing function on each iteration.
398+
///
399+
/// # Example
400+
///
401+
/// ```rust
402+
/// extern crate sample;
403+
///
404+
/// use sample::{signal, Signal};
405+
///
406+
/// fn main() {
407+
/// let mut f = [0.0];
408+
/// let mut signal = signal::gen_mut(move || {
409+
/// f[0] += 0.1;
410+
/// f
411+
/// });
412+
/// let func = |x: &[f64; 1]| {
413+
/// assert_eq!(*x, [0.1]);
414+
/// };
415+
/// let mut inspected = signal.inspect(func);
416+
/// let out = inspected.next();
417+
/// assert_eq!(out, [0.1]);
418+
/// }
419+
/// ```
420+
fn inspect<F>(self, inspect: F) -> Inspect<Self, F>
421+
where Self: Sized,
422+
F: FnMut(&Self::Frame),
423+
{
424+
Inspect {
425+
signal: self,
426+
inspect: inspect,
427+
}
428+
}
429+
397430
/// Moves the `Signal` into a `Bus` from which its output may be divided into multiple other
398431
/// `Signal`s in the form of `Output`s.
399432
///
@@ -689,6 +722,14 @@ pub struct Delay<S> {
689722
n_frames: usize,
690723
}
691724

725+
/// A signal that calls its enclosing function and returns the original value. The signal may
726+
/// mutate state.
727+
#[derive(Clone)]
728+
pub struct Inspect<S, F> {
729+
signal: S,
730+
inspect: F,
731+
}
732+
692733
/// Converts a `Signal` to a type that yields the individual interleaved samples.
693734
pub struct IntoInterleavedSamples<S>
694735
where S: Signal,
@@ -1695,6 +1736,20 @@ impl<S> Signal for Delay<S>
16951736
}
16961737

16971738

1739+
impl<S, F> Signal for Inspect<S, F>
1740+
where S: Signal,
1741+
F: FnMut(&S::Frame),
1742+
{
1743+
type Frame = S::Frame;
1744+
#[inline]
1745+
fn next(&mut self) -> Self::Frame {
1746+
let out = self.signal.next();
1747+
(self.inspect)(&out);
1748+
out
1749+
}
1750+
}
1751+
1752+
16981753
impl<S> IntoInterleavedSamples<S>
16991754
where S: Signal,
17001755
{

0 commit comments

Comments
 (0)