I'm reading source code of spl-token
these days, and I've noted that #[repr(C)]
is always added to structs and enums. For example, here is the token account struct of spl-token
, and it has #[repr(C)]
added.
I just learned that data layout (like alignment, padding...) is deterministic when using C representation. While the default Rust representation may in theory be changed, which means data layout from different versions of Rust compilers are not guaranteed to be the same. This in turn makes it difficult to do things like: when doing CPI, the invoked program is compiled using an older version of Rust compiler, and the Rust representation is changed by Rust Dev Team. So the invoking program (compiled with newer Rust compiler) doesn't know the data layout of the invoked program.
Is my understanding correct? Or is there any other reason Solana Dev Team adds #[repr(C)]
to structs and enums?