All conversions, especially those of TryFrom and Try, should use From/Into. E.g.
pub trait MyTry where Self: Into<Result<<Self as MyTry>::Ok, <Self as MyTry>::Error>>, <Self as MyTry>::Ok: Into<Self> {
type Ok;
type Error;
fn from_error(e: <Self as MyTry>::Error) -> Self;
}
impl<T, E> MyTry for Result<T, E> where E: Into<Self>, T: Into<Self> {
type Ok = T;
type Error = E;
fn from_error(e: E) -> Self {
Err(e)
}
}
#[derive(Eq, PartialEq)]
struct Foo;
// this should be impl<T> From<T> for Result<T, E>
impl<E> Into<Result<Foo, E>> for Foo {
fn into(self) -> Result<Foo, E> {
Ok(self)
}
}
fn main() {
let o: Result<Foo, Foo> = Foo.into();
let e: Result<Foo, Foo> = MyTry::from_error(Foo);
assert!(o != e);
}
(note that from_error is, sadly, still necessary. but also note that we don’t need into_result or from_ok, as those can be done with From/Into! this provides a more generic stdlib, so using it is easier! also note that we automatically get a TryFrom - From for Result<T, E> is equivalent to but better than TryFrom for T!)