Safe conversions for DSTs

Just wanted to point folks here to my recent post about types which are safe to be deserialized from arbitrary byte sequences: Pre-RFC: Trait for deserializing untrusted input. I think it’s effectively just a special case of InplaceReinterpretAs<T> for [u8], but there’s some discussion there of both ergonomics and also alignment and size issues. Glad to see that everybody seems to be discussing this stuff at once :slight_smile:

EDIT: I realized that a trait that always converts from [u8] is more powerful in one critical way - since it’s not parametric, you can derive it. That’s a big part of the proposal of my trait, ArbitraryBytesSafe. You could then do something like unsafe impl<T: ArbitraryBytesSafe> InplaceReinterpretAs<&T> for &[u8] (and the same for &mut), and use #[derive(ArbitraryBytesSafe)] to get a safe implementation of InplaceReinterpretAs for your type.

1 Like