Crates.io | multi_try |
lib.rs | multi_try |
version | 0.3.0 |
source | src |
created_at | 2019-02-23 03:40:37.522018 |
updated_at | 2019-02-23 19:29:07.462722 |
description | Safely combine results |
homepage | |
repository | https://github.com/JoshMcguigan/multi_try |
max_upload_size | |
id | 116676 |
size | 27,247 |
This crate allows you to combine multiple Result
types and return either a
tuple containing all of their results, or a Vec
of any errors which occurred.
It is useful when you want to provide an error message for all errors rather
than simply returning the first error.
Generics are used to support Result<T, E>
for any types of T
and E
. The
Ok
types of the combined results are NOT required to be the same, but all of
the Err
types must be the same.
use multi_try::MultiTry;
struct A {
b: Result<i32, MyErr>,
c: Result<i64, MyErr>,
d: Result<f32, MyErr>,
}
struct ValidatedA {
b: i32,
c: i64,
d: f32,
}
enum MyErr {
FailedB,
FailedC,
FailedD,
}
fn validate(a: A) -> Result<ValidatedA, Vec<MyErr>> {
let (b, c, d) = a.b.and_try(a.c).and_try(a.d)?;
Ok(ValidatedA { b, c, d })
}
Check the tests
directory for additional examples.
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.