3 ms·
As other commenters have mentioned, the demand here is almost all due to the desire for smoother interoperation with C code. What's gone unsaid so far is that,
by kibwen 9y ago
As other commenters have mentioned, the demand here is almost all due to the desire for smoother interoperation with C code. What's gone unsaid so far is that, despite still requiring the `unsafe` keyword for many operations, this feature helps make Rust code more safe when calling into C, because it simplifies the interface and eliminates the need for hacked-up handwritten workarounds. IOW, a little bit of standardized unsafety to replace a larger amount of bespoke unsafety.
- Manishearth 9y agoFWIW the hacked up workaround that bindgen uses is _beautiful_ https://github.com/servo/rust-bindgen/blob/master/tests/expectations/tests/union_fields.rs https://github.com/servo/rust-bindgen/blob/master/tests/expe... Basically, if you have a union of A, B, C, you create a struct with three zero-sized fields using BindgenUnionField<A>, and then add a field after that containing enough bits to actually fill out the size. Because the BindgenUnionField is zero sized, a pointer to it is a pointer to the beginning of the struct, and it has an accessor that treats the pointer as the contained type. This makes the API for field access `union.field.as_ref()` instead of `union.field`, but that's still pretty clean. It's still a hack, and I'll be happy to see it go, but it's a really fun hack.
- kibwen 9y agoIf only English already had a word for "beautiful, yet reprehensible". :)
- zem 9y agosirenlike
- tomsthumb 9y agoYou know this word in another language?
- andrewflnr 9y agoThere must be one in German. :)
- euyyn 9y agoProbably just the concatenation, like beautifulyetreprehensible.
- fanf2 9y agoWell "awful" etymologically means "inspiring awe"...
- Retra 9y ago>Because the BindgenUnionField is zero sized, a pointer to it is a pointer to the beginning of the struct Is that a stable assumption?
- Manishearth 9y agoBecause it's repr(C) IIRC yes.