Handling Rust Arrays in Java
Rust Side
Arrays in Rust are fixed-size collections, and their size and layout must be precisely known for Java to interact with them effectively.
Example Rust Array:
#![allow(unused)] fn main() { #[no_mangle] pub extern "C" fn create_array() -> *mut [i32; 5] { Box::into_raw(Box::new([1, 2, 3, 4, 5])) } }
Explanation: Box::into_raw
creates a raw pointer, enabling Java to handle the array. Here, #[no_mangle]
ensures the Rust function name remains unmangled, making it accessible from Java.
Java Side
To handle arrays from Rust in Java:
- Define a
SequenceLayout
for the array. - Access elements via
VarHandle
.
SequenceLayout arrayLayout = MemoryLayout.sequenceLayout(5, ValueLayout.JAVA_INT);
VarHandle elementHandle = arrayLayout.elementVarHandle(ValueLayout.JAVA_INT);
Explanation:
SequenceLayout: This layout describes a fixed-size array (5 elements of i32
).
VarHandle: Provides access to each element in the array.
Allocating and Accessing Elements
var arena = Arena.ofConfined();
MemorySegment arraySegment = arena.allocate(arrayLayout);
for (int i = 0; i < 5; i++) {
int value = (int) elementHandle.get(arraySegment, (long) i);
System.out.println("Array element " + i + ": " + value);
}
Explanation:
Memory Allocation: The array memory is managed within an arena, ensuring safety and easy cleanup.
Element Access: Each element is accessed via elementHandle
, following Rust’s array layout.