[][src]Attribute Macro some_error::some_error

#[some_error]

A Macro for allowing Anonymous Sum Types in the error of the returned result. The function this is attached to must return a type in the form of Result<T, E1 + E2 + ...>.

Example:

use std::io;
use some_error::*;
 
#[derive(Debug, Clone, Copy)]
struct NotZeroError(u32);
 
#[some_error]
fn my_func() -> Result<(), io::Error + NotZeroError>{
    let x = 3;
    if x != 0 {
        Err(NotZeroError(x))?;
    }
 
    Ok(())
}
 
fn main() {
    match my_func() {
        Ok(_) => {
            println!("Worked ok!");
        }
        Err(my_func::NotZeroError(NotZeroError(x))) => {
            println!("{} is not zero!!", x);
        }
        Err(my_func::io::Error(io_err)) => {
            println!("io error: {:?}", io_err);
        }
    }
}

More Info

  • The type of the anonymous sum type can be referenced via function_name::Error.
  • All variants of the anonymous sum type can be accessed via function_name::<path>
    • For example if you use the return type Result<(), fmt::Error + i32> the variants will be named function_name::fmt::Error and function_name::i32 and can be used as patterns to match against (see the above example)