java_fn!() { /* proc-macro */ }Expand description
Re-export the proc macros so users only need to depend on this crate. Return a typed Rust function that compiles and runs Java at program runtime.
Like java!, but supports parameters. The parameters declared in the
Java run(P1 p1, P2 p2, ...) method become the Rust function’s parameters.
Arguments are serialised by Rust and piped to the Java process via stdin;
Java reads them with DataInputStream.
Expands to a function value of type fn(P1, P2, ...) -> Result<T, JavaError>.
Call it immediately or store it in a variable.
§Supported parameter types
| Java type | Rust type |
|---|---|
byte | i8 |
short | i16 |
int | i32 |
long | i64 |
float | f32 |
double | f64 |
boolean | bool |
char | char |
String | &str |
Optional<BoxedT> | Option<T> |
Optional<String> | Option<&str> |
§Options
Same javac = "..." / java = "..." key-value pairs as java!.
§Examples
use inline_java::java_fn;
// Single int parameter
let double_it = java_fn! {
static int run(int n) {
return n * 2;
}
};
let result: i32 = double_it(21).unwrap();
assert_eq!(result, 42);
// Multiple parameters including String
let greet = java_fn! {
static String run(String greeting, String target) {
return greeting + ", " + target + "!";
}
};
let msg: String = greet("Hello", "World").unwrap();
assert_eq!(msg, "Hello, World!");