pub trait SimCodec<T> {
// Required methods
fn encode(value: &T) -> Vec<u8> ⓘ;
fn decode(bytes: &[u8]) -> T;
}Available on crate feature
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: Stream::sim_output_with and
Location::sim_input_with use the codec value only to infer its type. Codecs should
therefore be unit structs; any state in the value is ignored.
§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".