Consider compact nested enum?


Nested enum usually takes more space than what they really needs.

As a simplified example, Result<Option<usize>, isize> takes three words, one for the tag of the Result, one for the Option, and the rest for the usize inside. However, the two tags actually only have three states, so I can declare a new enum like enum ResultOption { OkNone, OkSome(usize), Err(isize) } which only takes two words.

Things are not always that simple, though. Sometimes you design an enum with a generic type, which sometimes takes another enum. In that case, it is hard to expand the inner enum into outer enum manually.

As a real example, in Servo’s style system, we have a DeclaredValueOwned where its parameter T can usually be an enum.

I imagine that the compiler can somehow flatten the nested enum, and tag number of outer enum can continue tag number of the largest inner enum. For example, for Result<Option<usize>, isize> case, we can do

  • 0 => Ok(None)
  • 1 => Ok(Some(usize))
  • 2 => Err(isize)

Since 0 and 1 are also tags for the inner enum, you can still take a reference to the inner Option enum and pass it to elsewhere.

What do you think about this idea?




Thanks for the pointer. I think someone must have raised similar stuff, but I failed to find any.