Skip to content

Commit

Permalink
Merge pull request #106 from NULLx76/into-iterator-ref
Browse files Browse the repository at this point in the history
Into iterator ref
  • Loading branch information
jdonszelmann authored Jun 9, 2023
2 parents 9cd94e1 + 500f3c0 commit 3b85e63
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 3 deletions.
56 changes: 56 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ mod tests {
use std::vec;
use std::vec::Vec;

use crate::ringbuffer_trait::{RingBufferIterator, RingBufferMutIterator};
use crate::{AllocRingBuffer, ConstGenericRingBuffer, GrowableAllocRingBuffer, RingBuffer};

#[test]
Expand Down Expand Up @@ -216,6 +217,37 @@ mod tests {
test_iter(ConstGenericRingBuffer::<i32, 8>::new());
}

#[test]
fn run_test_iter_ref() {
fn test_iter<B>(mut b: B)
where
B: RingBuffer<i32>,
for<'a> &'a B: IntoIterator<Item = &'a i32, IntoIter = RingBufferIterator<'a, i32, B>>,
{
b.push(1);
b.push(2);
b.push(3);
b.push(4);
b.push(5);
b.push(6);
b.push(7);

let mut iter = (&b).into_iter();
assert_eq!(&1, iter.next().unwrap());
assert_eq!(&7, iter.next_back().unwrap());
assert_eq!(&2, iter.next().unwrap());
assert_eq!(&3, iter.next().unwrap());
assert_eq!(&6, iter.next_back().unwrap());
assert_eq!(&5, iter.next_back().unwrap());
assert_eq!(&4, iter.next().unwrap());
assert_eq!(None, iter.next());
}

test_iter(AllocRingBuffer::new(8));
test_iter(GrowableAllocRingBuffer::with_capacity(8));
test_iter(ConstGenericRingBuffer::<i32, 8>::new());
}

#[test]
fn run_test_into_iter() {
fn test_iter(mut b: impl RingBuffer<i32>) {
Expand Down Expand Up @@ -339,6 +371,30 @@ mod tests {
test_iter_mut(ConstGenericRingBuffer::<i32, 8>::new());
}

#[test]
fn run_test_iter_mut_ref() {
fn test_iter_mut<B>(mut b: B)
where
B: RingBuffer<i32>,
for<'a> &'a mut B:
IntoIterator<Item = &'a mut i32, IntoIter = RingBufferMutIterator<'a, i32, B>>,
{
b.push(1);
b.push(2);
b.push(3);

for el in &mut b {
*el += 1;
}

assert_eq!(vec![2, 3, 4], b.to_vec())
}

test_iter_mut(AllocRingBuffer::new(8));
test_iter_mut(GrowableAllocRingBuffer::with_capacity(8));
test_iter_mut(ConstGenericRingBuffer::<i32, 8>::new());
}

#[test]
fn test_iter_mut_wrap() {
fn run_test_iter_mut_wrap(mut b: impl RingBuffer<i32>) {
Expand Down
22 changes: 21 additions & 1 deletion src/with_alloc/alloc_ringbuffer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use core::ops::{Index, IndexMut};

use crate::ringbuffer_trait::{RingBuffer, RingBufferIntoIterator};
use crate::ringbuffer_trait::{
RingBuffer, RingBufferIntoIterator, RingBufferIterator, RingBufferMutIterator,
};

extern crate alloc;

Expand Down Expand Up @@ -231,6 +233,24 @@ impl<T, SIZE: RingbufferSize> IntoIterator for AllocRingBuffer<T, SIZE> {
}
}

impl<'a, T, SIZE: RingbufferSize> IntoIterator for &'a AllocRingBuffer<T, SIZE> {
type Item = &'a T;
type IntoIter = RingBufferIterator<'a, T, AllocRingBuffer<T, SIZE>>;

fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}

impl<'a, T, SIZE: RingbufferSize> IntoIterator for &'a mut AllocRingBuffer<T, SIZE> {
type Item = &'a mut T;
type IntoIter = RingBufferMutIterator<'a, T, AllocRingBuffer<T, SIZE>>;

fn into_iter(self) -> Self::IntoIter {
self.iter_mut()
}
}

impl<T, SIZE: RingbufferSize> Extend<T> for AllocRingBuffer<T, SIZE> {
fn extend<A: IntoIterator<Item = T>>(&mut self, iter: A) {
let iter = iter.into_iter();
Expand Down
20 changes: 19 additions & 1 deletion src/with_alloc/vecdeque.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::ringbuffer_trait::RingBufferIntoIterator;
use crate::ringbuffer_trait::{RingBufferIntoIterator, RingBufferIterator, RingBufferMutIterator};
use crate::with_alloc::alloc_ringbuffer::RingbufferSize;
use crate::{AllocRingBuffer, RingBuffer};
use alloc::collections::VecDeque;
Expand Down Expand Up @@ -151,6 +151,24 @@ impl<T> IntoIterator for GrowableAllocRingBuffer<T> {
}
}

impl<'a, T> IntoIterator for &'a GrowableAllocRingBuffer<T> {
type Item = &'a T;
type IntoIter = RingBufferIterator<'a, T, GrowableAllocRingBuffer<T>>;

fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}

impl<'a, T> IntoIterator for &'a mut GrowableAllocRingBuffer<T> {
type Item = &'a mut T;
type IntoIter = RingBufferMutIterator<'a, T, GrowableAllocRingBuffer<T>>;

fn into_iter(self) -> Self::IntoIter {
self.iter_mut()
}
}

unsafe impl<T> RingBuffer<T> for GrowableAllocRingBuffer<T> {
unsafe fn ptr_len(rb: *const Self) -> usize {
(*rb).0.len()
Expand Down
20 changes: 19 additions & 1 deletion src/with_const_generics.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::ringbuffer_trait::RingBufferIntoIterator;
use crate::ringbuffer_trait::{RingBufferIntoIterator, RingBufferIterator, RingBufferMutIterator};
use crate::with_alloc::alloc_ringbuffer::RingbufferSize;
use crate::RingBuffer;
use core::iter::FromIterator;
Expand Down Expand Up @@ -223,6 +223,24 @@ impl<T, const CAP: usize> IntoIterator for ConstGenericRingBuffer<T, CAP> {
}
}

impl<'a, T, const CAP: usize> IntoIterator for &'a ConstGenericRingBuffer<T, CAP> {
type Item = &'a T;
type IntoIter = RingBufferIterator<'a, T, ConstGenericRingBuffer<T, CAP>>;

fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}

impl<'a, T, const CAP: usize> IntoIterator for &'a mut ConstGenericRingBuffer<T, CAP> {
type Item = &'a mut T;
type IntoIter = RingBufferMutIterator<'a, T, ConstGenericRingBuffer<T, CAP>>;

fn into_iter(self) -> Self::IntoIter {
self.iter_mut()
}
}

impl<T, const CAP: usize> Extend<T> for ConstGenericRingBuffer<T, CAP> {
fn extend<A: IntoIterator<Item = T>>(&mut self, iter: A) {
let iter = iter.into_iter();
Expand Down

1 comment on commit 3b85e63

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.