-
Notifications
You must be signed in to change notification settings - Fork 34
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
Hierarchical enums using arbitrary enum discriminants #81
Comments
This comment was marked as outdated.
This comment was marked as outdated.
Any input on this, now that |
Sorry for the delay in getting back to you on this one. This seems like very specific behaviour, and I'm not sure how general it is... I also don't fully follow the examples, though, so it could be that I'm just not following. Is there some fixed relationship between the outer and inner enums? Or is the situation that the inner-most enums all have uniquely specified discriminants, each of the inner enums may only appear in exactly one outer enum, and we should just pick whichever outer enum happens to be allowed to contain the inner enum? Could you point at some real-world code which is currently being manually written, and give some example inputs and how they map to outputs? |
I want to reduce things like this: https://github.com/rust-osdev/pci_types/blob/8fe7af081fbd34b2022cd33812635244992f8c72/src/device_type.rs#L185 and the enum above. Now for the highest possibly safety and ergonomics it makes sense to parse class, subclass (and sub-subclass) at once. MassStorageSCSIVendorSpecific = 0x1_0000_0000,
MassStorageSCSIStorageDevice = 0x1_0000_0011, but it's a bit much. It looks like there are two usecases for how to parse nested enums:
example for the enums I defined in my initial comment: |
Got something to work with for the tree usecase: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=4d908d6aeb29c1961524bb5b5275b4df Second one as well, for leaf-value-only: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=4e914bd235e82cfb9ecdce10cd34ac22 |
I think this feels like pretty special-case behaviour that probably warrants its own independent implementation outside of this crate. I don't think there's an obvious unique mapping from raw values to a hierarchy of enum values here, so I'd suggest writing a new macro which may use the |
Relevant issue: rust-lang/rust#60553
I am currently working on a PCI driver with many predefined values for classes and their subclasses:
As you can see, these form a kind of hierarchy.
Now I would want to parse the raw values from pci into an enum-value of type
ClassCode
.This would allow for a very nice API:
The text was updated successfully, but these errors were encountered: