derive_more
derive_more copied to clipboard
Make TryInto use better error type
Rather than returning a string as an error, might it not be better to use a struct that stores the type and variant names as strings, which implements Display to show a full error message? That struct can then easily implement std::error::Error too.
Perhaps this can be enabled as an optional feature, as suggested in https://github.com/JelteF/derive_more/pull/130? Although it kinda feels like this should be the default behaviour
I ran into this today as well, would love it if it returned a proper error instead.
I think you are both right and what we want instead is to have a generic error type provided by derive_more. Something like this (untested code):
struct TryIntoError<T> {
pub input: T
variant_names: &str
output_type: &str
}
impl fmt::Display for Point {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Only {} can be converted to {}", self.variant_names, self.output_type)
}
}
Added 1.0 milestone, since i agree that this should be the default. But changing the default would be a breaking change.