Closed ssh352 closed 3 months ago
There are multiple advantages.
There are multiple advantages.
Thank you for your detailed response. It looks like alkahest is as fast as raw struct and has many advantages.
- You can have unbound sequences with slice formula and serialize Vec or similar types.
[repr(C)] is not enough to serialize and deserialize a type. You also need zero padding bytes and all bit patters must represent valid value.
A C++ program sends and receives varieties of raw C struct(alignment 1, zero padding) to my Rust program over UDP.
originally I planned to
However I came across the very fast alkahest crate. Is this scenario a good use case of alkahest?
- Alkahest uses #[forbid(unsafe)] protecting from errors, casting pointer to raw bytes to #[repr(C)] would require unsafe code and failure to meet requirements will cause UB.
Ideally you'd have Alkahest on C++ side as well. But I didn't make C codegen yet.
In Alkahest format of the message is not the same as memory layout of #[repr(C)]
struct, so if you send #[repr(C)]
struct, you have to read into #[repr(C)]
struct.
If possible, use bytemuck
crate and derive Pod
for your structs on Rust side. Then you will be able to safely convert bytes to those structs.
alkahest is very fast. What's the advantage of using alkahest over repr(C) struct? thanks!