Skip to content

Commit

Permalink
into iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
jdonszelmann committed Jun 8, 2023
1 parent 6ac58df commit e5aa09c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 deletions.
33 changes: 29 additions & 4 deletions src/ringbuffer_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub trait RingBufferWrite<T>: RingBuffer<T> + Extend<T> {

/// Defines behaviour for ringbuffers which allow for reading from the start of them (as a queue).
/// For arbitrary buffer access however, [`RingBufferExt`] is necessary.
pub trait RingBufferRead<T>: RingBuffer<T> {
pub trait RingBufferRead<T>: RingBuffer<T> + IntoIterator<Item = T> {
/// dequeues the top item off the ringbuffer, and moves this item out.
fn dequeue(&mut self) -> Option<T>;

Expand Down Expand Up @@ -358,8 +358,7 @@ mod iter {
}
}

/// `RingBufferMutIterator` holds a reference to a `RingBufferRead` and iterates over it. `index` is the
/// current iterator position.
/// `RingBufferMutIterator` holds a reference to a `RingBufferRead` and iterates over it.
pub struct RingBufferDrainingIterator<'rb, T, RB: RingBufferRead<T>> {
obj: &'rb mut RB,
phantom: PhantomData<T>,
Expand All @@ -382,9 +381,35 @@ mod iter {
self.obj.dequeue()
}
}

/// `RingBufferIntoIterator` holds a `RingBufferRead` and iterates over it.
pub struct RingBufferIntoIterator<T, RB: RingBufferRead<T>> {
obj: RB,
phantom: PhantomData<T>,
}

impl<T, RB: RingBufferRead<T>> RingBufferIntoIterator<T, RB> {
#[inline]
pub fn new(obj: RB) -> Self {
Self {
obj,
phantom: PhantomData,
}
}
}

impl<T, RB: RingBufferRead<T>> Iterator for RingBufferIntoIterator<T, RB> {
type Item = T;

fn next(&mut self) -> Option<Self::Item> {
self.obj.dequeue()
}
}
}

pub use iter::{RingBufferDrainingIterator, RingBufferIterator, RingBufferMutIterator};
pub use iter::{
RingBufferDrainingIterator, RingBufferIntoIterator, RingBufferIterator, RingBufferMutIterator,
};

/// Implement various functions on implementors of [`RingBufferRead`].
/// This is to avoid duplicate code.
Expand Down
13 changes: 12 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, RingBufferExt, RingBufferRead, RingBufferWrite};
use crate::ringbuffer_trait::{
RingBuffer, RingBufferExt, RingBufferIntoIterator, RingBufferRead, RingBufferWrite,
};

extern crate alloc;

Expand Down Expand Up @@ -235,6 +237,15 @@ unsafe impl<T, SIZE: RingbufferSize> RingBufferExt<T> for AllocRingBuffer<T, SIZ
}
}

impl<T, SIZE: RingbufferSize> IntoIterator for AllocRingBuffer<T, SIZE> {
type Item = T;
type IntoIter = RingBufferIntoIterator<T, Self>;

fn into_iter(self) -> Self::IntoIter {
RingBufferIntoIterator::new(self)
}
}

impl<T, SIZE: RingbufferSize> RingBufferRead<T> for AllocRingBuffer<T, SIZE> {
fn dequeue(&mut self) -> Option<T> {
if self.is_empty() {
Expand Down
10 changes: 10 additions & 0 deletions src/with_alloc/vecdeque.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::ringbuffer_trait::RingBufferIntoIterator;
use crate::with_alloc::alloc_ringbuffer::RingbufferSize;
use crate::{AllocRingBuffer, RingBuffer, RingBufferExt, RingBufferRead, RingBufferWrite};
use alloc::collections::VecDeque;
Expand Down Expand Up @@ -151,6 +152,15 @@ impl<T> RingBuffer<T> for GrowableAllocRingBuffer<T> {
}
}

impl<T> IntoIterator for GrowableAllocRingBuffer<T> {
type Item = T;
type IntoIter = RingBufferIntoIterator<T, Self>;

fn into_iter(self) -> Self::IntoIter {
RingBufferIntoIterator::new(self)
}
}

impl<T> RingBufferRead<T> for GrowableAllocRingBuffer<T> {
fn dequeue(&mut self) -> Option<T> {
self.pop_front()
Expand Down
10 changes: 10 additions & 0 deletions src/with_const_generics.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::ringbuffer_trait::RingBufferIntoIterator;
use crate::with_alloc::alloc_ringbuffer::RingbufferSize;
use crate::{RingBuffer, RingBufferExt, RingBufferRead, RingBufferWrite};
use core::iter::FromIterator;
Expand Down Expand Up @@ -213,6 +214,15 @@ unsafe fn get_unchecked_mut<T, const N: usize>(
.expect("const array ptr shouldn't be null!")
}

impl<T, const CAP: usize> IntoIterator for ConstGenericRingBuffer<T, CAP> {
type Item = T;
type IntoIter = RingBufferIntoIterator<T, Self>;

fn into_iter(self) -> Self::IntoIter {
RingBufferIntoIterator::new(self)
}
}

impl<T, const CAP: usize> RingBufferRead<T> for ConstGenericRingBuffer<T, CAP> {
fn dequeue(&mut self) -> Option<T> {
if self.is_empty() {
Expand Down

1 comment on commit e5aa09c

@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 # to comment.