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

Clean up repr handling in zerocopy-derive #1581

Open
djkoloski opened this issue Aug 9, 2024 · 0 comments
Open

Clean up repr handling in zerocopy-derive #1581

djkoloski opened this issue Aug 9, 2024 · 0 comments

Comments

@djkoloski
Copy link
Member

It might be cleaner and more reliable to do something like:

enum DiscriminantRepr {
    C,
    Primitive(...),
}

impl DiscriminantRepr {
    fn try_extract_from_reprs(reprs: &[EnumRepr]) -> Result<DiscriminantRepr, ...> { ... }
}

Then we could make derive_is_bit_valid more structural:

pub(crate) fn derive_is_bit_valid(
    reprs: &[EnumRepr],
    ident: &Ident,
    generics: &Generics,
    data: &DataEnum,
) -> TokenStream {
    let idents = RawIdents::for_enum(ident);

    match DiscriminantRepr::try_extract_from_reprs(reprs) {
        Ok(DiscriminantRepr::C) => derive_is_bit_valid_c(data, generics, &idents),
        Ok(DiscriminantRepr::Primitive(prim)) => derive_is_bit_valid_primitive(data, generics, prim, &idents),
        Err(...) => ...,
    }
}

Originally posted by @joshlf in #879 (comment)

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

No branches or pull requests

1 participant