Been playing around with PhantomData in rust. Example usage in Rust's documentation deals with unsafe code and referemces, but what follows is an example in deserialization that doesn't touch unsafe code at all.
Say I wanted to implement something like `TryFrom<&[u8]>` for u16. There's an issue here: is the u16 in little endian or in big endian order? The obvious (but dirty) answer is to pick an endian and swap when necessary.
and then I could implement TryFrom roughly like thus using the byteorder crate:
```rust
impl<'a, Endian> TryFrom<&'a [u8]> for Wrappedu16<Endian>
where Endian: byteorder::ByteOrder {
fn try_from(src: &'a [u8]) -> Result<Self, !> {
Ok(Endian::read_u16(src)) // panics, unfortunately
}
}
```
It's not perfect by any means, but I think it's pretty neat!
Note: this is an example based on, but not equal to the case I actually developed it for. The focus is the technique, not the specific trait implemented.
Also: I like markdown, it's a shame that it doesn't actually work here.