On 3/1/13 11:51 AM, Graydon Hoare wrote:
It's also to represent enums known-to-C++ with the same values when
projected into rust types.

It'll always be a less-than-faithful translation, though, because C++ enum values don't have to correspond to any of the variants. You can even represent bitfields with C++ enums:

    enum e {
        a = 0x00,
        b = 0x01,
        c = 0x10,
    };

    int main() {
        e x = (e)(a | c);
        return 0;
    }

More importantly, the size of an enum is *undefined* in C++. So outside of C++11 `enum class` it's always dangerous to assume that a given Rust enum is bit-compatible with a C++ enum.

This much I think we need to address generally via explicit control over
packing. Especially on x86, _most_ of our enums should be bytes at most.
https://github.com/mozilla/rust/issues/1704

I'm a bit hesitant to commit to a particular bit representation of enums, since there's lots of room for compiler optimization here. For example, we might want to collapse `Option<~int>` into a nullable pointer, we might want to collapse `Result<(),~str>` into a nullable string, or we might want to collapse `Either<u8,~str>` into 1 word, assuming that strings can never occupy the top 256 bytes of the address space. I've thought for a while that perhaps it's best to just say that the bit pattern of Rust enums is unspecified, to give us as much room as possible to play with optimizations.

Casting _to_ is permitted currently via transmute, which is probably a
fine place to leave it. I agree it'd be nice to provide an auto_enum
syntax extension that defines a bad-value condition, and produces a
.from_int() safe function that hits that condition when given a bad integer.

Agreed.


3. There is no way to have multiple values correspond to a single enum
variant.

Is there in C++? The point of this feature has always been (to my mind)
emulation of C++ where sensible.

No. I guess this point is irrelevant. (Mostly I was thinking that it seems clear that we will need a syntax extension to properly support bitfields and that it is a bit odd to require a syntax extension for bitfields but to have support for explicit enum values in the language, given that they're very similar features.)

Patrick

_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to