[][src]Macro simple_error::try_with

macro_rules! try_with {
    ($expr: expr, $str: expr) => { ... };
    ($expr: expr, $fmt:expr, $($arg:tt)+) => { ... };
}

Helper macro for unwrapping Result values while returning early with a newly constructed SimpleError if the value of the expression is Err. Can only be used in functions that return Result<_, SimpleError>.

Examples

use self::simple_error::SimpleError;
use std::error::Error;

fn try_block(result: Result<(), SimpleError>, s: &str) -> Result<(), SimpleError> {
    Ok(try_with!(result, s))
}

// Above is equivalent to below.

fn try_block_equivalent(result: Result<(), SimpleError>, s: &str) -> Result<(), SimpleError> {
    match result {
        Ok(v) => Ok(v),
        Err(e) => {
            return Err(SimpleError::with(s, e));
        },
    }
}

// Use a format string

fn try_block_format(result: Result<(), SimpleError>, s: &str) -> Result<(), SimpleError> {
    Ok(try_with!(result, "with {}", s))
}

// Use a format string to a boxed error

fn try_block_format_to_box_error(result: Result<(), SimpleError>, s: &str) -> Result<(), Box<Error>> {
    Ok(try_with!(result, "with {}", s))
}