derive_more icon indicating copy to clipboard operation
derive_more copied to clipboard

Make TryInto use better error type

Open bravely-beep opened this issue 4 years ago • 1 comments

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

bravely-beep avatar Oct 06 '21 14:10 bravely-beep

I ran into this today as well, would love it if it returned a proper error instead.

orlp avatar Dec 24 '21 11:12 orlp

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)
    }
}

JelteF avatar Oct 15 '22 23:10 JelteF

Added 1.0 milestone, since i agree that this should be the default. But changing the default would be a breaking change.

JelteF avatar Oct 15 '22 23:10 JelteF