futures_concurrency/future/race_ok/vec/
error.rs

1#[cfg(all(feature = "alloc", not(feature = "std")))]
2use alloc::vec::Vec;
3
4use core::fmt;
5use core::ops::Deref;
6use core::ops::DerefMut;
7#[cfg(feature = "std")]
8use std::error::Error;
9
10/// A collection of errors.
11#[repr(transparent)]
12pub struct AggregateError<E> {
13    pub(crate) inner: Vec<E>,
14}
15
16impl<E> AggregateError<E> {
17    pub(crate) fn new(inner: Vec<E>) -> Self {
18        Self { inner }
19    }
20}
21
22impl<E: fmt::Display> fmt::Debug for AggregateError<E> {
23    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24        writeln!(f, "{self}:")?;
25
26        for (i, err) in self.inner.iter().enumerate() {
27            writeln!(f, "- Error {}: {err}", i + 1)?;
28        }
29
30        Ok(())
31    }
32}
33
34impl<E: fmt::Display> fmt::Display for AggregateError<E> {
35    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        write!(f, "{} errors occurred", self.inner.len())
37    }
38}
39
40impl<E> Deref for AggregateError<E> {
41    type Target = Vec<E>;
42
43    fn deref(&self) -> &Self::Target {
44        &self.inner
45    }
46}
47
48impl<E> DerefMut for AggregateError<E> {
49    fn deref_mut(&mut self) -> &mut Self::Target {
50        &mut self.inner
51    }
52}
53
54#[cfg(feature = "std")]
55impl<E: Error> Error for AggregateError<E> {}