futures_concurrency/future/race_ok/array/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use core::fmt;
use core::ops::{Deref, DerefMut};
#[cfg(feature = "std")]
use std::error::Error;

/// A collection of errors.
#[repr(transparent)]
pub struct AggregateError<E, const N: usize> {
    inner: [E; N],
}

impl<E, const N: usize> AggregateError<E, N> {
    pub(super) fn new(inner: [E; N]) -> Self {
        Self { inner }
    }
}

impl<E: fmt::Display, const N: usize> fmt::Debug for AggregateError<E, N> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "{self}:")?;

        for (i, err) in self.inner.iter().enumerate() {
            writeln!(f, "- Error {}: {err}", i + 1)?;
        }

        Ok(())
    }
}

impl<E: fmt::Display, const N: usize> fmt::Display for AggregateError<E, N> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} errors occured", self.inner.len())
    }
}

impl<E, const N: usize> Deref for AggregateError<E, N> {
    type Target = [E; N];

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl<E, const N: usize> DerefMut for AggregateError<E, N> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.inner
    }
}

#[cfg(feature = "std")]
impl<E: Error, const N: usize> std::error::Error for AggregateError<E, N> {}