subenum icon indicating copy to clipboard operation
subenum copied to clipboard

Feature request: permit subenum to be "unit-only"

Open BatmanAoD opened this issue 1 year ago • 1 comments

It would be nice to be able to extract just the variants from an enum as a sub-enum, ignoring tuple-like or struct-like data. So this:

#[repr(u32)]
#[subenum(Variants(unit_like))]
enum Foo {
    #[subenum(Variants)]
    SomeTuple(i32, String) = 37,
    #[subenum(Variants)]
    SomeStruct{
        data: String,
    } = 42,
}

...would generate something like this:

#[repr(u32)]
enum Variants {
    SomeTuple = 37,
    SomeStruct = 42,
}

impl TryFrom<Foo> for Variants {
    ...
}

Of course, Foo could not impl From<Variants> unless all the inner data implemented Default, and even then it might be preferable to have a more explicit impl Variants { pub fn as_foo_with_defaults(self) -> Foo { ... } }.

BatmanAoD avatar Oct 22 '24 16:10 BatmanAoD

Does strum cover your use case?

https://docs.rs/strum/latest/strum/derive.EnumDiscriminants.html

paholg avatar Feb 19 '25 19:02 paholg

Oops, sorry, I forgot to respond. Yes, it does; thanks!

BatmanAoD avatar Jul 26 '25 01:07 BatmanAoD