From eb0a3c051cd52dbc3af44ade4c2afae10eba94ea Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Tue, 13 Aug 2019 11:14:25 -0400 Subject: Add iterator implementation. --- src/lib.rs | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 70e1386..4d0a96b 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,8 +3,6 @@ // itself, is required to allow creating RingBuffers // statically). There may be work-arounds with ptr routines, and it // should be investigated. -// -// TODO: Needs impls for Iter/IterMut. #![no_std] #![feature(const_fn)] @@ -84,7 +82,7 @@ pub struct Writer<'a, T> { unsafe impl Send for Writer<'_, T> where T: Send {} unsafe impl Sync for Writer<'_, T> {} -impl<'a, T> Reader<'a, T> +impl Reader<'_, T> where T: Copy, { @@ -122,7 +120,17 @@ where } } -impl<'a, T> Writer<'a, T> +impl Iterator for Reader<'_, T> +where + T: Copy, +{ + type Item = T; + fn next(&mut self) -> Option { + self.shift() + } +} + +impl Writer<'_, T> where T: Copy, { @@ -222,4 +230,20 @@ mod test { rbr.shift(); assert_eq!(rbw.unshift(0xffff), Ok(())); } + + #[test] + fn can_iter() { + let rb = RingBuffer::::new(0); + let (rbr, mut rbw) = rb.split(); + + for i in 0..CAPACITY - 1 { + assert_eq!(rbw.unshift(i), Ok(())); + } + + let mut i = 0; + for e in rbr { + assert_eq!(e, i); + i += 1; + } + } } -- cgit v1.2.3