diff --git a/src/fillers.rs b/src/fillers.rs new file mode 100644 index 0000000..2d2b4c6 --- /dev/null +++ b/src/fillers.rs @@ -0,0 +1,69 @@ +use num::traits::Num; + +use super::error::DspError; + +pub struct ConstFiller<'a, NumType> +where + NumType: Num + Copy, +{ + value: NumType, + iterator: Box> + 'a>, +} + +impl<'a, NumType> ConstFiller<'a, NumType> +where + NumType: Num + Copy, +{ + pub fn from(iterator: I, value: NumType) -> ConstFiller<'a, NumType> + where + I: Iterator> + 'a, + { + ConstFiller { + value: value, + iterator: Box::new(iterator), + } + } +} + +impl<'a, NumType> Iterator for ConstFiller<'a, NumType> +where + NumType: Num + Copy, +{ + type Item = Result; + + fn next(&mut self) -> Option { + match self.iterator.next() { + Some(Ok(x)) => Some(Ok(x)), + Some(Err(DspError::WouldBlock)) => Some(Ok(self.value)), + Some(Err(e)) => Some(Err(e)), + None => None, + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn const_filler_test() { + let test = vec![ + Ok(4_f32), + Err(DspError::WouldBlock), + Ok(5_f32), + Err(DspError::WouldBlock), + Ok(6_f32), + Ok(7_f32), + ]; + + let mut filler = ConstFiller::from(test.into_iter(), 23_f32); + + assert_eq!(filler.next(), Some(Ok(4_f32))); + assert_eq!(filler.next(), Some(Ok(23_f32))); + assert_eq!(filler.next(), Some(Ok(5_f32))); + assert_eq!(filler.next(), Some(Ok(23_f32))); + assert_eq!(filler.next(), Some(Ok(6_f32))); + assert_eq!(filler.next(), Some(Ok(7_f32))); + assert_eq!(filler.next(), None); + } +} diff --git a/src/lib.rs b/src/lib.rs index 3deb783..dca9409 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ mod amdemod; mod error; +mod fillers; mod firfilter; mod multiply; mod resamplers;