Skip to content
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

add support for deriving NoUninit on enums with fields #292

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Freax13
Copy link
Contributor

@Freax13 Freax13 commented Dec 31, 2024

We check for padding like we do for structs except that we also consider the enum discriminant when calculating the unpadded size.

let integer = match repr.repr {
Repr::C => quote!(::core::ffi::c_int),
Repr::CWithDiscriminant(integer) => quote!(#integer),
_ => unreachable!(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_ => unreachable!(),
_ => bail!("Deriving NoUninit is not currently supported for repr(Int) enums with fields")

#[repr(Int)] enums also have a (different) defined layout to #[repr(C)]/#[repr(C, Int)], so ideally those would also be allowed, but if you don't want to add that in this PR, then this should probably be a bail! with an error message instead of unreachable!.

E.g. this unreachable!() currently causes a "proc-macro derive panicked" for a #[repr(u8)] enum.

use bytemuck::NoUninit;

#[derive(Debug, Clone, Copy, NoUninit)]
#[repr(u8)]
enum Foo {
    A(u8),
}
error: proc-macro derive panicked
 --> src/lib.rs:3:30
  |
3 | #[derive(Debug, Clone, Copy, NoUninit)]
  |                              ^^^^^^^^
  |
  = help: message: internal error: entered unreachable code

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks! AFAICT the layout is the same if there is no padding, so adding support for this was easy. I added a test for #[repr(Int)].

We check for padding like we do for structs except that we also
consider the enum discriminant when calculating the unpadded size.
@Freax13 Freax13 force-pushed the featurea/no-uninit-enum-with-fields branch from ecb270e to 5a6d5a0 Compare January 1, 2025 13:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants