diff options
author | Brian Cully <bjc@kublai.com> | 2019-08-13 11:14:25 -0400 |
---|---|---|
committer | Brian Cully <bjc@kublai.com> | 2019-08-13 11:14:25 -0400 |
commit | eb0a3c051cd52dbc3af44ade4c2afae10eba94ea (patch) | |
tree | 72281e657a0bb883eeedcce43686cee25a65b2b2 /src | |
parent | 9f270caa5d07dc93f38977ccb7b6b3089e06009d (diff) | |
download | starb-eb0a3c051cd52dbc3af44ade4c2afae10eba94ea.tar.gz starb-eb0a3c051cd52dbc3af44ade4c2afae10eba94ea.zip |
Add iterator implementation.
Diffstat (limited to 'src')
-rwxr-xr-x | src/lib.rs | 32 |
1 files changed, 28 insertions, 4 deletions
@@ -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<T> Send for Writer<'_, T> where T: Send {} unsafe impl<T> Sync for Writer<'_, T> {} -impl<'a, T> Reader<'a, T> +impl<T> Reader<'_, T> where T: Copy, { @@ -122,7 +120,17 @@ where } } -impl<'a, T> Writer<'a, T> +impl<T> Iterator for Reader<'_, T> +where + T: Copy, +{ + type Item = T; + fn next(&mut self) -> Option<Self::Item> { + self.shift() + } +} + +impl<T> 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::<usize>::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; + } + } } |