Skip to main content

Crate inline_java_macros

Crate inline_java_macros 

Source
Expand description

Proc-macro implementation for inline_java.

Provides three proc macros for embedding Java in Rust:

MacroWhen it runs
java!program runtime
java_fn!program runtime
ct_java!compile time

All macros require the user to write a static <T> run(...) method where T is one of: byte, short, int, long, float, double, boolean, char, String, T[], List<T>, or Optional<T> — including arbitrarily nested types like List<List<Integer>>, Optional<List<String>>, Optional<List<Optional<Integer[]>>>, etc.

§Wire format (Java → Rust, stdout)

The macro generates a main() that binary-serialises run()’s return value to stdout via DataOutputStream (raw UTF-8 for top-level String scalars).

Encoding per type:

  • Boxed(String) at top level: raw UTF-8 (no length prefix)
  • Boxed(String) inside a container: 4-byte BE length + UTF-8 bytes
  • Primitive / Boxed(non-String): fixed-width big-endian bytes via DataOutputStream
  • Array(T) / List(T): 4-byte BE count + N × encode(T)
  • Optional(T): 1-byte tag (0=absent, 1=present) + encode(T) if present

§Wire format (Rust → Java, stdin)

Parameters declared in run(...) are serialised by Rust and piped to the child process’s stdin. Java reads them with DataInputStream.

§Options

All three macros accept zero or more key = "value" pairs before the Java body, comma-separated. Recognised keys:

  • javac = "<args>" — extra arguments passed verbatim to javac (shell-quoted; single/double quotes respected).
  • java = "<args>" — extra arguments passed verbatim to java (shell-quoted; single/double quotes respected).

Macros§

ct_java
Run Java at compile time and splice its return value as a Rust literal.
java
Compile and run zero-argument Java code at program runtime.
java_fn
Return a typed Rust function that compiles and runs Java at program runtime.