Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Play with a boxed type for ExitDisplay #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ rust:
- stable
- beta
- nightly
- 1.26.0 #oldest supported release
- 1.27.0 #oldest supported release

matrix:
fast_finish: true
Expand Down
6 changes: 3 additions & 3 deletions examples/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ extern crate exitfailure;

use exitfailure::ExitDisplay;

fn main() -> Result<(), ExitDisplay<String>> {
fn main() -> Result<(), ExitDisplay> {
Ok(some_fn()?)
}

fn some_fn() -> Result<(), String> {
Err("this is an error message".into())
fn some_fn() -> Result<(), impl std::fmt::Display> {
Err("this is an error message")
}
23 changes: 14 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,26 +86,31 @@ impl<T: Into<failure::Error>> From<T> for ExitFailure {
/// ```rust,should_panic
/// # extern crate exitfailure;
/// # use exitfailure::ExitDisplay;
/// fn main() -> Result<(), ExitDisplay<String>> {
/// fn main() -> Result<(), ExitDisplay> {
/// some_other_fn()?;
/// Ok(some_fn()?)
/// }
///
/// fn some_fn() -> Result<(), String> {
/// Err("some error".into())
/// fn some_fn() -> Result<(), &'static str> {
/// Err("some error")
/// }
///
/// fn some_other_fn() -> Result<(), isize> {
/// Ok(())
/// }
/// ```
pub struct ExitDisplay<E: std::fmt::Display>(E);
pub struct ExitDisplay(Box<dyn std::fmt::Display>);

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

impl<E: std::fmt::Display> From<E> for ExitDisplay<E> {
impl<E: std::fmt::Display + 'static> From<E> for ExitDisplay {
fn from(e: E) -> Self {
ExitDisplay(e)
ExitDisplay(Box::new(e))
}
}

Expand All @@ -127,8 +132,8 @@ mod test {
#[test]
fn test_exitdisplay() {
let mut buffer = String::new();
let error = "some error".to_string();
let exitdisplay: ExitDisplay<String> = error.into();
let error = "some error";
let exitdisplay: ExitDisplay = error.into();
write!(buffer, "{:?}", exitdisplay).unwrap();
assert_eq!(buffer, "some error");
}
Expand Down