Skip to main content

Crate microrti_encoding

Crate microrti_encoding 

Source
Expand description

HLA data encoding helpers of IEEE 1516-2025: the encoding package of the standard Java and C++ APIs (12.11.4.2, 12.12.4.2) as a Rust library.

Attribute and parameter values are opaque octets for the RTI. Federates agree on their layout through the datatypes of the FOM, whose encodings are defined by IEEE 1516.2 (OMT). This crate implements those encodings, including the padding rules, in two layers.

§Typed layer

Encode and Decode are implemented by Rust types whose HLA datatype is known at compile time:

Bare integers and floats have no encoding of their own because the datatype decides size and byte order; the macros accept them with an encoding marker (speed: f32 as HLAfloat32BE).

use microrti_encoding::{
    decode_from_slice_strict, encode_to_vec, hla_fixed_record, DecoderError, HLAfloat64BE, HLAoctet,
};

hla_fixed_record! {
    /// A position report.
    pub struct Report {
        pub quality: HLAoctet,
        pub position: [HLAfloat64BE; 2],
        pub label: String,
    }
}

let report = Report {
    quality: HLAoctet(3),
    position: [HLAfloat64BE(1.0), HLAfloat64BE(-2.0)],
    label: "x".into(),
};
let bytes = encode_to_vec(&report);
// 1 octet, 7 of padding, 2 x 8 octets, a count and one UTF-16 code unit.
assert_eq!(bytes.len(), 1 + 7 + 16 + 4 + 2);
assert_eq!(decode_from_slice_strict::<Report>(&bytes)?, report);

§Dynamic layer

DataElement is the object-safe trait of the standard APIs. Every typed value is a data element, and the containers HLAfixedRecord, HLAfixedArray, HLAvariableArray, HLAvariantRecord and HLAextendableVariantRecord assemble datatypes at run time, for example from a parsed FOM.

use microrti_encoding::{
    DataElement, HLAboolean, HLAfixedRecord, HLAinteractionClassHandle, HLAvariableArray,
};

// MOM datatype HLAinteractionSubList: a variable array of HLAinteractionSubscription records.
let subscription = |class: [u8; 4], active: bool| {
    HLAfixedRecord::new().with(HLAinteractionClassHandle::new(class)).with(HLAboolean(active))
};
let mut list = HLAvariableArray::new(subscription([0; 4], false));
list.push(subscription([0, 0, 0, 9], true))?;
let bytes = list.to_bytes()?;
assert_eq!(bytes, [0, 0, 0, 1, 0, 0, 0, 4, 0, 0, 0, 9, 0, 0, 0, 1]);

let mut decoded = HLAvariableArray::new(subscription([0; 4], false));
decoded.decode_slice_strict(&bytes)?;
assert_eq!(decoded, list);

§Padding

Every datatype has an octet boundary (IEEE 1516.2 4.14.10), and its encoding starts at an offset, counted from the start of the outermost value, that is a multiple of it. Here each element aligns itself before it writes or reads, which yields the padding that 4.14.10.1 to 4.14.10.5 place between the parts of a constructed datatype and none after the last part: a constructed datatype starts at a multiple of its own boundary, the largest of its parts, so offsets counted from the outermost value and from the start of the datatype give the same padding. ByteWriter and ByteReader keep track of the offset. Encoding writes zero padding, as 4.14.10 requires of the sender; the clause says nothing of a receiver that finds other octets there, and decoding ignores what padding holds.

§Robustness

Decoding never panics. Errors are DecoderError values, element counts are checked against the octets that are left before anything is allocated, and memory use is bounded by a small multiple of the input size.

§FOM-driven codec

With the cargo feature fom the module fom adds a third layer: FomCodec encodes and decodes any data type of a merged FDD (microrti-fom) by its name, to and from a self-describing Value tree. The feature is off by default, and the default build does not depend on microrti-fom.

Modules§

extendable
Wire layout of HLAextendableVariantRecord (IEEE 1516.2-2025 4.14.10.3), the variant record encoding that lets FOM modules add alternatives to an existing data type (4.14.8, annex C): the length of the alternative travels with it, so a receiver can skip an alternative it does not know, and adding one never moves the neighbours of the record. The dynamic container, the hla_extendable_variant_record! macro and the FOM-driven codec all use the functions of this module.
fom
FOM-driven dynamic codec (cargo feature fom): encodes and decodes attribute and parameter values by the name of their FOM data type, to and from a self-describing Value tree.

