Skip to content

Dispair (Disp-Err) provides a simple wrapper struct that implements `std::error::Error` for any type that implements both `Display` and `Debug`

License

Notifications You must be signed in to change notification settings

Peperworx/dispair

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dispair

Dispair (Disp-Err) is a zero-dependency (other than std) library that provides a simple wrapper struct that implements Error for any type that implements both Debug and Display.

Example

use dispair::Dispair;
use std::error::Error;

#[derive(Debug)]
struct TestStruct;

impl core::fmt::Display for TestStruct {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        core::fmt::Debug::fmt(&self, f)
    }
}

fn main() {
    
    // Wrap `TestStruct` (which doesn't implement `Error`) in `Dispair` and return it.
    let err = Dispair(TestStruct);
    
    // Display just passes through to the wrapped value
    assert_eq!(format!("{}", err), "TestStruct");
    
    // Debug, however, does show `Dispair`.
    assert_eq!(format!("{:?}", err), "Dispair(TestStruct)");

    // We should be able to convert it into a `Box<dyn Error>`
    let err_boxed: Box<dyn Error> = Box::new(err);

    // And this should print nicely
    assert_eq!(err_boxed.to_string(), "TestStruct");
}

About

Dispair (Disp-Err) provides a simple wrapper struct that implements `std::error::Error` for any type that implements both `Display` and `Debug`

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages