-
-
Notifications
You must be signed in to change notification settings - Fork 99
Support unions in derive macros #94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This is probably not correct. The following code passes miri: fn main() {
union U {
_a: bool,
}
unsafe {
let _u: U = core::mem::MaybeUninit::uninit().assume_init();
}
} The current stance of UCG is somewhat undecided, but the issue seems to lean towards "all data is accepted for all unions." Of course, it still requires unsafe code to get the |
:( That is... quite inconvenient
My view is that if you use unsafe to do this sort of thing then all bets are off, so I still think that the impl is fine |
I don't think that's the case at all, because such unsafe code can be behind a sound API. // Library crate A
#[derive(NoPadding)]
union U {
_a: bool,
b: u8,
}
// Library crate B
pub fn lib_fn(x: i32) -> (bool, U) {
if x == 0 {
(true, U { b: '0' })
} else {
// In this case we don't care what `U` is
(false, MaybeUninit::uninit().assume_init())
}
}
// Upstream
fn main() {
let (_, u) = lib_fn(1);
dbg!(bytemuck::transmute::<_, u8>(u))
} The only unsafe code here is in library crate B, but I don't see how you can argue that it's unsound - especially given that it might have been written before the |
I think we need to go with JakobDegen on this one. All unions are effectively entirely padding, in a sense, at least for the purposes of our traits. Since all bytes of a union can be uninit, we can't have them be |
hm, I can buy that I think. I will remove the impls that depend on that from this PR and rebase on main |
It does hint that we may want to rename |
Hmmmmmmm |
I dunno, I still think |
Maybe |
I could dig it. What if we merge this and then I make another PR that does the blanket rename? |
This adds support for unions in the derive macros.
The logic goes:
AnyBitPattern
because actually accessing them is unsafe anyhowUnions areNoPadding
(andPod
, since they're alwaysAnyBitPattern
) ifAll their fields areNoPadding
All their fields are the same size (therefore no padding added at the end of any variants)