Merge pull request #46 from Havvy/field-less-enums

Rename C-like enums to field-less enums
pull/47/head
Alexis Beingessner 7 years ago committed by GitHub
commit aed22ffef0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -28,7 +28,7 @@ primitive:
* `*T as integer` * `*T as integer`
* `integer as *T` * `integer as *T`
* `number as number` * `number as number`
* `C-like-enum as integer` * `field-less enum as integer`
* `bool as integer` * `bool as integer`
* `char as integer` * `char as integer`
* `u8 as char` * `u8 as char`

@ -23,8 +23,8 @@ passed through the FFI boundary.
C, and is explicitly contrary to the behavior of an empty type in C++, which C, and is explicitly contrary to the behavior of an empty type in C++, which
still consumes a byte of space. still consumes a byte of space.
* DST pointers (fat pointers), tuples, and enums with data (that is, non-C-like * DST pointers (fat pointers), tuples, and enums with fields are not a concept
enums) are not a concept in C, and as such are never FFI-safe. in C, and as such are never FFI-safe.
* As an exception to the rule on enum with data, `Option<&T>` is * As an exception to the rule on enum with data, `Option<&T>` is
FFI-safe if `*const T` is FFI-safe, and they have the same FFI-safe if `*const T` is FFI-safe, and they have the same
@ -42,7 +42,7 @@ binary interface (ABI). Note that enum representation in C is implementation
defined, so this is really a "best guess". In particular, this may be incorrect defined, so this is really a "best guess". In particular, this may be incorrect
when the C code of interest is compiled with certain flags. when the C code of interest is compiled with certain flags.
* "C-like" enums with `repr(C)` or `repr(u*)` still may not be set to an * Field-less enums with `repr(C)` or `repr(u*)` still may not be set to an
integer value without a corresponding variant, even though this is integer value without a corresponding variant, even though this is
permitted behavior in C or C++. It is undefined behavior to (unsafely) permitted behavior in C or C++. It is undefined behavior to (unsafely)
construct an instance of an enum that does not match one of its construct an instance of an enum that does not match one of its
@ -53,19 +53,19 @@ compiled as normal.)
# repr(u*), repr(i*) # repr(u*), repr(i*)
These specify the size to make a C-like enum. If the discriminant overflows the These specify the size to make a field-less enum. If the discriminant overflows
integer it has to fit in, it will produce a compile-time error. You can manually the integer it has to fit in, it will produce a compile-time error. You can
ask Rust to allow this by setting the overflowing element to explicitly be 0. manually ask Rust to allow this by setting the overflowing element to explicitly
However Rust will not allow you to create an enum where two variants have the be 0. However Rust will not allow you to create an enum where two variants have
same discriminant. the same discriminant.
The term "C-like enum" only means that the enum doesn't have data in any The term "field-less enum" only means that the enum doesn't have data in any
of its variants. A C-like enum without a `repr(u*)` or `repr(C)` is of its variants. A field-less enum without a `repr(u*)` or `repr(C)` is
still a Rust native type, and does not have a stable ABI representation. still a Rust native type, and does not have a stable ABI representation.
Adding a `repr` causes it to be treated exactly like the specified Adding a `repr` causes it to be treated exactly like the specified
integer size for ABI purposes. integer size for ABI purposes.
A non-C-like enum is a Rust type with no guaranteed ABI (even if the Any enum with fields is a Rust type with no guaranteed ABI (even if the
only data is `PhantomData` or something else with zero size). only data is `PhantomData` or something else with zero size).
Adding an explicit `repr` to an enum suppresses the null-pointer Adding an explicit `repr` to an enum suppresses the null-pointer

@ -21,7 +21,7 @@ Rust gives you the following ways to lay out composite data:
* arrays (homogeneous product types) * arrays (homogeneous product types)
* enums (named sum types -- tagged unions) * enums (named sum types -- tagged unions)
An enum is said to be *C-like* if none of its variants have associated data. An enum is said to be *field-less* if none of its variants have associated data.
Composite structures will have an alignment equal to the maximum Composite structures will have an alignment equal to the maximum
of their fields' alignment. Rust will consequently insert padding where of their fields' alignment. Rust will consequently insert padding where

Loading…
Cancel
Save