3 ms·
I really wish there was a safe way to initialize structs with arbitrary binary data in rust. Perhaps I'm missing something, but it seems intuitively safe, espec
by coldnose 5y ago
I really wish there was a safe way to initialize structs with arbitrary binary data in rust. Perhaps I'm missing something, but it seems intuitively safe, especially if the struct only contains integer primitives.
- sophacles 5y agoHow would it be possible to make it safe? The compiler can't verify that N random bytes input from an arbitrary source are going to be valid for the types in the struct - you have to tell tell the compiler "trust this even if you can't verify it from the source code" - hence the unsafe keyword.
- Alexendoo 5y agoEnter safe transmute - https://github.com/rust-lang/project-safe-transmute/blob/master/rfcs/0000-safe-transmute.md https://github.com/rust-lang/project-safe-transmute/blob/mas...
- sophacles 5y agoOh cool! Thanks for the pointer.
- coldnose 5y agoFor integer primitives, all possible values are valid. Just allowing binary initialization for structs that consist of integer types would allow compatibility with C structure. As it is, I have to either resort to unsafe code or manually deserialize every struct
- sophacles 5y agoOh I see, that makes sense. Also there's a sibling reply to yours for a cool looking proposal to do just that if you haven't seen it yet.