oxidecomputer / amd-apcb

AMD Generic Encapsulated Software Architecture Platform Security Processor Configuration Block manipulation library
Mozilla Public License 2.0
13 stars 1 forks source link

Find a way to generalize struct_variants_enum #88

Open daym opened 1 year ago

daym commented 1 year ago

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

Or

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,