Value Layout
ValueLayout
is the most primitive layout type, representing the layout of,
well, primitives. They are:
- ValueLayout.ADDRESS
- ValueLayout.JAVA_BOOLEAN
- ValueLayout.JAVA_BYTE
- ValueLayout.JAVA_CHAR
- ValueLayout.JAVA_DOUBLE
- ValueLayout.JAVA_FLOAT
- ValueLayout.JAVA_INT
- ValueLayout.JAVA_LONG
- ValueLayout.JAVA_SHORT
- ValueLayout.ADDRESS_UNALIGNED
- ValueLayout.JAVA_CHAR_UNALIGNED
- ValueLayout.JAVA_DOUBLE_UNALIGNED
- ValueLayout.JAVA_FLOAT_UNALIGNED
- ValueLayout.JAVA_INT_UNALIGNED
- ValueLayout.JAVA_LONG_UNALIGNED
- ValueLayout.JAVA_SHORT_UNALIGNED
These all correspond to the Java
primitives (ADDRESS
is a bit special), aligned and unaligned, which have
direct mappings to C primitive types.
Type Mappings: Java, C, and Rust
Java Type | C Type | Rust Type | Description |
---|---|---|---|
ValueLayout.ADDRESS | Pointer | *mut , *const | Pointer to a memory location. |
ValueLayout.JAVA_INT | int | i32 | 32-bit signed integer. |
ValueLayout.JAVA_LONG | long | i64 | 64-bit signed integer. |
ValueLayout.JAVA_SHORT | short | i16 | 16-bit signed integer. |
ValueLayout.JAVA_BYTE | char | i8 | 8-bit signed integer. |
ValueLayout.JAVA_BOOLEAN | char (0 or 1) | bool | Boolean value (true or false). |
ValueLayout.JAVA_FLOAT | float | f32 | 32-bit floating-point number. |
ValueLayout.JAVA_DOUBLE | double | f64 | 64-bit floating-point number. |
ValueLayout.JAVA_CHAR | short (UTF-16) | u16 | 16-bit unsigned integer for UTF-16. |
Unsigned Types
Java Type | C Type | Rust Type | Description |
---|---|---|---|
ValueLayout.JAVA_INT | unsigned int | u8 | 8-bit unsigned integer. |
ValueLayout.JAVA_INT | unsigned int | u16 | 16-bit unsigned integer. |
ValueLayout.JAVA_LONG | unsigned long | u32 | 32-bit unsigned integer. |
ValueLayout.JAVA_LONG | unsigned long | u64 | 64-bit unsigned integer. |
So the _UNALIGNED
versions are exactly the same as their counterparts
except that they have an alignment of 1. This allows storing them unaligned,
but it will also force the JVM to issue special instruction sequences to load
values, since most CPU architectures do not natively support unaligned loads
and stores from or to memory. It is also worth noting that
ValueLayout.JAVA_DOUBLE
and ValueLayout.JAVA_LONG
have
platform-dependent alignment because some CPU architectures require
natural alignment (size = alignment, so 8 in this case) whereas some like
x86 only require an alignment of 4. All other primitives are defined to have
natural alignment.
Beyond representing primitive types, ValueLayouts
also provide access to
different byte ordering (also known as endianness) through the
.withOrder(ByteOrder)
method. The choices for ByteOrder
are BIG_ENDIAN
,
and LITTLE_ENDIAN
, although the static method ByteOrder.nativeOrder()
will return whichever of those your CPU natively uses (usually
LITTLE_ENDIAN
). This is required by many serialization formats, such as
most network formats, because many of them require BIG_ENDIAN
byte
order while most CPU architectures only natively support LITTLE_ENDIAN
.
Rust doesn’t have int
, long
, etc., so we must use a different translation to
For additional information on ValueLayout
, visit Oracle's official documentation, and official Rust resource The Rustonomicon.