This crate provides a few boilerplate macros to enable conversions between types that are unions with a built-in discriminatory field. An example is a network protocol that consists of multiple packet-types with their respective packet-type indicated by a field on the struct:
```rust
pub struct Packet { packet_type: u8, // an unknown (depends on packet type) payload data: [u8; 7], }
pub struct StatusPacket { /// must be 0x02 for a status packet packettype: u8, status0: u8, status1: u8, status2: u8, ts: [u8; 4], }
extern crate pcast;
pub enum ConversionError { WrongPacketType }
subtype_of!(Packet => StatusPacket | ConversionError { Ok(()) });
```
The StatusPacket has three fields for various flags and a four byte timestamp here; its presence is indicated by a value of 0x02 in packet_type.
The subtypeof macro can now be used to declare express this. As a result, a Packet can be tryinto'd into a StatusPacket and references can be passed because &StatusPacket will Deref to &Packet.
A conversion from &mut StatusPacket to &mut Packet is not included, as altering the Packet-structure might violate invariants required by StatusPacket.