Skip to content

minimal extraction of enum_metadata from PR #207.#208

Closed
ratmice wants to merge 6 commits intoPeternator7:masterfrom
ratmice:enum_metadata
Closed

minimal extraction of enum_metadata from PR #207.#208
ratmice wants to merge 6 commits intoPeternator7:masterfrom
ratmice:enum_metadata

Conversation

@ratmice
Copy link
Copy Markdown
Contributor

@ratmice ratmice commented Jan 24, 2022

This is the minimal extraction of EnumMetadata, which excises OpaqueRepr from my pr #207.

It doesn't yet fix the duplicate code issue, I also think there is a function in #183 which this hadn't implemented.

I need to work on the other crate where the stuff which was excised goes however,
So If anyone wants to pick this up, by all means.

I assume what needs to be done is pull as much as we can out into strum_macros/helpers/ and then call those from the various ..._inner(..) calls. Instead of just duplicating everything. But note that the types and constness between the inherent and trait impl may differ making it difficult to remove all duplication.

Otherwise i'll try and get back to this when I can -- or we can close it and reopen at that time!

@ratmice
Copy link
Copy Markdown
Contributor Author

ratmice commented Jan 24, 2022

It seems like github's diff comparison doesn't pick up on the fact that macros/enum_metadata.rs is a copy of enum_fromrepr.rs with 70% similarity, but viewing in another diff tool like gitk should be helpful when getting rid of duplicated code.

@ratmice
Copy link
Copy Markdown
Contributor Author

ratmice commented Jan 26, 2022

So, I took a first pass at this, somehow my attempt to move out the duplicated code ended up longer than the combined duplication.
It might not be very pretty, likely requires another pass

Edit:
fwiw, I didn't think I would be able to use this macro with the stuff removed from the other branch,
because I thought once we had a type Repr: + Traits, we wouldn't be able to add additional trait bounds to it.

Turns out that was wrong, and I can use this macro as-is in my crate,
it is here: https://github.com/ratmice/enum_extra

This uses MetadataImpl in enum_count and enum_variant_names,
in addition to enum_metadata, and from_repr from the previous commits.
Comment thread strum_macros/src/helpers/metadata_impl.rs Outdated
@ratmice
Copy link
Copy Markdown
Contributor Author

ratmice commented Jan 28, 2022

Wonder if we should include a const REPR_TYPE_STR: &'static str = "u8" for example.

@schneems
Copy link
Copy Markdown
Contributor

The original referenced PR was closed and this one was last updated in 2022. What is the status? Is this still relevant or needed?

Otherwise i'll try and get back to this when I can -- or we can close it and reopen at that time!

❤️ If this isn't actively being worked on can it be closed? If you want to come back to it, I think it would benefit from a brand new PR with a self contained description explaining either what this is fixing (expected/actual disagreement) or what it's adding (can't do today versus with my feature you can).

Can you close this PR?

@ratmice
Copy link
Copy Markdown
Contributor Author

ratmice commented Mar 27, 2025

Sure this can be closed, It works fine, but never got reviewed.
This patch is used in my library https://github.com/ratmice/enum_extra

but I'll likely just rewrite that to avoid using strum, I never heard back after trying to minimize that in this patch...
Until I rewrite that crate to not use this branch, I'll just use my local fork of strum.

@ratmice ratmice closed this Mar 27, 2025
@ratmice
Copy link
Copy Markdown
Contributor Author

ratmice commented Mar 27, 2025

To answer your question about what this does that strum currently can't do today,
this pr basically provided what is now https://docs.rs/strum/latest/strum/trait.VariantMetadata.html

beyond what is in that it also added an associated type Repr that gave you a type listed in the #[repr(..)] where the enum was declared, from_repr and to_repr functions that converted between the repr type and the enum type.

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.

2 participants