File: transmute-zst-generics.rs

package info (click to toggle)
rustc 1.86.0%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 913,560 kB
  • sloc: xml: 158,127; python: 35,921; javascript: 19,689; sh: 19,600; cpp: 18,906; ansic: 13,124; asm: 4,376; makefile: 708; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (44 lines) | stat: -rw-r--r-- 1,160 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
//@ run-pass

// Transmuting to/from ZSTs that contain generics.

#![feature(transmute_generic_consts)]

// Verify non-generic ZST -> generic ZST transmute
unsafe fn cast_zst0<T>(from: ()) -> [T; 0] {
    ::std::mem::transmute::<(), [T; 0]>(from)
}

// Verify generic ZST -> non-generic ZST transmute
unsafe fn cast_zst1<T>(from: [T; 0]) -> () {
    ::std::mem::transmute::<[T; 0], ()>(from)
}

// Verify transmute with generic compound types
unsafe fn cast_zst2<T>(from: ()) -> [(T, T); 0] {
    ::std::mem::transmute::<(), [(T, T); 0]>(from)
}

// Verify transmute with ZST propagation through arrays
unsafe fn cast_zst3<T>(from: ()) -> [[T; 0]; 8] {
    ::std::mem::transmute::<(), [[T; 0]; 8]>(from)
}

// Verify transmute with an extra ZST field
pub struct PtrAndZst<T: ?Sized> {
    _inner: *mut T,
    _other: (),
}
pub unsafe fn cast_ptr<T: ?Sized>(from: *mut T) -> PtrAndZst<T> {
    std::mem::transmute(from)
}

pub fn main() {
    unsafe {
        let _: [u32; 0] = cast_zst0(());
        let _ = cast_zst1::<u32>([]);
        let _: [(u32, u32); 0] = cast_zst2(());
        let _: [[u32; 0]; 8] = cast_zst3(());
        cast_ptr(&mut 42);
    };
}