]> git.lizzy.rs Git - rust.git/blob - example/mini_core.rs
Print a message when panicking from mini_core
[rust.git] / example / mini_core.rs
1 #![feature(no_core, lang_items, intrinsics, unboxed_closures, type_ascription)]
2 #![no_core]
3 #![allow(dead_code)]
4
5 #[lang = "sized"]
6 pub trait Sized {}
7
8 #[lang = "unsize"]
9 pub trait Unsize<T: ?Sized> {}
10
11 #[lang = "coerce_unsized"]
12 pub trait CoerceUnsized<T> {}
13
14 impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b T {}
15 impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a mut U> for &'a mut T {}
16 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *const T {}
17 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for *mut T {}
18
19 #[lang = "dispatch_from_dyn"]
20 pub trait DispatchFromDyn<T> {}
21
22 // &T -> &U
23 impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<&'a U> for &'a T {}
24 // &mut T -> &mut U
25 impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<&'a mut U> for &'a mut T {}
26 // *const T -> *const U
27 impl<T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<*const U> for *const T {}
28 // *mut T -> *mut U
29 impl<T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<*mut U> for *mut T {}
30
31 #[lang = "receiver"]
32 pub trait Receiver {}
33
34 impl<T: ?Sized> Receiver for &T {}
35 impl<T: ?Sized> Receiver for &mut T {}
36 impl<T: ?Sized> Receiver for Box<T> {}
37
38 #[lang = "copy"]
39 pub unsafe trait Copy {}
40
41 unsafe impl Copy for bool {}
42 unsafe impl Copy for u8 {}
43 unsafe impl Copy for u16 {}
44 unsafe impl Copy for u32 {}
45 unsafe impl Copy for u64 {}
46 unsafe impl Copy for usize {}
47 unsafe impl Copy for i8 {}
48 unsafe impl Copy for i16 {}
49 unsafe impl Copy for i32 {}
50 unsafe impl Copy for isize {}
51 unsafe impl Copy for char {}
52 unsafe impl<'a, T: ?Sized> Copy for &'a T {}
53 unsafe impl<T: ?Sized> Copy for *const T {}
54 unsafe impl<T: ?Sized> Copy for *mut T {}
55
56 #[lang = "sync"]
57 pub unsafe trait Sync {}
58
59 unsafe impl Sync for bool {}
60 unsafe impl Sync for u8 {}
61 unsafe impl Sync for u16 {}
62 unsafe impl Sync for u32 {}
63 unsafe impl Sync for u64 {}
64 unsafe impl Sync for usize {}
65 unsafe impl Sync for i8 {}
66 unsafe impl Sync for i16 {}
67 unsafe impl Sync for i32 {}
68 unsafe impl Sync for isize {}
69 unsafe impl Sync for char {}
70 unsafe impl<'a, T: ?Sized> Sync for &'a T {}
71 unsafe impl Sync for [u8; 16] {}
72
73 #[lang = "freeze"]
74 trait Freeze {}
75
76 #[lang = "not"]
77 pub trait Not {
78     type Output;
79
80     fn not(self) -> Self::Output;
81 }
82
83 impl Not for bool {
84     type Output = bool;
85
86     fn not(self) -> bool {
87         !self
88     }
89 }
90
91 #[lang = "mul"]
92 pub trait Mul<RHS = Self> {
93     type Output;
94
95     #[must_use]
96     fn mul(self, rhs: RHS) -> Self::Output;
97 }
98
99 impl Mul for u8 {
100     type Output = Self;
101
102     fn mul(self, rhs: Self) -> Self::Output {
103         self * rhs
104     }
105 }
106
107 #[lang = "add"]
108 pub trait Add<RHS = Self> {
109     type Output;
110
111     fn add(self, rhs: RHS) -> Self::Output;
112 }
113
114 impl Add for u8 {
115     type Output = Self;
116
117     fn add(self, rhs: Self) -> Self {
118         self + rhs
119     }
120 }
121
122 impl Add for usize {
123     type Output = Self;
124
125     fn add(self, rhs: Self) -> Self {
126         self + rhs
127     }
128 }
129
130 #[lang = "sub"]
131 pub trait Sub<RHS = Self> {
132     type Output;
133
134     fn sub(self, rhs: RHS) -> Self::Output;
135 }
136
137 impl Sub for usize {
138     type Output = Self;
139
140     fn sub(self, rhs: Self) -> Self {
141         self - rhs
142     }
143 }
144
145 #[lang = "bitor"]
146 pub trait BitOr<RHS = Self> {
147     type Output;
148
149     #[must_use]
150     fn bitor(self, rhs: RHS) -> Self::Output;
151 }
152
153 impl BitOr for bool {
154     type Output = bool;
155
156     fn bitor(self, rhs: bool) -> bool {
157         self | rhs
158     }
159 }
160
161 impl<'a> BitOr<bool> for &'a bool {
162     type Output = bool;
163
164     fn bitor(self, rhs: bool) -> bool {
165         *self | rhs
166     }
167 }
168
169 #[lang = "eq"]
170 pub trait PartialEq<Rhs: ?Sized = Self> {
171     fn eq(&self, other: &Rhs) -> bool;
172     fn ne(&self, other: &Rhs) -> bool;
173 }
174
175 impl PartialEq for u8 {
176     fn eq(&self, other: &u8) -> bool {
177         (*self) == (*other)
178     }
179     fn ne(&self, other: &u8) -> bool {
180         (*self) != (*other)
181     }
182 }
183
184 impl PartialEq for u16 {
185     fn eq(&self, other: &u16) -> bool {
186         (*self) == (*other)
187     }
188     fn ne(&self, other: &u16) -> bool {
189         (*self) != (*other)
190     }
191 }
192
193 impl PartialEq for u32 {
194     fn eq(&self, other: &u32) -> bool {
195         (*self) == (*other)
196     }
197     fn ne(&self, other: &u32) -> bool {
198         (*self) != (*other)
199     }
200 }
201
202 impl PartialEq for usize {
203     fn eq(&self, other: &usize) -> bool {
204         (*self) == (*other)
205     }
206     fn ne(&self, other: &usize) -> bool {
207         (*self) != (*other)
208     }
209 }
210
211 impl PartialEq for char {
212     fn eq(&self, other: &char) -> bool {
213         (*self) == (*other)
214     }
215     fn ne(&self, other: &char) -> bool {
216         (*self) != (*other)
217     }
218 }
219
220 impl<T: ?Sized> PartialEq for *const T {
221     fn eq(&self, other: &*const T) -> bool {
222         *self == *other
223     }
224     fn ne(&self, other: &*const T) -> bool {
225         *self != *other
226     }
227 }
228
229 #[lang = "neg"]
230 pub trait Neg {
231     type Output;
232
233     fn neg(self) -> Self::Output;
234 }
235
236 impl Neg for isize {
237     type Output = isize;
238
239     fn neg(self) -> isize {
240         -self
241     }
242 }
243
244 pub enum Option<T> {
245     Some(T),
246     None,
247 }
248
249 pub use Option::*;
250
251 #[lang = "phantom_data"]
252 pub struct PhantomData<T: ?Sized>;
253
254 #[lang = "fn_once"]
255 #[rustc_paren_sugar]
256 pub trait FnOnce<Args> {
257     type Output;
258
259     extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
260 }
261
262 #[lang = "fn_mut"]
263 #[rustc_paren_sugar]
264 pub trait FnMut<Args>: FnOnce<Args> {
265     extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output;
266 }
267
268 #[lang = "panic"]
269 pub fn panic(&(_msg, _file, _line, _col): &(&'static str, &'static str, u32, u32)) -> ! {
270     unsafe {
271         libc::puts("Panicking\0" as *const str as *const u8);
272         intrinsics::abort();
273     }
274 }
275
276 #[lang = "eh_personality"]
277 fn eh_personality() -> ! {
278     loop {}
279 }
280
281 #[lang = "drop_in_place"]
282 #[allow(unconditional_recursion)]
283 pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
284     // Code here does not matter - this is replaced by the
285     // real drop glue by the compiler.
286     drop_in_place(to_drop);
287 }
288
289 #[lang = "owned_box"]
290 pub struct Box<T: ?Sized>(*mut T);
291
292 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Box<U>> for Box<T> {}
293
294 #[lang = "exchange_malloc"]
295 // Make it available to jited mini_core_hello_world
296 // FIXME remove next line when jit supports linking rlibs
297 #[inline(always)]
298 unsafe fn allocate(size: usize, _align: usize) -> *mut u8 {
299     libc::malloc(size)
300 }
301
302 #[lang = "drop"]
303 pub trait Drop {
304     fn drop(&mut self);
305 }
306
307 pub mod intrinsics {
308     extern "rust-intrinsic" {
309         pub fn abort() -> !;
310         pub fn size_of<T>() -> usize;
311         pub fn size_of_val<T: ?::Sized>(val: &T) -> usize;
312         pub fn min_align_of<T>() -> usize;
313         pub fn min_align_of_val<T: ?::Sized>(val: &T) -> usize;
314         pub fn copy<T>(src: *const T, dst: *mut T, count: usize);
315         pub fn transmute<T, U>(e: T) -> U;
316         pub fn uninit<T>() -> T;
317         pub fn ctlz_nonzero<T>(x: T) -> T;
318         pub fn needs_drop<T>() -> bool;
319     }
320 }
321
322 pub mod libc {
323     #[link(name = "c")]
324     extern "C" {
325         pub fn puts(s: *const u8);
326         pub fn malloc(size: usize) -> *mut u8;
327         pub fn memcpy(dst: *mut u8, src: *const u8, size: usize);
328         pub fn memmove(dst: *mut u8, src: *const u8, size: usize);
329         pub fn strncpy(dst: *mut u8, src: *const u8, size: usize);
330     }
331 }
332
333 #[lang = "index"]
334 pub trait Index<Idx: ?Sized> {
335     type Output: ?Sized;
336     fn index(&self, index: Idx) -> &Self::Output;
337 }
338
339 impl<T> Index<usize> for [T; 3] {
340     type Output = T;
341
342     fn index(&self, index: usize) -> &Self::Output {
343         &self[index]
344     }
345 }
346
347 impl<T> Index<usize> for [T] {
348     type Output = T;
349
350     fn index(&self, index: usize) -> &Self::Output {
351         &self[index]
352     }
353 }