Optional trait impls

#1

I want to contribute a new type and Iterator impl to an existing, published library. The iterator does io and is fallible.

I have implemented Iterator for MyType with type Item = std::io::Result<Vec<u8>>.

However, I don’t think Iterator<Item=Result<..>> is a good pattern. Too many things can go wrong, such as accidentally counting Errs as Oks, and so forth. For these reasons, in my own code I prefer to use fallible-iterator for iterators that can fail.

But this code is a contribution to another project which only judiciously adds new crate dependencies.

I think the Rust way of handling this is conditional compilation or “features”. What I think I’d like to do is implement FallibleIterator and IntoFallibleIterator for MyType if the fallible-iterator crate is already a dep of the consuming crate, or if they’ve opted in to the fallible-iterator feature. That way we can get the best of both worlds. Is there a way to do it so that they automatically get FallibleIterator if they already have fallible-iterator in their project, without requiring that they opt-in to the feature in Cargo.toml?

Or do you think this is a waste of time and I should just content myself with implementing Iterator?

#2

I’d expect that people would just call https://docs.rs/fallible-iterator/0.2.1/fallible_iterator/fn.convert.html and be fine with that

1 Like
#3

That was in the back of my mind as well. But what would you do for traits that don’t have a blanket impl like that?