Implementing ? Try for a custom type

impl<'visit, T, E: AsRef<[u8]>> Try for Result<WriteVisitor<'visit, T>, MemError<'_, E>> {
    type Ok = WriteVisitor<'visit, T>;
    type Error = MemError<'visit, E>;

    fn into_result(self) -> Result<Self::Ok, Self::Error> {
        Ok(self)
    }

    fn from_error(err: Self::Error) -> Self {
        panic!("Invalid operation. Error: {}", err.to_string())
    }

    fn from_ok(v: Self::Ok) -> Self {
        v
    }
}

The compile error herefrom is essentially the idea that you can't implement a trait from outside the owner's crate (in this case, Result<T, E>'s). Is it even possible to implement a custom Try for a custom type?

and in my lib.rs, I have #![feature(try_trait)]

Looks like you just have to create your own Result and Option types

Why are you trying to implement Try for std::result::Result, it already implements Try. If you want a different behavior from the standard Result, then you will either have to new-type it,

struct MyResult(Result<A, B>);

or create your own Result

enum MyResult {
    Ok(A),
    Err(B)
}

And implement Try on that type.


Panicing on from_error is code smell, you shouldn't make ? sugar for panics.

Wow, so months ago, I created a custom Result type (i'm re-using something I did when I first started the language) that only impl'd Error. That's why the ? operator didn't work. Lesson learned: just use Result