pub trait CombineErrors {
type Ok;
type Error;
// Required method
fn combine_errors(self) -> Result<Self::Ok, ErrorStream<Self::Error>>;
}Expand description
A trait for converting a tuple of Result<_, ErrorStream<_>>s
into a Result of a tuple or combined ErrorStream.
Required Associated Types§
Required Methods§
Sourcefn combine_errors(self) -> Result<Self::Ok, ErrorStream<Self::Error>>
fn combine_errors(self) -> Result<Self::Ok, ErrorStream<Self::Error>>
Combine errors from multiple places into one.
This can be thought of as a kind of parallel ?.
If your goal is to show the user as many errors as possible, you should call this as late as possible, on as wide a tuple as you can.
Example usage:
use spacetimedb_data_structures::error_stream::{ErrorStream, CombineErrors};
struct MyError { cause: String };
fn age() -> Result<i32, ErrorStream<MyError>> {
//...
}
fn name() -> Result<String, ErrorStream<MyError>> {
// ...
}
fn likes_dogs() -> Result<bool, ErrorStream<MyError>> {
// ...
}
fn description() -> Result<String, ErrorStream<MyError>> {
// A typical usage of the API:
// Collect multiple `Result`s in parallel, only using
// `.combine_errors()?` once no more progress can be made.
let (age, name, likes_dogs) =
(age(), name(), likes_dogs()).combine_errors()?;
Ok(format!(
"{} is {} years old and {}",
name,
age,
if likes_dogs { "likes dogs" } else { "does not like dogs" }
))
}