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

Commits on Sep 12, 2023

  1. Add TryFromBytes trait

    `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
    joshlf committed Sep 12, 2023
    Configuration menu
    Copy the full SHA
    c79a650 View commit details
    Browse the repository at this point in the history
  2. Add try_transmute!, try_transmute_{ref,mut}!

    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
    joshlf committed Sep 12, 2023
    Configuration menu
    Copy the full SHA
    83e66cc View commit details
    Browse the repository at this point in the history
  3. [derive] Support TryFromBytes for structs

    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
    joshlf committed Sep 12, 2023
    Configuration menu
    Copy the full SHA
    3c385c8 View commit details
    Browse the repository at this point in the history
  4. [WIP][derive] Support custom TryFromBytes validator

    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
    joshlf committed Sep 12, 2023
    Configuration menu
    Copy the full SHA
    e250332 View commit details
    Browse the repository at this point in the history
  5. [WIP] TryFromBuild derive support for enums

    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 committed Sep 12, 2023
    Configuration menu
    Copy the full SHA
    42e44c5 View commit details
    Browse the repository at this point in the history