How to specify the type of view to list in Rust to interact with C ++?

Is there a way to do a C ++ style enumeration with an explicit view type in Rust? Example:

enum class Number: int16_t {
    Zero, One, Two, Three, Four, Five, Six, Seven, Eight, Nine
};

If not, is there another way to organize such variables? I interact with an external library, so it is important to specify the type. I know I can just do:

type Number = int16_t;
let One: Number = 1;
let Two: Number = 2;
let Three: Number = 3;

But this, in my opinion, represents a lot of redundancy;


Note that this question is not a duplicate. Is it possible to wrap C enums in Rust? , since this is a C ++ wrapper, not a C wrapper.

+4
source share
1 answer

You can specify a view for listing.

#[repr(i16)]
enum Foo {
    One = 1,
    Two = 2,
}
+8
source

All Articles