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?

1 Like

See https://github.com/rust-lang/rfcs/issues/1230

4 Likes

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

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.