Debian Codegraph @

gcc/testsuite/rust/execute/torture/str-layout1.rs

raw
1 // { dg-additional-options "-w" }
2 // { dg-output "t1sz=5 t2sz=10\r*" }
3 #![feature(intrinsics)]
4
5 #[lang = "sized"]
6 pub trait Sized {}
7
8 mod mem {
9 extern "rust-intrinsic" {
10 #[rustc_const_stable(feature = "const_transmute", since = "1.46.0")]
11 fn transmute<T, U>(_: T) -> U;
12 }
13 }
14
15 extern "C" {
16 fn printf(s: *const i8, ...);
17 }
18
19 struct FatPtr<T> {
20 data: *const T,
21 len: usize,
22 }
23
24 pub union Repr<T> {
25 rust: *const [T],
26 rust_mut: *mut [T],
27 raw: FatPtr<T>,
28 }
29
30 impl<T> [T] {
31 pub const fn len(&self) -> usize {
32 unsafe { Repr { rust: self }.raw.len }
33 }
34 }
35
36 impl str {
37 pub const fn len(&self) -> usize {
38 self.as_bytes().len()
39 }
40
41 pub const fn as_bytes(&self) -> &[u8] {
42 unsafe { mem::transmute(self) }
43 }
44 }
45
46 fn main() -> i32 {
47 let t1: &str = "TEST1";
48 let t2: &str = &"TEST_12345";
49
50 let t1sz = t1.len();
51 let t2sz = t2.len();
52
53 unsafe {
54 let a = "t1sz=%i t2sz=%i\n";
55 let b = a as *const str;
56 let c = b as *const i8;
57
58 printf(c, t1sz as i32, t2sz as i32);
59 }
60
61 0
62 }