Take dependency on zerocopy for safe transmutation#63
Take dependency on zerocopy for safe transmutation#63daniel-levin wants to merge 0 commit intoPhantomical:masterfrom
Conversation
2e2d9ff to
163d180
Compare
There was a problem hiding this comment.
So I have a couple concerns about this PR. I'm not strictly opposed to adding a dependency on zerocopy but I don't consider avoiding a single transmute between bytes and a known #[repr(C)] struct to be worth it, especially since it is being added as a dependency of the sys crate. As such, I'd like to know where else you think it would be useful.
edit: If you have an external need where you want the structs in the sys crate to derive the relevant zerocopy traits I would also be ok with adding that behind a feature flag.
As a code level concern: the bindings files are autogenerated so any changes made manually will be overwritten the next time I run regenerate.sh. You will need to modify regenerate.sh to make this change automatically.
For the avoidance of doubt, the Rust standard library depends on zerocopy, so it's already a transitive dependency. In an of itself, not a justification for taking the dependency, but possible useful to know.
I incorrectly concluded that the different authors over time appearing in a git blame meant that these files had been hand-edited. I'm going to change the bindgen scripts instead.
Good point. I have found several places. Consider the Lastly, even though we can trivially reason about the safety of transmutes from |
Normally, I don't like taking new dependencies if they can be avoided. But,
zerocopyis no ordinary dependency. First of all, we already depend on it as a transitive dependency ofstd. Secondly, it supplements rustc's analysis of data structures to automatically reason about which transmutes are safe. That is, changingstd::mem::transmutetozerocopy::transmutedoes not simply sequester the unsafe code. Rather, this macro will result in a compile error unlesszerocopycan establish that the resultant unsafe code is sound. Jack Wrenn wrote up a nice article on how it works: https://jack.wrenn.fyi/blog/safety-goggles-for-alchemists/This change includes the only call site of
std::mem::transmutethat could be swapped out in one shot. There are other places where zerocopy can relieve the burden of reasoning about safety, but they will require more invasive changes.