#jni #java #ffi

catch_panic

A helper macro for safe Java-Rust interop that "catches" Rust panics and rethrows them as Java exceptions

1 stable release

1.0.0 Jul 25, 2022

#1990 in Rust patterns

Download history 75/week @ 2024-07-20 3/week @ 2024-07-27 37/week @ 2024-08-03 87/week @ 2024-08-10 19/week @ 2024-08-17 27/week @ 2024-08-24 51/week @ 2024-08-31 42/week @ 2024-09-07 9/week @ 2024-09-14 70/week @ 2024-09-21 29/week @ 2024-09-28 29/week @ 2024-10-05 41/week @ 2024-10-12 36/week @ 2024-10-19 12/week @ 2024-10-26 54/week @ 2024-11-02

151 downloads per month

MIT/Apache

12KB
119 lines

⚾ #[catch_panic]

Crates.io Documentation License

A helper macro for safe Java-Rust interop that "catches" Rust panics and rethrows them as Java exceptions.

Getting Started

Add catch_panic as a dependency to your Cargo.toml:

[dependencies]
catch_panic = "1.0.0"

Usage

Attach #[catch_panic] to a JNI callback to have panics converted into RuntimeExceptions:

use jni::JNIEnv;
use catch_panic::catch_panic;

#[no_mangle]
#[catch_panic]
pub extern "C" fn Java_com_example_Example_panic(_env: JNIEnv) {
    panic!("everything is not fine");
}

See the docs for macro options and more information.

License

This crate is dual-licensed under either:

at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

The code in this crate is a derivative of code from HermitSocialClub/HermitRepo, specifically the files ProjectTomato/tomato_macros/src/lib.rs and ProjectTomato/tomato/src/util.rs. All authors of these two files have agreed to relicense the original code under the above license.

Dependencies

~2–13MB
~104K SLoC