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
25 changes: 22 additions & 3 deletions src/exactly_one_err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::error::Error;
use std::fmt::{Debug, Display, Formatter, Result as FmtResult};

use std::iter::ExactSizeIterator;
use std::iter::{ExactSizeIterator, Fuse};

use either::Either;

Expand All @@ -22,7 +22,7 @@ where
I: Iterator,
{
first_two: Option<Either<[I::Item; 2], I::Item>>,
inner: I,
inner: Fuse<I>,
}

impl<I> ExactlyOneError<I>
Expand All @@ -31,7 +31,10 @@ where
{
/// Creates a new `ExactlyOneErr` iterator.
pub(crate) fn new(first_two: Option<Either<[I::Item; 2], I::Item>>, inner: I) -> Self {
Self { first_two, inner }
Self {
first_two,
inner: inner.fuse(),
}
}

fn additional_len(&self) -> usize {
Expand Down Expand Up @@ -89,6 +92,22 @@ where

impl<I> ExactSizeIterator for ExactlyOneError<I> where I: ExactSizeIterator {}

impl<I: DoubleEndedIterator> DoubleEndedIterator for ExactlyOneError<I> {
fn next_back(&mut self) -> Option<Self::Item> {
if let Some(next) = self.inner.next_back() {
return Some(next);
};
match self.first_two.take() {
Some(Either::Left([first, second])) => {
self.first_two = Some(Either::Right(first));
Some(second)
}
Some(Either::Right(second)) => Some(second),
None => None,
}
}
}

impl<I> Display for ExactlyOneError<I>
where
I: Iterator,
Expand Down
10 changes: 10 additions & 0 deletions tests/quick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1400,6 +1400,16 @@ quickcheck! {
}
}

quickcheck! {
fn exactly_one_double_ended(a: Vec<i32>) -> TestResult {
let ret = a.iter().copied().exactly_one();
match a.len() {
1 => TestResult::passed(),
_ => TestResult::from_bool(a.iter().rev().copied().eq(ret.unwrap_err().rev())),
}
}
}

quickcheck! {
fn at_most_one_i32(a: Vec<i32>) -> TestResult {
let ret = a.iter().cloned().at_most_one();
Expand Down
Loading