How to represent `enum XXX {A, B, C(Arc)}` in 8 bytes

⚓ Rust    📅 2025-10-16    👤 surdeus    👁️ 1      

surdeus

Suppose I'm writing a big-integer type with small-number optimization for 0 and 1, while larger integers are stored behind an Arc. When representing this type as an enum, the compiler automatically uses the Arc's null pointer for one additional unit variant (If I understand it correctly). However, if two additional variants are provided, the enum's size will be aligned to 16 (on a 64-bit machine), which is unacceptable to me.

enum Integer<T> {
    Zero,
    Other(Arc<T>)
}; // size = 8

enum Integer<T> {
    Zero,
    One,
    Other(Arc<T>)
}; // size = 16

Is it possible to use other invalid memory addresses to represent both Zero and One?
What is the recommended way to do this?

2 posts - 2 participants

Read full topic

🏷️ Rust_feed