1 unstable release

0.1.0-rc1 Dec 22, 2022

#7 in #customize

21 downloads per month
Used in custom-try

Zlib OR MIT OR Apache-2.0

6KB
113 lines

::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
}

lib.rs:

Crate not intended for direct use. Use https:://docs.rs/custom-try instead.

Dependencies

~1.5MB
~34K SLoC