@@ -394,6 +394,39 @@ pub trait Signal {
394
394
}
395
395
}
396
396
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
+
397
430
/// Moves the `Signal` into a `Bus` from which its output may be divided into multiple other
398
431
/// `Signal`s in the form of `Output`s.
399
432
///
@@ -689,6 +722,14 @@ pub struct Delay<S> {
689
722
n_frames : usize ,
690
723
}
691
724
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
+
692
733
/// Converts a `Signal` to a type that yields the individual interleaved samples.
693
734
pub struct IntoInterleavedSamples < S >
694
735
where S : Signal ,
@@ -1695,6 +1736,20 @@ impl<S> Signal for Delay<S>
1695
1736
}
1696
1737
1697
1738
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
+
1698
1753
impl < S > IntoInterleavedSamples < S >
1699
1754
where S : Signal ,
1700
1755
{
0 commit comments