]> git.lizzy.rs Git - rust.git/blob - example/mini_core_hello_world.rs
Rename examples directory to example to prevent rls trying to compile its content
[rust.git] / example / mini_core_hello_world.rs
1 // Adapted from https://github.com/sunfishcode/mir2cranelift/blob/master/rust-examples/nocore-hello-world.rs
2
3 #![feature(no_core, unboxed_closures, start, lang_items, box_syntax)]
4 #![no_core]
5 #![allow(dead_code)]
6
7 extern crate mini_core;
8
9 use mini_core::*;
10
11 #[link(name = "c")]
12 extern "C" {
13     fn puts(s: *const u8);
14 }
15
16 unsafe extern "C" fn my_puts(s: *const u8) {
17     puts(s);
18 }
19
20 // TODO remove when jit supports linking rlibs
21 #[cfg(jit)]
22 extern "C" fn panic<T>(_: T) -> ! {
23     unsafe {
24         intrinsics::abort();
25     }
26 }
27
28 #[lang = "termination"]
29 trait Termination {
30     fn report(self) -> i32;
31 }
32
33 impl Termination for () {
34     fn report(self) -> i32 {
35         unsafe {
36             NUM = 6 * 7 + 1 + (1u8 == 1u8) as u8; // 44
37             *NUM_REF as i32
38         }
39     }
40 }
41
42 trait SomeTrait {
43     fn object_safe(&self);
44 }
45
46 impl SomeTrait for &'static str {
47     fn object_safe(&self) {
48         unsafe {
49             puts(*self as *const str as *const u8);
50         }
51     }
52 }
53
54 struct NoisyDrop {
55     text: &'static str,
56     inner: NoisyDropInner,
57 }
58
59 struct NoisyDropInner;
60
61 impl Drop for NoisyDrop {
62     fn drop(&mut self) {
63         unsafe {
64             puts(self.text as *const str as *const u8);
65         }
66     }
67 }
68
69 impl Drop for NoisyDropInner {
70     fn drop(&mut self) {
71         unsafe {
72             puts("Inner got dropped!\0" as *const str as *const u8);
73         }
74     }
75 }
76
77 #[lang = "start"]
78 fn start<T: Termination + 'static>(
79     main: fn() -> T,
80     _argc: isize,
81     _argv: *const *const u8,
82 ) -> isize {
83     main().report() as isize
84 }
85
86 static mut NUM: u8 = 6 * 7;
87 static NUM_REF: &'static u8 = unsafe { &NUM };
88
89 macro_rules! assert {
90     ($e:expr) => {
91         if !$e {
92             panic(&(stringify!(! $e), file!(), line!(), 0));
93         }
94     };
95 }
96
97 macro_rules! assert_eq {
98     ($l:expr, $r: expr) => {
99         if $l != $r {
100             panic(&(stringify!($l != $r), file!(), line!(), 0));
101         }
102     }
103 }
104
105 struct Unique<T: ?Sized> {
106     pointer: *const T,
107     _marker: PhantomData<T>,
108 }
109
110 impl<T: ?Sized, U: ?Sized> CoerceUnsized<Unique<U>> for Unique<T> where T: Unsize<U> {}
111
112 fn main() {
113     unsafe {
114         let hello: &[u8] = b"Hello\0" as &[u8; 6];
115         let ptr: *const u8 = hello as *const [u8] as *const u8;
116         puts(ptr);
117
118         // TODO remove when jit supports linking rlibs
119         #[cfg(not(jit))]
120         {
121             let world: Box<&str> = box "World!\0";
122             puts(*world as *const str as *const u8);
123             world as Box<SomeTrait>;
124         }
125
126         assert_eq!(intrinsics::size_of_val(hello) as u8, 6);
127
128         let chars = &['C', 'h', 'a', 'r', 's'];
129         let chars = chars as &[char];
130         assert_eq!(intrinsics::size_of_val(chars) as u8, 4 * 5);
131
132         let a: &dyn SomeTrait = &"abc\0";
133         a.object_safe();
134
135         assert_eq!(intrinsics::size_of_val(a) as u8, 16);
136         assert_eq!(intrinsics::size_of_val(&0u32) as u8, 4);
137
138         assert_eq!(intrinsics::min_align_of::<u16>() as u8, 2);
139         assert_eq!(intrinsics::min_align_of_val(&a) as u8, intrinsics::min_align_of::<&str>() as u8);
140
141         assert!(!intrinsics::needs_drop::<u8>());
142         assert!(intrinsics::needs_drop::<NoisyDrop>());
143
144         Unique {
145             pointer: 0 as *const &str,
146             _marker: PhantomData,
147         } as Unique<dyn SomeTrait>;
148     }
149
150     let _ = NoisyDrop {
151         text: "Outer got dropped!\0",
152         inner: NoisyDropInner,
153     };
154
155     const FUNC_REF: Option<fn()> = Some(main);
156     match FUNC_REF {
157         Some(_) => {},
158         None => assert!(false),
159     }
160 }