CollectAllErrors

Trait CollectAllErrors 

Source
pub trait CollectAllErrors {
    type Item;
    type Error;

    // Required method
    fn collect_all_errors<C: FromIterator<Self::Item>>(
        self,
    ) -> Result<C, ErrorStream<Self::Error>>;
}
Expand description

A trait for collecting errors from an iterator of results, returning all errors if anything failed.

Required Associated Types§

Source

type Item

The item type we are aggregating.

Source

type Error

The error type we are aggregating.

Required Methods§

Source

fn collect_all_errors<C: FromIterator<Self::Item>>( self, ) -> Result<C, ErrorStream<Self::Error>>

Collect errors from an iterator of results into a single error stream. If all results are Ok, returns the collected values. Otherwise, combine all errors into a single error stream, and return it.

You CANNOT use the standard library function Result<T, ErrorStream<E>>::collect() for this, as it will return the FIRST error it encounters, rather than collecting all errors!

The collection can be anything that implements FromIterator.

Example usage:

use spacetimedb_data_structures::error_stream::{
    ErrorStream,
    CollectAllErrors
};
use std::collections::HashSet;

enum MyError { /* ... */ }

fn operation(
    data: String,
    checksum: u32
) -> Result<i32, ErrorStream<MyError>> {
    /* ... */
}

fn many_operations(
    data: Vec<(String, u32)>
) -> Result<HashSet<i32>, ErrorStream<MyError>> {
    data
        .into_iter()
        .map(|(data, checksum)| operation(data, checksum))
        .collect_all_errors::<HashSet<_>>()
}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T, E, I: Iterator<Item = Result<T, ErrorStream<E>>>> CollectAllErrors for I

Source§

type Item = T

Source§

type Error = E