I just had a crazy idea that might be RFC material, but I wanted feedback from the community first.
There are, in the standard lib and in other crates, a number of traits with the following structure:
pub trait MyTrait {
type Error: std::error::Error;
fn my_method(&self) -> Result<Foo, Self::Error>;
}
Some implementations of this trait will never fail when calling my_method
. Those are encouraged to use Infallible
or the unstable never
for MyTrait::Error
.
Still, when I call my_method
on a value which I know can not fail, I have to unwrap
the result, which is verbose and annoying.
let bar = Bar::new(); // Bar: MyTrait<Error=Infallible>
bar.my_method().unwrap().foo_method();
Here is the idea: Result<T, Infallible>
and Result<T, !>
could implement Deref<Target=T>
and DerefMut<Target=T>
. So the code above could be written more simply:
bar.my_method().foo_method();
What do you think?