3 ms·
That's complicated. Types as automata seems overkill. "A type, Src, is transmutable into a type, Dst, if every possible value of Src is a valid value of Dst."
by Animats 2y ago
That's complicated. Types as automata seems overkill.
"A type, Src, is transmutable into a type, Dst, if every possible value of Src is a valid value of Dst."
Do you really need that much generality? How about "A type is fully mapped if all bit patterns which can be stored in the type are valid values. Fully mapped types of equal length can be transmuted into each other. References are never valid values for this operation."
u8 through u128 are fully mapped, as are i8 through i128. Floats can be considered fully mapped.
Structs of those types with no padding and valid alignment are fully mapped.
This is enough to handle network packets, the main use case.
- wyager 2y agoThere are tons of network protocols where only certain bit patterns are admissible. The algorithm linked here is substantially more useful than just "your entire type has to be made of u8s"
- Animats 2y agoYou don't know on input if the bit pattern will be valid. If you're going to coerce raw bytes into it, the usual case for network protocols, you have to be prepared for any bit pattern.
- wyager 2y agoThe article also discusses a `TryTransmute` trait which will cover this case. Additionally, you often want to coerce from the constrained in-memory representation to the wire representation.