]> git.lizzy.rs Git - rust.git/blob - src/test/ui/self/arbitrary_self_types_raw_pointer_trait.rs
Update ui tests
[rust.git] / src / test / ui / self / arbitrary_self_types_raw_pointer_trait.rs
1 // run-pass
2 #![feature(arbitrary_self_types)]
3
4 use std::ptr;
5
6 trait Foo {
7     fn foo(self: *const Self) -> &'static str;
8
9     unsafe fn bar(self: *const Self) -> i64;
10
11     unsafe fn complicated(self: *const *const Self) -> i64 where Self: Sized {
12         (*self).bar()
13     }
14 }
15
16 impl Foo for i32 {
17     fn foo(self: *const Self) -> &'static str {
18         "I'm an i32!"
19     }
20
21     unsafe fn bar(self: *const Self) -> i64 {
22         *self as i64
23     }
24 }
25
26 impl Foo for u32 {
27     fn foo(self: *const Self) -> &'static str {
28         "I'm a u32!"
29     }
30
31     unsafe fn bar(self: *const Self) -> i64 {
32         *self as i64
33     }
34 }
35
36 fn main() {
37     let null_i32 = ptr::null::<i32>() as *const dyn Foo;
38     let null_u32 = ptr::null::<u32>() as *const dyn Foo;
39
40     assert_eq!("I'm an i32!", null_i32.foo());
41     assert_eq!("I'm a u32!", null_u32.foo());
42
43     let valid_i32 = 5i32;
44     let valid_i32_thin = &valid_i32 as *const i32;
45     assert_eq!("I'm an i32!", valid_i32_thin.foo());
46     assert_eq!(5, unsafe { valid_i32_thin.bar() });
47     assert_eq!(5, unsafe { (&valid_i32_thin as *const *const i32).complicated() });
48     let valid_i32_fat = valid_i32_thin as *const dyn Foo;
49     assert_eq!("I'm an i32!", valid_i32_fat.foo());
50     assert_eq!(5, unsafe { valid_i32_fat.bar() });
51
52     let valid_u32 = 18u32;
53     let valid_u32_thin = &valid_u32 as *const u32;
54     assert_eq!("I'm a u32!", valid_u32_thin.foo());
55     assert_eq!(18, unsafe { valid_u32_thin.bar() });
56     assert_eq!(18, unsafe { (&valid_u32_thin as *const *const u32).complicated() });
57     let valid_u32_fat = valid_u32_thin as *const dyn Foo;
58     assert_eq!("I'm a u32!", valid_u32_fat.foo());
59     assert_eq!(18, unsafe { valid_u32_fat.bar() });
60
61 }