Macros§

hla_enumerated
Declares an enumerated datatype (IEEE 1516.2 4.14.5): a fieldless enum carried by a basic representation.
hla_extendable_variant_record
Declares an HLAextendableVariantRecord datatype as a Rust enum.
hla_fixed_record
Declares an HLAfixedRecord datatype as a Rust struct.
hla_variant_record
Declares an HLAvariantRecord datatype as a Rust enum.

Structs§

ByteReader
Source of a decoding: a cursor over a byte slice.
ByteWriter
Destination of an encoding.
FixedArrayOf
Encoding marker for [V; N] carried as an HLAfixedArray whose elements use the marker R.
HLAASCIIchar
HLAASCIIchar: one ASCII character carried in an HLAoctet. The value is not range checked.
HLAASCIIstring
HLAASCIIstring: an HLAvariableArray of HLAASCIIchar, one octet per character.
HLAattributeHandle
HLAattributeHandle: the encoded form of an attribute handle.
HLAboolean
HLAboolean: the enumerated datatype HLAfalse = 0, HLAtrue = 1 carried in an HLAinteger32BE.
HLAbyte
HLAbyte: one uninterpreted 8-bit byte carried in an HLAoctet.
HLAdimensionHandle
HLAdimensionHandle: the encoded form of a dimension handle.
HLAextendableVariantRecord
HLAextendableVariantRecord (IEEE 1516.2 4.14.10.3): a variant record whose encoding states the length of the alternative, so that a receiver skips alternatives it does not know and other FOM modules can add alternatives (4.14.8, annex C).
HLAfederateHandle
HLAfederateHandle: the encoded form of a federate handle.
HLAfixedArray
HLAfixedArray (IEEE 1516.2 4.14.10.4): a fixed number of elements of one datatype, encoded in order without a count. The octet boundary is the boundary of the element datatype.
HLAfixedRecord
HLAfixedRecord (IEEE 1516.2 4.14.10.1): fields of any datatypes, encoded in order, each at its own octet boundary. Nothing follows the last field. The octet boundary of the record is the largest boundary of its fields.
HLAfloat32BE
HLAfloat32BE: IEEE 754 single precision, big endian.
HLAfloat32LE
HLAfloat32LE: IEEE 754 single precision, little endian.
HLAfloat64BE
HLAfloat64BE: IEEE 754 double precision, big endian.
HLAfloat64LE
HLAfloat64LE: IEEE 754 double precision, little endian.
HLAinteger16BE
HLAinteger16BE: 16-bit two’s complement integer, big endian.
HLAinteger16LE
HLAinteger16LE: 16-bit two’s complement integer, little endian.
HLAinteger32BE
HLAinteger32BE: 32-bit two’s complement integer, big endian.
HLAinteger32LE
HLAinteger32LE: 32-bit two’s complement integer, little endian.
HLAinteger64BE
HLAinteger64BE: 64-bit two’s complement integer, big endian.
HLAinteger64LE
HLAinteger64LE: 64-bit two’s complement integer, little endian.
HLAinteractionClassHandle
HLAinteractionClassHandle: the encoded form of an interaction class handle.
HLAlogicalTime
HLAlogicalTime: the encoded form of a logical time.
HLAlogicalTimeInterval
HLAlogicalTimeInterval (MIM datatype HLAtimeInterval): the encoded form of a time interval.
HLAmessageRetractionHandle
HLAmessageRetractionHandle: the encoded form of a message retraction handle.
HLAobjectClassHandle
HLAobjectClassHandle: the encoded form of an object class handle.
HLAobjectInstanceHandle
HLAobjectInstanceHandle: the encoded form of an object instance handle.
HLAoctet
HLAoctet: one uninterpreted octet.
HLAoctetPairBE
HLAoctetPairBE: two octets, most significant first.
HLAoctetPairLE
HLAoctetPairLE: two octets, least significant first.
HLAopaqueData
HLAopaqueData: an HLAvariableArray of HLAbyte (32-bit big-endian count, then the octets).
HLAparameterHandle
HLAparameterHandle: the encoded form of a parameter handle.
HLAregionHandle
HLAregionHandle: the encoded form of a region handle.
HLAtransportationTypeHandle
HLAtransportationTypeHandle: the encoded form of a transportation type handle.
HLAunicodeChar
HLAunicodeChar: one UTF-16 code unit carried in an HLAoctetPairBE.
HLAunicodeString
HLAunicodeString: an HLAvariableArray of HLAunicodeChar, that is UTF-16 code units in big-endian order (IEEE 1516.2 4.14.4, 4.14.6). The count is the number of code units; characters outside the BMP take a surrogate pair.
HLAunsignedInteger8
HLAunsignedInteger8: 8-bit unsigned integer (MIM basic representation new in 2025).
HLAunsignedInteger16BE
HLAunsignedInteger16BE: 16-bit unsigned integer, big endian.
HLAunsignedInteger16LE
HLAunsignedInteger16LE: 16-bit unsigned integer, little endian.
HLAunsignedInteger32BE
HLAunsignedInteger32BE: 32-bit unsigned integer, big endian.
HLAunsignedInteger32LE
HLAunsignedInteger32LE: 32-bit unsigned integer, little endian.
HLAunsignedInteger64BE
HLAunsignedInteger64BE: 64-bit unsigned integer, big endian.
HLAunsignedInteger64LE
HLAunsignedInteger64LE: 64-bit unsigned integer, little endian.
HLAvariableArray
HLAvariableArray (IEEE 1516.2 4.14.10.5): the element count as HLAinteger32BE, then the elements of one datatype, each at its own octet boundary. An empty array is the count alone. The octet boundary is the larger of 4 and the boundary of the element datatype.
HLAvariantRecord
HLAvariantRecord (IEEE 1516.2 4.14.10.2): the discriminant, padding to the largest octet boundary of all alternatives, then the alternative selected by the discriminant. A discriminant without alternative data is encoded alone. The octet boundary is the largest boundary of the discriminant and of all alternatives.
VariableArrayOf
Encoding marker for Vec<V> carried as an HLAvariableArray whose elements use the marker R.

