failure icon indicating copy to clipboard operation
failure copied to clipboard

iter_causes is only implemented on trait objects

Open fitzgen opened this issue 7 years ago • 3 comments

This means that one has to either use UFCS or casting into a trait object to actually use the method:

let e: failure::Error = get_the_error_from_somewhere();

// Have to use UFCS (to implicitly create a trait object):
for c in Fail::iter_causes(&e) {
    // ...
}

// or explicitly turn the error into a trait object:
for c in (&e as &Fail).iter_causes() {
    // ...
}

// but I really just want to do this:
for c in e.iter_causes() {
    // ...
}

iter_causes should also be a provided method for all implementations of the trait:

trait Fail {
    // ...

    fn iter_causes(&self) -> Causes {
        // the default implementation here...
    }
}

Note that adding an iter_causes method to failure::Error is sufficient to fix the first example, and let it use iter_causes directly, but it won't fix the issue for any other type that implements failure::Fail.

fitzgen avatar Aug 24 '18 18:08 fitzgen

Ran into needing to use UFCS in example usage of #233 (and my #244). Thanks for the explanation! I hadn't figured out why that was necessary. In #244 I added a module helper function to work around it. Would it be appropriate to review the reasoning for all the impl Fail methods vs implementing the same in trait Fail? I've actually never seen this usage of impl <TraitSymbol> before!?!

dekellum avatar Aug 24 '18 18:08 dekellum

@fitzgen we removed that because it caused too many issues: https://github.com/rust-lang-nursery/failure/issues/211

mitsuhiko avatar Aug 28 '18 19:08 mitsuhiko

@mitsuhiko there is a workaround to #211 using an additional trait: https://play.rust-lang.org/?gist=57cfbdb5361482707ed70246d5156a65&version=nightly&mode=debug&edition=2018

Diggsey avatar Oct 07 '18 16:10 Diggsey