Variant size differences

As we saw in the previous chapters, the size of an enumeration will be the size of the biggest element plus the tag, but, as we discussed, this can be troublesome if we have many small variants and one has a bigger size: all variants will occupy the whole space for the biggest one. We saw that an option would be to move the big variant to a heap allocation.

We can detect enumerations with variants significantly bigger than the rest with the variant_size_differences lint. It will check for enumerations where a variant is at least three times bigger than the rest:

#![warn(variant_size_differences)]

enum Test {
A(u8),
B(u32),
}
Note that it will not work for unions and that if we have middle-sized variants, even if the difference between the biggest one and the smallest one is more than three times, the lint will not alert us.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.107.25