Skip to content

Commit

Permalink
Implements ErrorDisplay on 'dyn Error'
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimaweapon committed May 18, 2024
1 parent 6b87728 commit 3926e0c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "erdp"
description = "Crate for display an error and its nested errors"
version = "0.1.0"
version = "0.1.1"
license = "MIT"
repository = "https://github.com/ultimaweapon/erdp"
edition = "2021"
Expand Down
13 changes: 13 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ impl<T: Error> ErrorDisplay for T {
}
}

impl ErrorDisplay for dyn Error {
fn display(&self) -> Display {
Display(self)
}
}

/// Implementation of [`std::fmt::Display`] for display an error and its nested errors.
pub struct Display<'a>(&'a dyn Error);

Expand Down Expand Up @@ -56,6 +62,13 @@ mod tests {
assert_eq!(e.display().to_string(), "nested error -> entity not found");
}

#[test]
fn trait_object() {
let e: Box<dyn Error> = Box::new(TestError::Single);

assert_eq!(e.display().to_string(), "an error without nested errors");
}

#[derive(Debug, Error)]
enum TestError {
#[error("an error without nested errors")]
Expand Down

0 comments on commit 3926e0c

Please sign in to comment.