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

[derive] Rename is_c_like -> is_fieldless #804

Merged
merged 1 commit into from
Jan 22, 2024

Conversation

joshlf
Copy link
Member

@joshlf joshlf commented Jan 21, 2024

The implementation of this method checks for the presence of fields. While this is /similar/ to the concept of a C-like enum, it's not the same. Fieldless enums are exactly as described: none of their variants has fields. C-like enums, on the other hand, are more recently referred to as "unit-only" because their variants are not permitted to contain parentheses or curly braces. E.g., the following is a fieldless enum, but is not a C-like/unit-only enum:

  #[repr(u8)]
  enum Foo {
      A,   // This is fine
      B(), // This is not fine
      C{}  // This is not fine
  }

This distinction is important specifically when it comes to discriminants. The syntax Foo::A as u8 computes the discriminant value of the A variant. However, Foo::B is a type constructor, and thus is a function. As a result, Foo::B as u8 computes the address of the function Foo::B.

@joshlf joshlf requested a review from jswrenn January 21, 2024 23:46
@joshlf joshlf enabled auto-merge January 21, 2024 23:46
zerocopy-derive/src/lib.rs Outdated Show resolved Hide resolved
@joshlf joshlf force-pushed the derive-enum-c-like-field-less branch from d620424 to 8c787b3 Compare January 21, 2024 23:51
@joshlf joshlf requested a review from jswrenn January 21, 2024 23:51
jswrenn
jswrenn previously approved these changes Jan 21, 2024
The implementation of this method checks for the presence of fields.
While this is /similar/ to the concept of a C-like enum, it's not the
same. Fieldless enums are exactly as described: none of their variants
has fields. C-like enums, on the other hand, are more recently referred
to as "unit-only" because their variants are not permitted to contain
parentheses or curly braces. E.g., the following is a fieldless enum,
but is not a C-like/unit-only enum:

  #[repr(u8)]
  enum Foo {
      A,   // This is fine
      B(), // This is not fine
      C{}  // This is not fine
  }

This distinction is important specifically when it comes to
discriminants. The syntax `Foo::A as u8` computes the discriminant value
of the `A` variant. However, `Foo::B` is a type constructor, and thus is
a function. As a result, `Foo::B as u8` computes the address of the
function `Foo::B`.
@joshlf joshlf added this pull request to the merge queue Jan 22, 2024
Merged via the queue into main with commit dd107ad Jan 22, 2024
127 checks passed
@joshlf joshlf deleted the derive-enum-c-like-field-less branch January 22, 2024 00:20
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.

None yet

2 participants