Skip to content

Commit 3017f2a

Browse files
committed
wip: trial a boxed exitdisplay instead of exitdisplay<T>
1 parent 22eb199 commit 3017f2a

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

examples/display.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ extern crate exitfailure;
22

33
use exitfailure::ExitDisplay;
44

5-
fn main() -> Result<(), ExitDisplay<String>> {
5+
fn main() -> Result<(), ExitDisplay> {
66
Ok(some_fn()?)
77
}
88

9-
fn some_fn() -> Result<(), String> {
10-
Err("this is an error message".into())
9+
fn some_fn() -> Result<(), impl std::fmt::Display> {
10+
Err("this is an error message")
1111
}

src/lib.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,26 +86,31 @@ impl<T: Into<failure::Error>> From<T> for ExitFailure {
8686
/// ```rust,should_panic
8787
/// # extern crate exitfailure;
8888
/// # use exitfailure::ExitDisplay;
89-
/// fn main() -> Result<(), ExitDisplay<String>> {
89+
/// fn main() -> Result<(), ExitDisplay> {
90+
/// some_other_fn()?;
9091
/// Ok(some_fn()?)
9192
/// }
9293
///
93-
/// fn some_fn() -> Result<(), String> {
94-
/// Err("some error".into())
94+
/// fn some_fn() -> Result<(), &'static str> {
95+
/// Err("some error")
96+
/// }
97+
///
98+
/// fn some_other_fn() -> Result<(), isize> {
99+
/// Ok(())
95100
/// }
96101
/// ```
97-
pub struct ExitDisplay<E: std::fmt::Display>(E);
102+
pub struct ExitDisplay(Box<dyn std::fmt::Display>);
98103

99104
/// Prints the underlying error type, using `Display` and not `Debug`.
100-
impl<E: std::fmt::Display> std::fmt::Debug for ExitDisplay<E> {
105+
impl std::fmt::Debug for ExitDisplay {
101106
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
102107
write!(f, "{}", self.0)
103108
}
104109
}
105110

106-
impl<E: std::fmt::Display> From<E> for ExitDisplay<E> {
111+
impl<E: std::fmt::Display + 'static> From<E> for ExitDisplay {
107112
fn from(e: E) -> Self {
108-
ExitDisplay(e)
113+
ExitDisplay(Box::new(e))
109114
}
110115
}
111116

@@ -127,8 +132,8 @@ mod test {
127132
#[test]
128133
fn test_exitdisplay() {
129134
let mut buffer = String::new();
130-
let error = "some error".to_string();
131-
let exitdisplay: ExitDisplay<String> = error.into();
135+
let error = "some error";
136+
let exitdisplay: ExitDisplay = error.into();
132137
write!(buffer, "{:?}", exitdisplay).unwrap();
133138
assert_eq!(buffer, "some error");
134139
}

0 commit comments

Comments
 (0)