|
| 1 | +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +extern crate core; |
| 12 | +use core::intrinsics::discriminant_value; |
| 13 | + |
| 14 | +enum CLike1 { |
| 15 | + A, |
| 16 | + B, |
| 17 | + C, |
| 18 | + D |
| 19 | +} |
| 20 | + |
| 21 | +enum CLike2 { |
| 22 | + A = 5, |
| 23 | + B = 2, |
| 24 | + C = 19, |
| 25 | + D |
| 26 | +} |
| 27 | + |
| 28 | +enum ADT { |
| 29 | + First(u32, u32), |
| 30 | + Second(u64) |
| 31 | +} |
| 32 | + |
| 33 | +enum NullablePointer { |
| 34 | + Something(&'static u32), |
| 35 | + Nothing |
| 36 | +} |
| 37 | + |
| 38 | +static CONST : u32 = 0xBEEF; |
| 39 | + |
| 40 | +pub fn main() { |
| 41 | + unsafe { |
| 42 | + |
| 43 | + assert_eq!(discriminant_value(&CLike1::A), 0); |
| 44 | + assert_eq!(discriminant_value(&CLike1::B), 1); |
| 45 | + assert_eq!(discriminant_value(&CLike1::C), 2); |
| 46 | + assert_eq!(discriminant_value(&CLike1::D), 3); |
| 47 | + |
| 48 | + assert_eq!(discriminant_value(&CLike2::A), 5); |
| 49 | + assert_eq!(discriminant_value(&CLike2::B), 2); |
| 50 | + assert_eq!(discriminant_value(&CLike2::C), 19); |
| 51 | + assert_eq!(discriminant_value(&CLike2::D), 20); |
| 52 | + |
| 53 | + assert_eq!(discriminant_value(&ADT::First(0,0)), 0); |
| 54 | + assert_eq!(discriminant_value(&ADT::Second(5)), 1); |
| 55 | + |
| 56 | + assert_eq!(discriminant_value(&NullablePointer::Nothing), 1); |
| 57 | + assert_eq!(discriminant_value(&NullablePointer::Something(&CONST)), 0); |
| 58 | + |
| 59 | + assert_eq!(discriminant_value(&10), 0); |
| 60 | + assert_eq!(discriminant_value(&"test"), 0); |
| 61 | + } |
| 62 | +} |
0 commit comments