pub trait SimCodec<T> {
// Required methods
fn encode(value: &T) -> Vec<u8> ⓘ;
fn decode(bytes: &[u8]) -> T;
}sim only.Expand description
A serialization codec for values crossing the simulation dylib boundary.
The test and simulation dylib exchange only encoded bytes, so custom codecs do not need to
use serde. See BincodeCodec for the default.
The codec is selected purely at the type level, as a type parameter of
Stream::sim_output_with, Location::sim_input_with and their cluster
counterparts (e.g. stream.sim_output_with::<MyCodec>()). No codec value is ever
constructed, so a codec is typically a unit struct, but any type will do.
§Defining a custom codec
Generated dylib code refers to the codec by its definition path. The codec must therefore:
- be public through to the crate root, and
- live outside
#[cfg(test)]and integration-test targets.
Its serialization library and hydro_lang with the sim feature must be regular
dependencies, not dev-dependencies.
use hydro_lang::sim::codec::SimCodec;
pub struct Message(u32);
pub struct MessageCodec;
impl SimCodec<Message> for MessageCodec {
fn encode(value: &Message) -> Vec<u8> {
value.0.to_le_bytes().to_vec()
}
fn decode(bytes: &[u8]) -> Message {
Message(u32::from_le_bytes(bytes.try_into().unwrap()))
}
}Required Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".