]> git.lizzy.rs Git - rust.git/blob - example/mini_core_hello_world.rs
[WIP]
[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, slice_patterns, never_type, linkage)]
4 #![no_core]
5 #![allow(dead_code)]
6
7 extern crate mini_core;
8
9 use mini_core::*;
10 use mini_core::libc::*;
11
12 unsafe extern "C" fn my_puts(s: *const u8) {
13     puts(s);
14 }
15
16 #[lang = "termination"]
17 trait Termination {
18     fn report(self) -> i32;
19 }
20
21 impl Termination for () {
22     fn report(self) -> i32 {
23         unsafe {
24             NUM = 6 * 7 + 1 + (1u8 == 1u8) as u8; // 44
25             *NUM_REF as i32
26         }
27     }
28 }
29
30 trait SomeTrait {
31     fn object_safe(&self);
32 }
33
34 impl SomeTrait for &'static str {
35     fn object_safe(&self) {
36         unsafe {
37             puts(*self as *const str as *const u8);
38         }
39     }
40 }
41
42 struct NoisyDrop {
43     text: &'static str,
44     inner: NoisyDropInner,
45 }
46
47 struct NoisyDropInner;
48
49 impl Drop for NoisyDrop {
50     fn drop(&mut self) {
51         unsafe {
52             puts(self.text as *const str as *const u8);
53         }
54     }
55 }
56
57 impl Drop for NoisyDropInner {
58     fn drop(&mut self) {
59         unsafe {
60             puts("Inner got dropped!\0" as *const str as *const u8);
61         }
62     }
63 }
64
65 impl SomeTrait for NoisyDrop {
66     fn object_safe(&self) {}
67 }
68
69 enum Ordering {
70     Less = -1,
71     Equal = 0,
72     Greater = 1,
73 }
74
75 #[lang = "start"]
76 fn start<T: Termination + 'static>(
77     main: fn() -> T,
78     argc: isize,
79     argv: *const *const u8,
80 ) -> isize {
81     if argc == 3 {
82         unsafe { puts(*argv); }
83         unsafe { puts(*((argv as usize + intrinsics::size_of::<*const u8>()) as *const *const u8)); }
84         unsafe { puts(*((argv as usize + 2 * intrinsics::size_of::<*const u8>()) as *const *const u8)); }
85     }
86
87     main().report();
88     0
89 }
90
91 static mut NUM: u8 = 6 * 7;
92 static NUM_REF: &'static u8 = unsafe { &NUM };
93
94 macro_rules! assert {
95     ($e:expr) => {
96         if !$e {
97             panic(&(stringify!(! $e), file!(), line!(), 0));
98         }
99     };
100 }
101
102 macro_rules! assert_eq {
103     ($l:expr, $r: expr) => {
104         if $l != $r {
105             panic(&(stringify!($l != $r), file!(), line!(), 0));
106         }
107     }
108 }
109
110 struct Unique<T: ?Sized> {
111     pointer: *const T,
112     _marker: PhantomData<T>,
113 }
114
115 impl<T: ?Sized, U: ?Sized> CoerceUnsized<Unique<U>> for Unique<T> where T: Unsize<U> {}
116
117 fn take_f32(_f: f32) {}
118 fn take_unique(_u: Unique<()>) {}
119
120 fn main() {
121
122 }