| 1 |
//! Definitions used in derived implementations of [`core::convert`] traits. |
| 2 |
|
| 3 |
#[cfg(feature = "try_from")] |
| 4 |
pub use self::try_from::TryFromReprError; |
| 5 |
#[cfg(feature = "try_into")] |
| 6 |
pub use self::try_into::TryIntoError; |
| 7 |
|
| 8 |
#[cfg(feature = "try_from")] |
| 9 |
mod try_from { |
| 10 |
use core::{error::Error, fmt}; |
| 11 |
|
| 12 |
/// Error returned by the derived [`TryFrom`] implementation on enums to |
| 13 |
/// convert from their repr. |
| 14 |
/// |
| 15 |
/// [`TryFrom`]: macro@crate::TryFrom |
| 16 |
#[derive(Clone, Copy, Debug)] |
| 17 |
pub struct TryFromReprError<T> { |
| 18 |
/// Original input value which failed to convert via the derived |
| 19 |
/// [`TryFrom`] implementation. |
| 20 |
/// |
| 21 |
/// [`TryFrom`]: macro@crate::TryFrom |
| 22 |
pub input: T, |
| 23 |
} |
| 24 |
|
| 25 |
impl<T> TryFromReprError<T> { |
| 26 |
#[doc(hidden)] |
| 27 |
#[must_use] |
| 28 |
#[inline] |
| 29 |
pub const fn new(input: T) -> Self { |
| 30 |
Self { input } |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
// `T`, as a discriminant, should only be an integer type, and therefore be `Debug`. |
| 35 |
impl<T: fmt::Debug> fmt::Display for TryFromReprError<T> { |
| 36 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 37 |
write!( |
| 38 |
f, |
| 39 |
"`{:?}` does not correspond to a unit variant", |
| 40 |
self.input |
| 41 |
) |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
// `T` should only be an integer type and therefore be `Debug`. |
| 46 |
impl<T: fmt::Debug> Error for TryFromReprError<T> {} |
| 47 |
} |
| 48 |
|
| 49 |
#[cfg(feature = "try_into")] |
| 50 |
mod try_into { |
| 51 |
use core::{error::Error, fmt}; |
| 52 |
|
| 53 |
/// Error returned by the derived [`TryInto`] implementation. |
| 54 |
/// |
| 55 |
/// [`TryInto`]: macro@crate::TryInto |
| 56 |
#[derive(Clone, Copy, Debug)] |
| 57 |
pub struct TryIntoError<T> { |
| 58 |
/// Original input value which failed to convert via the derived |
| 59 |
/// [`TryInto`] implementation. |
| 60 |
/// |
| 61 |
/// [`TryInto`]: macro@crate::TryInto |
| 62 |
pub input: T, |
| 63 |
variant_names: &'static str, |
| 64 |
output_type: &'static str, |
| 65 |
} |
| 66 |
|
| 67 |
impl<T> TryIntoError<T> { |
| 68 |
#[doc(hidden)] |
| 69 |
#[must_use] |
| 70 |
#[inline] |
| 71 |
pub const fn new( |
| 72 |
input: T, |
| 73 |
variant_names: &'static str, |
| 74 |
output_type: &'static str, |
| 75 |
) -> Self { |
| 76 |
Self { |
| 77 |
input, |
| 78 |
variant_names, |
| 79 |
output_type, |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
impl<T> fmt::Display for TryIntoError<T> { |
| 85 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 86 |
write!( |
| 87 |
f, |
| 88 |
"Only {} can be converted to {}", |
| 89 |
self.variant_names, self.output_type, |
| 90 |
) |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
impl<T: fmt::Debug> Error for TryIntoError<T> {} |
| 95 |
} |