Stable enum tags?


#1

A little experiment about using Rust:
https://dominuscarnufex.github.io/cours/rs-kernel/en.html

It says:

Rust gives absolutely no guaranty on the internal representation of its enums. That means that when you add new variants to the enum, the actual numerical value associated to each variant might change.

Thus, userspace code compiled with one version of the source might not be compatible with a kernel compiled with another version of the code. The fact that the stdlib and the kernel are compiled at the same time and from the exact same source attenuates the problem, as long as the softwares are dynamically linked to the stdlib.

On the other hand, software statically linked might need to be recompiled to continue working. One way around might be to never change the order of the variants, and only adding new ones at the end of the enum, so the number of the preexisting ones should remain the same across versions.

If that’s a real problem can you solve it adding tag values?

enum Foo {
    #[tag(0)]
    Spam(u8),

    #[tag(1)]
    Bar(u8),

    #[tag(2)]
    Baz(f32, f32),
}

#2

Even with fixed tag values, the entire layout is not a stable ABI. I think I would stick to #[repr(C)] for such interfaces.