1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//! 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:
//!
//! * the basic representations as wrappers of the Rust type they carry: [`HLAinteger32BE`]`(i32)`,
//! [`HLAfloat64LE`]`(f64)`, [`HLAoctet`]`(u8)`...;
//! * the simple datatypes [`HLAASCIIchar`], [`HLAunicodeChar`], [`HLAbyte`], [`HLAboolean`];
//! * [`HLAASCIIstring`], [`HLAunicodeString`] (also `String`), [`HLAopaqueData`];
//! * the handle elements ([`HLAfederateHandle`]...) and [`HLAlogicalTime`], [`HLAlogicalTimeInterval`];
//! * `Vec<T>` (`HLAvariableArray`) and `[T; N]` (`HLAfixedArray`);
//! * structs and enums declared with [`hla_fixed_record!`], [`hla_enumerated!`], [`hla_variant_record!`]
//! and [`hla_extendable_variant_record!`].
//!
//! 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);
//! # Ok::<(), DecoderError>(())
//! ```
//!
//! # 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);
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
//!
//! # Padding
//!
//! Every datatype has an octet boundary, 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 the standard places between the parts of a constructed datatype and
//! none after the last part. [`ByteWriter`] and [`ByteReader`] keep track of the offset.
//!
//! # 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`.
pub use *;
pub use ;
pub use ;
pub use ;
pub use ;
pub use *;
pub use ;
pub use ;
pub use __first_or_zero;
pub use ;