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

Find a way to generalize struct_variants_enum #88

Open
daym opened this issue Jun 9, 2023 · 0 comments
Open

Find a way to generalize struct_variants_enum #88

daym opened this issue Jun 9, 2023 · 0 comments

Comments

@daym
Copy link
Collaborator

daym commented Jun 9, 2023

It's kinda silly that we implement that outselves, as a macro_rules macro no less. But previous attempts using enum dispatch crates didn't work.

It would be nicer to have a derive macro or something.

What this is supposed to do is if you have this

mod quux {
  #[a]
  struct A {
  }
  #[b]
  struct B {
  }
}

then it should add this:

mod quux {
    enum ElementRef<'a> {
        Unknown(&'a [u8]),
        A(&'a A),
        B(&'a B),
    }
    enum MutElementRef<'a> {
        Unknown(&'a mut [u8]),
        A(&'a mut A),
        B(&'a mut B),
    }
    enum Element {
        Unknown(Vec<u8>),
        A(A),
        B(B),
    }
}

The current solution works fine but is "a little" opaque and wastes the Rust compiler's time building a state machine using text substitution.

Problems replacing it are are:

Either

  • Currently, Rust does not support #[derive] on mod, only on struct and enum.

Or

  • Currently, Rust does not support implementing traits on individual enum variants.

One or the other would be required, or a horrible workaround (make the derive macro go on each struct and automatically collect stuff for the new enums, then emit that WHEN?).

Also,

  • It's not allowed to do this (twice): mod a { } mod a { }.
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