Module robusta_jni::convert [−][src]
Expand description
Conversion facilities. This module provides two trait families: FromJavaValue/IntoJavaValue (infallible conversions) and TryFromJavaValue/TryIntoJavaValue (fallible conversions), similar to the ones found in the standard library.
The call_type
attribute controls which of the two conversion families is selected during code generation.
call_type
is a per-function attribute.
Specific parameters that can be given to call_type
can be found in the module documentation relative to the trait family (safe module for fallible conversions and unchecked module for infallible conversions)
If the call_type
attribute is omitted, the fallible conversion trait family is chosen.
Example usage:
use robusta_jni::bridge; #[bridge] mod jni { #[package(com.example.robusta)] struct HelloWorld; impl HelloWorld { #[call_type(unchecked)] pub extern "jni" fn special(mut input1: Vec<i32>, input2: i32) -> Vec<String> { input1.push(input2); input1.iter().map(ToString::to_string).collect() } #[call_type(safe(exception_class = "java.lang.IllegalArgumentException", message = "invalid value"))] pub extern "jni" fn bar(foo: i32) -> ::robusta_jni::jni::errors::Result<i32> { Ok(foo) } } }
Raising exceptions from native code
If you want to have the option of throwing a Java exception from native code (conversion errors aside), you can
annotate your function signature with a jni::errors::Result<T>
return type.
When used with #[call_type(safe)]
, if an Err
is returned a Java exception is thrown (the one specified in the call_type
attribute,
or java.lang.RuntimeException
if omitted).
Re-exports
Modules
Structs
Traits
A trait for types that are ffi-safe to use with JNI. It is implemented for primitives, JObject and jobject. Users that want automatic conversion should instead implement FromJavaValue, IntoJavaValue and/or TryFromJavaValue, TryIntoJavaValue
This trait provides type signatures for types. It is necessary to support conversions to/from Java types.