Skip to content

Commit d7ab4e7

Browse files
committed
AllEqualValueError implements std::error::Error
1 parent 7b8f942 commit d7ab4e7

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

src/all_equal_value_err.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1-
/// Value returned for the error case of `Itertools::all_equal_value()`.
1+
#[cfg(doc)]
2+
use crate::Itertools;
3+
#[cfg(feature = "use_std")]
4+
use std::error::Error;
5+
use std::fmt::{Debug, Display, Formatter, Result as FmtResult};
6+
7+
/// Value returned for the error case of [`Itertools::all_equal_value`].
28
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
39
pub struct AllEqualValueError<Item>(pub Option<[Item; 2]>);
10+
11+
impl<Item> Display for AllEqualValueError<Item> {
12+
fn fmt(&self, f: &mut Formatter) -> FmtResult {
13+
match self.0 {
14+
None => {
15+
write!(
16+
f,
17+
"got zero elements when all elements were expected to be equal"
18+
)
19+
}
20+
Some([_, _]) => {
21+
write!(
22+
f,
23+
"got different elements when all elements were expected to be equal"
24+
)
25+
}
26+
}
27+
}
28+
}
29+
30+
#[cfg(feature = "use_std")]
31+
impl<Item> Error for AllEqualValueError<Item> where Item: Debug {}

0 commit comments

Comments
 (0)