Skip to content

Add const macro version of try_from_repr. #74

@ijackson

Description

@ijackson

For Reasons, we want a const version of from_repr.

(We want to have generic types that are parameterised over enumset constants. But Rust only supports integers as const generic parameters. So we're using the repr as the const parameter. We would like to be able to convert that repr back to an EnumSet<T> constly so that any wrong bit values are a compile time rather than runtime panic.)

There's from_repr but it's not const. It can't simply be made const because .expect() isn't available in const. try_from_repr relies on .and_not which would need to be replaced with macro calls (which may be much slower). So just making try_from_repr const is not straightforward. (#27 relates.)

Would you be open to an enum_set_try_from_repr macro? (The version returning Option is sufficient for all use cases, even if the downstream crate has to manually unwrap it, so I propose providing only that fallible version.)

(In our code, it happens that we don't actually need this const from_repr constructor, because the only reason this is const is an error check, and we can do that with ::all().repr() and ad-hoc masking in the packed integer repr.. But it would be nicer to do things in a more principled way.)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions