evobench_tools/utillib/
tuple_transpose.rs

1//! Convert tuples of `Result` values into a `Result` of the `Ok`
2//! values so that errors can be propagated easily. (There is also
3//! `tuple-transpose` crate offering the same, but it might not stay
4//! around and has no docs.)
5
6pub trait TupleTranspose {
7    type Output;
8    fn transpose(self) -> Self::Output;
9}
10
11impl<V1, E> TupleTranspose for Result<V1, E> {
12    type Output = Result<V1, E>;
13
14    fn transpose(self) -> Self::Output {
15        self
16    }
17}
18
19impl<V1, V2, E> TupleTranspose for (Result<V1, E>, Result<V2, E>) {
20    type Output = Result<(V1, V2), E>;
21
22    fn transpose(self) -> Self::Output {
23        Ok((self.0?, self.1?))
24    }
25}
26
27impl<V1, V2, V3, E> TupleTranspose for (Result<V1, E>, Result<V2, E>, Result<V3, E>) {
28    type Output = Result<(V1, V2, V3), E>;
29
30    fn transpose(self) -> Self::Output {
31        Ok((self.0?, self.1?, self.2?))
32    }
33}
34
35impl<V1, V2, V3, V4, E> TupleTranspose
36    for (Result<V1, E>, Result<V2, E>, Result<V3, E>, Result<V4, E>)
37{
38    type Output = Result<(V1, V2, V3, V4), E>;
39
40    fn transpose(self) -> Self::Output {
41        Ok((self.0?, self.1?, self.2?, self.3?))
42    }
43}
44
45impl<V1, V2, V3, V4, V5, E> TupleTranspose
46    for (
47        Result<V1, E>,
48        Result<V2, E>,
49        Result<V3, E>,
50        Result<V4, E>,
51        Result<V5, E>,
52    )
53{
54    type Output = Result<(V1, V2, V3, V4, V5), E>;
55
56    fn transpose(self) -> Self::Output {
57        Ok((self.0?, self.1?, self.2?, self.3?, self.4?))
58    }
59}