Last change
on this file since 520fa9e was 1725989, checked in by Peter A. Buhr <pabuhr@…>, 15 months ago |
add enumeration test programs for different programming languages
|
-
Property mode
set to
100644
|
File size:
825 bytes
|
Line | |
---|
1 | use std::mem;
|
---|
2 |
|
---|
3 | #[repr(u8)] enum Fieldless {
|
---|
4 | Tuple() = 5,
|
---|
5 | Struct{} = 10,
|
---|
6 | Unit = 12,
|
---|
7 | }
|
---|
8 | impl Fieldless {
|
---|
9 | fn discriminant(&self) -> u8 {
|
---|
10 | // SAFETY: Because `Self` is marked `repr(u8)`, its layout is a `repr(C)` `union`
|
---|
11 | // between `repr(C)` structs, each of which has the `u8` discriminant as its first
|
---|
12 | // field, so we can read the discriminant without offsetting the pointer.
|
---|
13 | unsafe { *<*const _>::from(self).cast::<u8>() }
|
---|
14 | }
|
---|
15 | }
|
---|
16 | fn main() {
|
---|
17 | let mut fl : Fieldless;
|
---|
18 | fl = Fieldless::Struct{};
|
---|
19 | match fl {
|
---|
20 | Fieldless::Struct{} => println!( "Struct" ),
|
---|
21 | _ => (),
|
---|
22 | }
|
---|
23 | if mem::discriminant(&fl) == mem::discriminant(&Fieldless::Struct{}) {
|
---|
24 | println!( "Struct" );
|
---|
25 | }
|
---|
26 | if fl.discriminant() == 10 {
|
---|
27 | println!( "Struct" );
|
---|
28 | }
|
---|
29 | }
|
---|
30 |
|
---|
31 | // Local Variables: //
|
---|
32 | // tab-width: 4 //
|
---|
33 | // End: //
|
---|
Note:
See
TracBrowser
for help on using the repository browser.