Struct inheritance/embedding best practice

That looks very much like a database-heavy project. I'm guessing the goal is/was to have all those fields in all SQL tables?

I wouldn't know a solution from the top of my head, but two things that come to mind would be:

  • Generate the classes by macro:
db_struct! {
  // Macro implementation magically inserts common fields,
  // can even do defaults
  my_field1: foo,
  my_field2: bar
}

This probably is simplest for ORMs (Diesel!) to interpret (I hope)

  • Second: embed the Base-struct as a field in all structs. If you make sure that there is a Default impl for Base, the user wouldn't even have to init it thanks to the default-init syntax as explained here on StackOverflow (that was surprisingly hard to find, do we have a documentation problem here?)

  • Mix and match the above: make a macro that inserts the fields and generates the default impl. (Or use #[derive(default)] )

Just some stabs in the dark to get the discussion started :slight_smile:

2 Likes