Skip to content

Feature Request: single_result #1034

@MasterTemple

Description

@MasterTemple

This is similar to partition_result.
The idea is that instead of a collection of Ok values and a collection of Err values, it would return a single Result.

This is ideal for situations where you want to report all errors (not short-circuiting), and the Ok values only matter if there are no errors.

The implementation is below:

fn single_result<A, B, T, E>(self) -> Result<A, B>
where
    Self: Iterator<Item = Result<T, E>> + Sized,
    A: Default + Extend<T>,
    B: Default + Extend<T>,
{
    let mut is_err = false;
    let (a, b): (A, B) = self.partition_map(|r| match r {
        Ok(v) => Either::Left(v),
        Err(v) => {
            is_err = true;
            Either::Right(v)
        },
    });
    if is_err {
        Err(b)
    } else {
        Ok(a)
    }
}

Thank you for your time and consideration.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions