aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib.rs
diff options
context:
space:
mode:
authorBrian Cully <bjc@kublai.com>2019-08-13 11:14:25 -0400
committerBrian Cully <bjc@kublai.com>2019-08-13 11:14:25 -0400
commiteb0a3c051cd52dbc3af44ade4c2afae10eba94ea (patch)
tree72281e657a0bb883eeedcce43686cee25a65b2b2 /src/lib.rs
parent9f270caa5d07dc93f38977ccb7b6b3089e06009d (diff)
downloadstarb-eb0a3c051cd52dbc3af44ade4c2afae10eba94ea.tar.gz
starb-eb0a3c051cd52dbc3af44ade4c2afae10eba94ea.zip
Add iterator implementation.
Diffstat (limited to 'src/lib.rs')
-rwxr-xr-xsrc/lib.rs32
1 files changed, 28 insertions, 4 deletions
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<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;
+ }
+ }
}