1 | //! Definitions used in derived implementations of [`core::ops`] traits. |
2 | |
3 | use core::fmt; |
4 | |
5 | /// Error returned by the derived implementations when an arithmetic or logic |
6 | /// operation is invoked on a unit-like variant of an enum. |
7 | #[derive (Clone, Copy, Debug)] |
8 | pub struct UnitError { |
9 | operation_name: &'static str, |
10 | } |
11 | |
12 | impl UnitError { |
13 | #[doc (hidden)] |
14 | #[must_use ] |
15 | #[inline ] |
16 | pub const fn new(operation_name: &'static str) -> Self { |
17 | Self { operation_name } |
18 | } |
19 | } |
20 | |
21 | impl fmt::Display for UnitError { |
22 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
23 | write!(f, "Cannot {}() unit variants" , self.operation_name) |
24 | } |
25 | } |
26 | |
27 | #[cfg (feature = "std" )] |
28 | impl std::error::Error for UnitError {} |
29 | |