Complete Runnable Example
Copy and run this example to see serialization in action.Walkthrough
1. Define Your Message Type
Copy and #[repr(C)]. The #[repr(C)] attribute ensures C-compatible memory layout, which is required for serialization.
The
#[repr(C)] attribute ensures the struct has a C-compatible memory layout, which is required for zero-copy serialization and enables cross-language compatibility.2. Send Messages
send_message() method automatically serializes the struct using its to_bytes() implementation. No manual serialization code is needed.
For
Copy types, serialization uses raw byte conversion via the raw::struct_to_bytes() function. The struct is converted to bytes using its #[repr(C)] memory layout.3. Receive and Deserialize
from_bytes() method automatically:
- Handles relocatable-wrapped messages (extracts payload if present)
- Validates byte length matches the type size
- Deserializes using
raw::bytes_to_struct()
from_bytes() automatically unwraps relocatable messages. If the bytes are wrapped in a relocatable message format, it extracts the payload field. If not, it uses the bytes directly. This provides backward compatibility with both wrapped and unwrapped messages.4. How Serialization Works
For Local Communication:- Data is sent as raw bytes using zero-copy shared memory (iceoryx2)
- No serialization occurs - direct memory copy
- Fastest possible performance
- Struct is serialized to bytes using
#[repr(C)]layout - Bytes are sent over Zenoh network
- Deserialization happens on the receiving end
What to Try Next
- Use different message types - Try arrays, nested structs, or fixed-size strings
- Check message size - Use
std::mem::size_of::<YourType>()to see the serialized size - Use protobuf - Try the
ProtoSerializabletrait for cross-language compatibility - Explore supported types - See what types work with automatic serialization