Expand description
The Marshal trait - the type-system contract for “this value can
cross an address-space boundary byte-identically.”
Marshal is strictly stronger than Send. A Send value can
travel between threads inside one process, where pointers and
references mean the same thing in both threads. A Marshal value
can travel between processes (or be serialised to disk and read
back), where pointers into the originating process’s heap, file
descriptors, and any other resource handle that means different
things in different address spaces are forbidden.
§The contract
Marshal::PAYLOAD_BYTESis the exact byte width of the marshalled form.Marshal::marshalwrites exactlyPAYLOAD_BYTESinto a caller-supplied buffer.Marshal::unmarshalreads exactlyPAYLOAD_BYTESand reconstructs a value byte-identical to the original.- Round-tripping:
unmarshal(&buf)aftermarshal(&v, &mut buf)produces a value indistinguishable fromvfor every valuev.
§Why unsafe
Correctness depends on every reachable byte of the value being
position-independent across address spaces. The compiler cannot
check this for arbitrary user types - a type with an inner
Box<u8> plus a manual marshal impl that copies the box’s
raw bytes compiles cleanly and crashes at runtime. The trait is
therefore unsafe to implement; the implementer asserts the
contract holds.
§Auto-impls
Safe blanket impls are provided for the primitive integer and
floating-point types, bool, (), and [T; N] where T: Marshal. These cover the common case (move a u64 job ID, an
[u8; 48] argument blob, a (u32, u32) pair) without requiring
any unsafe code at the call site.
§Connection to pass_registry
subetha_cxc::pass_registry solves the same problem (closures
that need to execute in another process) at the runtime layer,
by registering closure handlers by integer ID. Marshal is the
compile-time counterpart: a closure whose captured environment
reduces to a Marshal payload can be shipped across processes by
marshalling the payload and looking up the handler by ID. Both
layers cooperate to make cross-process execution byte-safe.
Enums§
- Marshal
Error - Error returned when
Marshal::unmarshalcannot reconstruct a value from the source buffer.
Traits§
- Marshal
- Type-system contract for “this value can be flattened into a fixed-size byte payload and reconstructed byte-identically in another address space.”