Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/tests/dyn_compat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use crate::traits::{Consumer, Producer};

#[test]
fn producer_should_dyn_compatible() {
fn _asdf(_a: &dyn Producer<Item = f32>) {}
}

#[test]
fn consumer_should_dyn_compatible() {
fn _asdf(_a: &dyn Consumer<Item = f32>) {}
}
1 change: 1 addition & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod access;
mod basic;
#[cfg(feature = "alloc")]
mod drop;
mod dyn_compat;
mod fmt_write;
mod frozen;
mod hold;
Expand Down
9 changes: 7 additions & 2 deletions src/traits/consumer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ use super::{
};
use crate::utils::{move_uninit_slice, slice_as_uninit_mut, slice_assume_init_mut, slice_assume_init_ref};
use core::{iter::Chain, mem::MaybeUninit, ptr, slice};

#[cfg(feature = "std")]
use std::io::{self, Write};

/// Consumer part of ring buffer.

pub trait Consumer: Observer {
/// Set read index.
///
Expand Down Expand Up @@ -176,7 +178,10 @@ pub trait Consumer: Observer {
}

/// Returns an iterator that removes items one by one from the ring buffer.
fn pop_iter(&mut self) -> PopIter<'_, Self> {
fn pop_iter(&mut self) -> PopIter<'_, Self>
where
Self: Sized,
{
PopIter::new(self)
}

Expand Down Expand Up @@ -254,7 +259,7 @@ pub trait Consumer: Observer {
/// To achieve this we write only one contiguous slice at once. So this call may write less than `occupied_len` items even if the writer is ready to get more.
fn write_into<S: Write>(&mut self, writer: &mut S, count: Option<usize>) -> Option<io::Result<usize>>
where
Self: Consumer<Item = u8>,
Self: Consumer<Item = u8> + Sized,
{
let (left, _) = self.occupied_slices();
let count = usize::min(count.unwrap_or(left.len()), left.len());
Expand Down
21 changes: 17 additions & 4 deletions src/traits/producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ pub trait Producer: Observer {
///
/// *Inserted items are committed to the ring buffer all at once in the end,*
/// *e.g. when buffer is full or iterator has ended.*
fn push_iter<I: Iterator<Item = Self::Item>>(&mut self, mut iter: I) -> usize {
fn push_iter<I: Iterator<Item = Self::Item>>(&mut self, mut iter: I) -> usize
where
Self: Sized,
{
let (left, right) = self.vacant_slices_mut();
let mut count = 0;
for place in left.iter_mut().chain(right.iter_mut()) {
Expand Down Expand Up @@ -128,7 +131,7 @@ pub trait Producer: Observer {
/// To achieve this we read only one contiguous slice at once. So this call may read less than `vacant_len` items in the buffer even if the reader is ready to provide more.
fn read_from<S: Read>(&mut self, reader: &mut S, count: Option<usize>) -> Option<io::Result<usize>>
where
Self: Producer<Item = u8>,
Self: Producer<Item = u8> + Sized,
{
let (left, _) = self.vacant_slices_mut();
let count = cmp::min(count.unwrap_or(left.len()), left.len());
Expand Down Expand Up @@ -188,8 +191,18 @@ where
}

#[inline]
fn push_iter<I: Iterator<Item = Self::Item>>(&mut self, iter: I) -> usize {
self.base_mut().push_iter(iter)
fn push_iter<I: Iterator<Item = Self::Item>>(&mut self, iter: I) -> usize
where
Self: Sized,
{
let mut count = 0;
for item in iter {
if self.try_push(item).is_err() {
break;
}
count += 1;
}
count
}

#[inline]
Expand Down
Loading