I have a tuple of Results (where the error type in both cases have From traits for the Result of my current function) and I'd like to convert it into a Result of tuple instead.
I'm currently doing this ugly thing...
let (a_, b_) = thing_that_produces_the_tuples();
let a = a_?;
let b = b_?;
and I'm hoping for something more like
let (a, b) = thing_that_produces_the_tuples().magical_incantation();
In Haskell or Scala I'd call this (roughly) a zip operation, and I'd find it by doing a Hoogle search for the type signature I expect. What is the best way to search for these sorts of functions in Rust in general?
Implementing Try for the tuple (impl Try, impl Try, ...) up to some reasonable length seems like it might make a good addition to the standard library at some point. That would make this reduce to
// NB: Doesn't currently work
let (left, right) = try_something()?;
The biggest problem with implementing the Result transpose operation is the resulting error variant: For a two-tuple, you need a type that can represent the first, second, or both components being Err with potentially differing payloads.
Hmm, would it be able to construct a Residual across arbitrary items? Seems like it would be a double-conversion issue, but I'm not too familiar with that machinery.
I'm not too familiar with it either; making a fully-generic impl might be asking too much of the type system. But it feels like you should be able to write impls for tuples of solely Result<...> or Option<...> without too much difficulty-- You'd just need to use something like an Either enum as the error type which will then dispatch to the appropriate Into conversion.
Edit: After briefly playing around with this, it's significantly less trivial than I thought due to conflicts with the blanket From/Into implementations and orphan rules. It still feels like it should be possible, though.
I managed to write a proof-of-concept, but it runs into a couple of issues:
It's impossible to write the requisite FromResidual implementation because the error type is throwing away information: You'd need some way to construct a non-error value for each of the elements that didn't cause the error to be thrown. This doesn't cause any problems if you're just using it to convert into something like Result, though-- You just can't use the tuple as the final destination of a ? expression.
Orphan rules prevent the blanket foreign FromResidual implementation that you'd like to have. Instead, you need separate identical implementations for each destination Try type.