Skip to content

danielhenrymantilla/custom-try.rs

::custom-try

Macro to customize the behavior of ?

Repository Latest version Documentation MSRV unsafe forbidden License CI

Examples

use ::custom_try::custom_try;

#[repr(transparent)]
pub struct FfiResult {
    pub status_code: ::std::os::raw::c_int,
}

impl FfiResult {
    pub const OK: Self = Self { status_code: 0 };
    pub const ERR: Self = Self { status_code: -1 };
}

macro_rules! unwrap_option {( $option:expr $(,)? ) => (
    match $option {
        Some(thing) => thing,
        None => return FfiResult::ERR,
    }
)}

#[custom_try(unwrap_option!)]
extern "C" fn ffi_function() -> FfiResult {
    let x = the_answer_to_life_the_universe_and_everything()?;
    println!("{}", x);
    FfiResult::OK
}

/// If you only have one case of `?` semantics, you can default to that one
/// using the default `r#try!` macro name.
use unwrap_option as r#try;

#[custom_try]
extern "C" fn ffi_function2() -> FfiResult {
    let x = the_answer_to_life_the_universe_and_everything()?;
    println!("{}", x);
    FfiResult::OK
}

About

Macro to customize the behavior of `?`

Resources

License

Apache-2.0 and 2 other licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Zlib
LICENSE-ZLIB

Stars

Watchers

Forks

Releases

No releases published