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
31 changes: 31 additions & 0 deletions src/all_equal_value_err.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#[cfg(doc)]
use crate::Itertools;
#[cfg(feature = "use_std")]
use std::error::Error;
use std::fmt::{Debug, Display, Formatter, Result as FmtResult};

/// Value returned for the error case of [`Itertools::all_equal_value`].
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct AllEqualValueError<Item>(pub Option<[Item; 2]>);

impl<Item> Display for AllEqualValueError<Item> {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
match self.0 {
None => {
write!(
f,
"got zero elements when all elements were expected to be equal"
)
}
Some([_, _]) => {
write!(
f,
"got different elements when all elements were expected to be equal"
)
}
}
}
}

#[cfg(feature = "use_std")]
impl<Item> Error for AllEqualValueError<Item> where Item: Debug {}
14 changes: 8 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ pub mod structs {
FilterOk, Interleave, InterleaveShortest, MapInto, MapOk, Positions, Product, PutBack,
TakeWhileRef, TupleCombinations, Update, WhileSome,
};
pub use crate::all_equal_value_err::AllEqualValueError;
#[cfg(feature = "use_alloc")]
pub use crate::combinations::{ArrayCombinations, Combinations};
#[cfg(feature = "use_alloc")]
Expand Down Expand Up @@ -180,6 +181,7 @@ pub use crate::either_or_both::EitherOrBoth;
pub mod free;
#[doc(inline)]
pub use crate::free::*;
mod all_equal_value_err;
#[cfg(feature = "use_alloc")]
mod combinations;
#[cfg(feature = "use_alloc")]
Expand Down Expand Up @@ -2268,27 +2270,27 @@ pub trait Itertools: Iterator {
/// two non-equal elements found.
///
/// ```
/// use itertools::Itertools;
/// use itertools::{Itertools, AllEqualValueError};
///
/// let data = vec![1, 1, 1, 2, 2, 3, 3, 3, 4, 5, 5];
/// assert_eq!(data.iter().all_equal_value(), Err(Some((&1, &2))));
/// assert_eq!(data.iter().all_equal_value(), Err(AllEqualValueError(Some([&1, &2]))));
/// assert_eq!(data[0..3].iter().all_equal_value(), Ok(&1));
/// assert_eq!(data[3..5].iter().all_equal_value(), Ok(&2));
/// assert_eq!(data[5..8].iter().all_equal_value(), Ok(&3));
///
/// let data: Option<usize> = None;
/// assert_eq!(data.into_iter().all_equal_value(), Err(None));
/// assert_eq!(data.into_iter().all_equal_value(), Err(AllEqualValueError(None)));
/// ```
#[allow(clippy::type_complexity)]
fn all_equal_value(&mut self) -> Result<Self::Item, Option<(Self::Item, Self::Item)>>
fn all_equal_value(&mut self) -> Result<Self::Item, AllEqualValueError<Self::Item>>
where
Self: Sized,
Self::Item: PartialEq,
{
let first = self.next().ok_or(None)?;
let first = self.next().ok_or(AllEqualValueError(None))?;
let other = self.find(|x| x != &first);
if let Some(other) = other {
Err(Some((first, other)))
Err(AllEqualValueError(Some([first, other])))
} else {
Ok(first)
}
Expand Down
10 changes: 7 additions & 3 deletions tests/test_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::it::multipeek;
use crate::it::multizip;
use crate::it::peek_nth;
use crate::it::repeat_n;
use crate::it::AllEqualValueError;
use crate::it::ExactlyOneError;
use crate::it::FoldWhile;
use crate::it::Itertools;
Expand Down Expand Up @@ -282,14 +283,17 @@ fn all_equal() {

#[test]
fn all_equal_value() {
assert_eq!("".chars().all_equal_value(), Err(None));
assert_eq!("".chars().all_equal_value(), Err(AllEqualValueError(None)));
assert_eq!("A".chars().all_equal_value(), Ok('A'));
assert_eq!("AABBCCC".chars().all_equal_value(), Err(Some(('A', 'B'))));
assert_eq!(
"AABBCCC".chars().all_equal_value(),
Err(AllEqualValueError(Some(['A', 'B'])))
);
assert_eq!("AAAAAAA".chars().all_equal_value(), Ok('A'));
{
let mut it = [1, 2, 3].iter().copied();
let result = it.all_equal_value();
assert_eq!(result, Err(Some((1, 2))));
assert_eq!(result, Err(AllEqualValueError(Some([1, 2]))));
let remaining = it.next();
assert_eq!(remaining, Some(3));
assert!(it.next().is_none());
Expand Down
Loading