Skip to main content

derive_more/
try_unwrap.rs

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