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

feat(bitfield): add enum_from_bits! macro #450

Merged
merged 3 commits into from
Jul 22, 2023
Merged

feat(bitfield): add enum_from_bits! macro #450

merged 3 commits into from
Jul 22, 2023

Conversation

hawkw
Copy link
Owner

@hawkw hawkw commented Jul 22, 2023

This branch adds a macro to the mycelium-bitfield crate that allows
automatically generating FromBits implementations for enum types
that are repr(u{N}). For example, we can now write:

mycelium_bitfield::enum_from_bits! {
    #[derive(Debug, Eq, PartialEq)]
    pub enum MyGeneratedEnum<u8> {
        /// Isn't this cool?
        Wow = 0b1001,
        /// It sure is! :D
        Whoa = 0b0110,
    }
}

This probably should be a derive proc-macro but I'm having so much
fun doing macro_rules! crimes in the bitfield crate for whatever
reason...

Closes #443

@hawkw hawkw enabled auto-merge (squash) July 22, 2023 17:45
hawkw added a commit that referenced this pull request Jul 22, 2023
Depends on #450.

This branch replaces some boilerplate implementations of `FromBits` for
enum types with the `enum_from_bits!` macro added to `mycelium-bitfield`
in #450.

I didn't touch a couple manual `FromBits` implementations. Some parts of
`mycelium-pci` manually implement `FromBits` in order to return a custom
error type, so I left those alone. A few places in `maitake` use manual
implementations for enums that cover all possible 2-bit bit patterns, so
that the error arm can be `unreachable_unchecked!`. I didn't touch those
either, since the perf delta may actually matter in those cases.
This branch adds a macro to the `mycelium-bitfield` crate that allows
automatically generating `FromBits` implementations for `enum` types
that are `repr(u{N})`. For example, we can now write:

```rust
mycelium_bitfield::enum_from_bits! {
    #[derive(Debug, Eq, PartialEq)]
    pub enum MyGeneratedEnum<u8> {
        /// Isn't this cool?
        Wow = 0b1001,
        /// It sure is! :D
        Whoa = 0b0110,
    }
}
```

This probably _should_ be a `derive` proc-macro but I'm having so much
fun doing `macro_rules!` crimes in the bitfield crate for whatever
reason...

Closes #443
hawkw added a commit that referenced this pull request Jul 22, 2023
Depends on #450.

This branch replaces some boilerplate implementations of `FromBits` for
enum types with the `enum_from_bits!` macro added to `mycelium-bitfield`
in #450.

I didn't touch a couple manual `FromBits` implementations. Some parts of
`mycelium-pci` manually implement `FromBits` in order to return a custom
error type, so I left those alone. A few places in `maitake` use manual
implementations for enums that cover all possible 2-bit bit patterns, so
that the error arm can be `unreachable_unchecked!`. I didn't touch those
either, since the perf delta may actually matter in those cases.
@hawkw hawkw merged commit ab096b4 into main Jul 22, 2023
18 checks passed
@hawkw hawkw deleted the eliza/bits-enum branch July 22, 2023 18:25
hawkw added a commit that referenced this pull request Jul 23, 2023
Depends on #450.

This branch replaces some boilerplate implementations of `FromBits` for
enum types with the `enum_from_bits!` macro added to `mycelium-bitfield`
in #450.

I didn't touch a couple manual `FromBits` implementations. Some parts of
`mycelium-pci` manually implement `FromBits` in order to return a custom
error type, so I left those alone. A few places in `maitake` use manual
implementations for enums that cover all possible 2-bit bit patterns, so
that the error arm can be `unreachable_unchecked!`. I didn't touch those
either, since the perf delta may actually matter in those cases.
hawkw added a commit that referenced this pull request Jul 23, 2023
## mycelium-bitfield-v0.1.3 - (2023-07-23)

[d0a6f13](https://github.com/hawkw/mycelium/d0a6f13cc53e0cd5dbd493b63ba0711fd06dc985)...[ab096b4](https://github.com/hawkw/mycelium/ab096b4bcf64beda092b3980ca32cb50ad4c2682)

### Added

- Generate `fmt::UpperHex` and `LowerHex`
  ([#292](#292))
  ([b6138c8](https://github.com/hawkw/mycelium/b6138c8b7b704c59e394fd5600c321d7eede0a46))
- Add `Pack::pair_with`
  ([#294](#294))
  ([47b08b7](https://github.com/hawkw/mycelium/47b08b7506712cf5664cf62bf5f0c72fa226a994))
- Add `Pair::pack_from_{src, dst}`
  ([#294](#294))
  ([48f48ca](https://github.com/hawkw/mycelium/48f48cab628c88edc4fa3ce6a045c522976a62e7))
- Skip `_` fields in `fmt::Debug`
  ([#375](#375))
  ([46ca526](https://github.com/hawkw/mycelium/46ca52615338f5341fd0006b56a20379a1a797de))
- Add `display_ascii` to generated bitfields
  ([#420](#420))
  ([7842802](https://github.com/hawkw/mycelium/7842802e1b52da0d7939c972b69964cddc4f875a))
- Select Unicode format with `Display` alt-mode
  ([#420](#420))
  ([a09d55b](https://github.com/hawkw/mycelium/a09d55b7dd81aed292ec8265ce28c8f70f0b293d))
- Add `enum_from_bits!` macro
  ([#450](#450))
  ([ab096b4](https://github.com/hawkw/mycelium/ab096b4bcf64beda092b3980ca32cb50ad4c2682),
  closes [#443](#443))

### Documented

- Fix main branch docs link
  ([#290](#290))
  ([4dbdf37](https://github.com/hawkw/mycelium/4dbdf376aab37ba88d48abf6c25ec2f386f62c44))
- Replace tables with bulleted lists
  ([#451](#451))
  ([10cd876](https://github.com/hawkw/mycelium/10cd8765964ce98e18787f7ba5c299ed7a11a86d))

### Fixed

- Remove recursion in `fmt::Binary`
  ([#292](#292))
  ([0488696](https://github.com/hawkw/mycelium/04886961ba460fceb662fdff8f80d481e54ed241))
- Don't have `fmt::alt` control struct formatting
  ([#292](#292))
  ([03055f0](https://github.com/hawkw/mycelium/03055f086dbdbb68be6cb2a6eef96ffa450df61c))
- Make `pair_with` work with other bitfields
  ([#295](#295))
  ([532ee98](https://github.com/hawkw/mycelium/532ee987e0eddd203fc7ff3698a4c9f36232c669))
- Make `Packing` work with typed specs
  ([#295](#295))
  ([7b86e81](https://github.com/hawkw/mycelium/7b86e811a77831f4745e2b9437b3125ac27be8c8))
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.

bitfield: derive FromBits for repr(uN) enums
1 participant