Enums§

DecoderError
Reasons why a byte sequence cannot be decoded.
EncoderError
Reasons why a value cannot be encoded.

Traits§

BasicData
A basic data representation wrapping one native Rust value (HLAinteger32BE wraps i32).
DataElement
A value that knows its HLA encoding at run time.
Decode
A Rust type that can be rebuilt from its HLA encoding.
Encode
A Rust type with a fixed HLA encoding.
Enumerated
A datatype usable as the discriminant of a variant record: an enumerated datatype, or a basic representation used directly.
Representation
An encoding marker: Self names the HLA datatype used to carry Rust values of type V.

Functions§

decode_from_slice
Decodes one value from the start of bytes; octets after the value are ignored.
decode_from_slice_strict
Decodes one value that must span bytes exactly: trailing octets are DecoderError::TrailingBytes.
decode_prefix
Decodes one value from the start of bytes and returns it with the number of octets it used.
encode_into
Appends the encoding of value to buffer as a new outermost value: padding is computed from the first appended octet, whatever buffer already holds. On error buffer is left unchanged.
encode_to_vec
Encodes value as an outermost value.
max_boundary
Largest of the given octet boundaries, 1 for an empty list. Usable in constants.
padding_for
Number of padding octets needed at offset to reach the next multiple of boundary.
try_encode_to_vec
Encodes value as an outermost value, reporting values that have no encoding.

Type Aliases§

DynElement
A data element behind a pointer, with the 'static bound every element has anyway.
HLAcount
MIM simple datatype HLAcount.
HLAfloat64Time
MIM simple datatype HLAfloat64Time: a bare logical time of the float representation (12.4).
HLAindex
MIM simple datatype HLAindex.
HLAinteger64Time
MIM simple datatype HLAinteger64Time: a bare logical time of the integer representation (12.4).
HLAmsec
MIM simple datatype HLAmsec.
HLAnormalizedFederateHandle
MIM simple datatype HLAnormalizedFederateHandle.
HLAnormalizedInteractionClassHandle
MIM simple datatype HLAnormalizedInteractionClassHandle.
HLAnormalizedObjectClassHandle
MIM simple datatype HLAnormalizedObjectClassHandle.
HLAnormalizedObjectInstanceHandle
MIM simple datatype HLAnormalizedObjectInstanceHandle.
HLAseconds
MIM simple datatype HLAseconds.
HLAtoken
MIM array datatype HLAtoken: an HLAfixedArray of zero HLAbyte, encoded as nothing at all.
HLAupdateRateName
MIM array datatype HLAupdateRateName.