Skip to main content

ResultExt

Trait ResultExt 

Source
pub trait ResultExt: Sized {
    type Success;
    type Error: Fail;

    // Required method
    fn chain_inspect_err<ErrorKindT: ChainErrorKind>(
        self,
        map: impl FnOnce(&mut Self::Error) -> ErrorKindT,
    ) -> Result<Self::Success, ErrorKindT::Error>;

    // Provided method
    fn chain_err<ErrorKindT: ChainErrorKind>(
        self,
        map: impl FnOnce() -> ErrorKindT,
    ) -> Result<Self::Success, ErrorKindT::Error> { ... }
}
Expand description

Extension trait which adds the family of .chain_err methods to Result objects.

Required Associated Types§

Required Methods§

Source

fn chain_inspect_err<ErrorKindT: ChainErrorKind>( self, map: impl FnOnce(&mut Self::Error) -> ErrorKindT, ) -> Result<Self::Success, ErrorKindT::Error>

Like chain_err, but the callback is given an opportunity to inspect the original error.

Provided Methods§

Source

fn chain_err<ErrorKindT: ChainErrorKind>( self, map: impl FnOnce() -> ErrorKindT, ) -> Result<Self::Success, ErrorKindT::Error>

Replace the error in a Result with a new error built from map’s ErrorKind output.

The original error is stored as the cause/source of the new one.

Examples found in repository?
examples/main.rs (line 71)
69    fn load(wad: &Path, metadata: &Path) -> Result<()> {
70        // `chain_err` stashes the original error as the `cause` of a new error.
71        let wad_file = File::open(wad).chain_err(|| ErrorKind::WadIo(wad.to_owned()))?;
72
73        let mut metadata_content = String::new();
74
75        // `chain_inspect_err` stashes the original error as the `cause` of the new
76        // error, but it first allows the callback to inspect it.
77        let metadata_file = File::open(metadata)
78            .and_then(|mut file| file.read_to_string(&mut metadata_content))
79            .chain_inspect_err(|_io_error| ErrorKind::MetadataIo(metadata.to_owned()))?;
80
81        validate_metadata(metadata, &metadata_content)?;
82
83        Ok(())
84    }

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<SuccessT, ErrorT: Fail> ResultExt for Result<SuccessT, ErrorT>

Source§

type Success = SuccessT

Source§

type Error = ErrorT

Source§

fn chain_inspect_err<ErrorKindT: ChainErrorKind>( self, chain: impl FnOnce(&mut ErrorT) -> ErrorKindT, ) -> Result<Self::Success, ErrorKindT::Error>

Implementors§