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

[WIP] TryFromBuild derive support for enums #371

Conversation

joshlf
Copy link
Member

@joshlf joshlf commented Sep 11, 2023

TODO:

  • Clean up repr computation
  • Support enums with fields
  • If we don't support enums with fields, enforce that
  • Should enum variants be considered "used" in the sense of dead code? It's possible to never construct an enum variant from Rust's perspective even though it is constructed via TryFromBytes.


let cases = var.fields.fields().into_iter().map(foo);
Some(quote!(|| {
struct Variant {
Copy link
Member Author

Choose a reason for hiding this comment

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

Give this an explicit repr -- oops!

@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-enums branch from 3c07e88 to ba55c12 Compare September 11, 2023 22:28
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-validator branch from c2db8b1 to 342026f Compare September 11, 2023 22:28
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-enums branch from ba55c12 to 6f7276b Compare September 11, 2023 22:52
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-validator branch from 342026f to 7286c44 Compare September 11, 2023 22:52
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-enums branch from 6f7276b to 4ad50c6 Compare September 11, 2023 23:05
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-validator branch from 7286c44 to bff9a97 Compare September 11, 2023 23:05
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-enums branch from 4ad50c6 to 7898e14 Compare September 11, 2023 23:31
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-validator branch from bff9a97 to d463caf Compare September 11, 2023 23:31
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-enums branch 2 times, most recently from 58a3786 to 1277a6e Compare September 12, 2023 00:34
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-validator branch from d463caf to 90c2aed Compare September 12, 2023 00:34
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-enums branch from 1277a6e to 34d48a2 Compare September 12, 2023 00:48
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-validator branch from 90c2aed to 7707fb1 Compare September 12, 2023 00:48
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-enums branch from 34d48a2 to f912032 Compare September 12, 2023 00:48
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-validator branch from 7707fb1 to de82f0e Compare September 12, 2023 00:48
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-enums branch from f912032 to f7ad4d1 Compare September 12, 2023 01:19
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-validator branch from de82f0e to 575c570 Compare September 12, 2023 01:19
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-enums branch from f7ad4d1 to 18ed997 Compare September 12, 2023 01:40
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-validator branch from 575c570 to ceb1983 Compare September 12, 2023 01:40
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-enums branch from 18ed997 to 8185e85 Compare September 12, 2023 02:22
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-validator branch from ceb1983 to 98ca637 Compare September 12, 2023 02:22
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-enums branch from 8185e85 to 94b0458 Compare September 12, 2023 02:49
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-validator branch from 98ca637 to 8eff3b0 Compare September 12, 2023 02:49
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-enums branch from 94b0458 to be161c6 Compare September 12, 2023 03:19
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-validator branch from 8eff3b0 to 4004cbe Compare September 12, 2023 03:20
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-enums branch 2 times, most recently from 5fbe8b4 to 06f8296 Compare September 12, 2023 03:43
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-validator branch from 4004cbe to 8595f94 Compare September 12, 2023 03:43
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-enums branch from 06f8296 to 06b5db5 Compare September 12, 2023 06:02
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-validator branch from 8595f94 to 843e2ee Compare September 12, 2023 06:02
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-enums branch from 06b5db5 to 8b0983c Compare September 12, 2023 06:16
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-validator branch from 843e2ee to a46bdc1 Compare September 12, 2023 06:16
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-enums branch from 8b0983c to 286537d Compare September 12, 2023 15:41
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-validator branch from a46bdc1 to e1cd6a7 Compare September 12, 2023 15:41
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-enums branch from 286537d to d4cd61b Compare September 12, 2023 15:44
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-validator branch from e1cd6a7 to 55aee47 Compare September 12, 2023 15:44
joshlf and others added 5 commits September 12, 2023 16:46
`TryFromBytes` can be implemented for types which are not `FromZeroes`
or `FromBytes`; it supports performing a runtime check to determine
whether a given byte sequence contains a valid instance of `Self`.

This is the first step of #5. Future commits will add support for a
custom derive and for implementing `TryFromBytes` on unsized types.

TODO:
- More thorough tests for non-FromBytes types (bool, char, etc)
- Tests that go through the `TryFromBytes` public methods rather than
  testing `is_bit_valid` directly
- Pick public TryFromBytes method names that are more consistent with
  FromBytes? In particular:
  - try_from_ref -> try_from_bytes
  - try_from_mut -> try_from_bytes_mut

Makes progress on #5
TODO: Commit message body

TODO:
- In `try_transmute!`, should the argument be dropped or forgotten (ie,
  `mem::forget`) when the transmute fails? We could also return the
  original value in a `Result::Error`, but that would be inconsistent
  with `TryFrom`. Then again, `TryFrom` provides a custom error type, so
  in theory implementers could do that if they wanted to. Most of the
  types that don't consume Copy types.

Makes progress on #5
TODO:
- Should DataExt::fields have a different shape? Extracting field names
  for enums doesn't really make sense
- Lots and lots of tests
- If we manage to land derive(KnownLayout) first, replace manual impls
  of KnownLayout with the derive
- Is there any way to make sure the code we emit doesn't emit warnings?
  I tested with an earlier version of the code that had lots of code
  smells like pointer coercions that emitted warnings, and even after
  emitting `#[deny(...)]` attributes, I couldn't get code which made use
  of the derive to complain at all - the compiler just happily accepted
  it.

Makes progress on #5
TODO:
- Cleaner way to pass name of validator to `impl_block`?
- Cleaner way to parse validator attribute?
- Safety comment in emitted call to `validate`
- Tests (especially including tests for the error message resulting from
  passing a validator with the wrong type signature - test for both
  argument types and return types)
  - Also test for invalid zerocopy attributes
  - Also test for hygiene - that the validator can't access variables
    from the `is_bit_valid` function body scope
- Other misc TODO comments in code
TODO:
- Clean up repr computation
- Support enums with fields
- If we don't support enums with fields, enforce that
- Should enum variants be considered "used" in the sense of dead code?
  It's possible to never construct an enum variant from Rust's
  perspective even though it is constructed via TryFromBytes.
- Confirm that we reject uninhabited enums. Currently Rust doesn't let
  you put a repr on these enums, so we would reject them for lack of a
  repr, but we should probably have a more reliable check than that.
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-enums branch from d4cd61b to 42e44c5 Compare September 12, 2023 16:47
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-validator branch 4 times, most recently from 5c45ea8 to 3271ca4 Compare September 19, 2023 16:43
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-validator branch 3 times, most recently from 1439d4b to fe3118f Compare September 19, 2023 22:33
@joshlf joshlf force-pushed the try-from-bytes-raw-argument-to-is-bit-valid--derive-validator branch from fe3118f to 1f8ff06 Compare October 12, 2023 21:41
@joshlf joshlf closed this Sep 10, 2024
@joshlf joshlf deleted the try-from-bytes-raw-argument-to-is-bit-valid--derive-enums branch September 10, 2024 13:38
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.

1 participant