File: actually_not_an_enum-discriminant.rs

package info (click to toggle)
rustc 1.87.0%2Bdfsg1-1~bpo13%2B2
  • links: PTS, VCS
  • area: main
  • in suites: trixie-backports
  • size: 925,560 kB
  • sloc: xml: 158,127; python: 36,039; javascript: 19,761; sh: 19,737; cpp: 18,981; ansic: 13,133; asm: 4,376; makefile: 710; perl: 29; lisp: 28; ruby: 19; sql: 11
file content (49 lines) | stat: -rw-r--r-- 1,197 bytes parent folder | download | duplicates (15)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//@ run-pass
#![feature(core_intrinsics)]

use std::intrinsics::discriminant_value;

struct Zst;

struct Struct {
    _a: u32,
}

union Union {
    _a: u32,
}

fn check(v: u8) {
    assert_eq!(v, 0);
}

pub fn generic<T>()
where
    for<'a> T: Fn(&'a isize),
{
    let v: Vec<T> =  Vec::new();
    let _: u8 = discriminant_value(&v);
}

fn main() {
    // check that we use `u8` as the discriminant value
    // for everything that is not an enum.
    check(discriminant_value(&true));
    check(discriminant_value(&'a'));
    check(discriminant_value(&7));
    check(discriminant_value(&7.0));
    check(discriminant_value(&Zst));
    check(discriminant_value(&Struct { _a: 7 }));
    check(discriminant_value(&Union { _a: 7 }));
    check(discriminant_value(&[7, 77]));
    check(discriminant_value(&(7 as *const ())));
    check(discriminant_value(&(7 as *mut ())));
    check(discriminant_value(&&7));
    check(discriminant_value(&&mut 7));
    check(discriminant_value(&check));
    let fn_ptr: fn(u8) = check;
    check(discriminant_value(&fn_ptr));
    let hrtb: for<'a> fn(&'a str) -> &'a str = |x| x;
    check(discriminant_value(&hrtb));
    check(discriminant_value(&(7, 77, 777)));
}