Skip to content

Commit

Permalink
Merge pull request #108 from Alastair-smith2/race_ok_array_error_format
Browse files Browse the repository at this point in the history
Improve RaceOk error format for array impl
  • Loading branch information
matheus-consoli authored Nov 28, 2022
2 parents 63a1088 + 3a49ce5 commit 058da68
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/future/race_ok/array/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use core::fmt;
use core::ops::{Deref, DerefMut};
use std::error::Error;

/// A collection of errors.
#[repr(transparent)]
Expand All @@ -13,19 +14,26 @@ impl<E, const N: usize> AggregateError<E, N> {
}
}

impl<E: fmt::Debug, const N: usize> fmt::Debug for AggregateError<E, N> {
impl<E: Error, const N: usize> fmt::Debug for AggregateError<E, N> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut list = f.debug_list();
for err in self.inner.as_ref() {
list.entry(err);
writeln!(f, "{self}:")?;

for (i, err) in self.inner.iter().enumerate() {
writeln!(f, "- Error {}: {err}", i + 1)?;
let mut source = err.source();
while let Some(err) = source {
writeln!(f, " ↳ Caused by: {err}")?;
source = err.source();
}
}
list.finish()

Ok(())
}
}

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

Expand All @@ -43,4 +51,4 @@ impl<E, const N: usize> DerefMut for AggregateError<E, N> {
}
}

impl<E: fmt::Debug, const N: usize> std::error::Error for AggregateError<E, N> {}
impl<E: Error, const N: usize> std::error::Error for AggregateError<E, N> {}

0 comments on commit 058da68

Please sign in to comment.