Debian Codegraph @

third_party/rust/derive_more/src/convert.rs

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