derive_more/
try_unwrap.rs

1/// Error returned by the derived [`TryUnwrap`] implementation.
2///
3/// [`TryUnwrap`]: macro@crate::TryUnwrap
4#[derive(Clone, Copy, Debug, PartialEq)]
5pub struct TryUnwrapError<T> {
6    /// Original input value which failed to convert via the derived
7    /// [`TryUnwrap`] implementation.
8    ///
9    /// [`TryUnwrap`]: macro@crate::TryUnwrap
10    pub input: T,
11    enum_name: &'static str,
12    variant_name: &'static str,
13    func_name: &'static str,
14}
15
16impl<T> TryUnwrapError<T> {
17    #[doc(hidden)]
18    #[must_use]
19    #[inline]
20    pub const fn new(
21        input: T,
22        enum_name: &'static str,
23        variant_name: &'static str,
24        func_name: &'static str,
25    ) -> Self {
26        Self {
27            input,
28            enum_name,
29            variant_name,
30            func_name,
31        }
32    }
33}
34
35impl<T> core::fmt::Display for TryUnwrapError<T> {
36    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
37        write!(
38            f,
39            "Attempt to call `{enum_name}::{func_name}()` on a `{enum_name}::{variant_name}` value",
40            enum_name = self.enum_name,
41            variant_name = self.variant_name,
42            func_name = self.func_name,
43        )
44    }
45}
46
47#[cfg(feature = "std")]
48impl<T: core::fmt::Debug> std::error::Error for TryUnwrapError<T> {}