]> git.lizzy.rs Git - rust.git/blob - example/mini_core.rs
Fix some more stuff
[rust.git] / example / mini_core.rs
1 #![feature(no_core, lang_items, intrinsics, unboxed_closures, type_ascription, extern_types, untagged_unions)]
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 impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Box<U>> for Box<T> {}
31
32 #[lang = "receiver"]
33 pub trait Receiver {}
34
35 impl<T: ?Sized> Receiver for &T {}
36 impl<T: ?Sized> Receiver for &mut T {}
37 impl<T: ?Sized> Receiver for Box<T> {}
38
39 #[lang = "copy"]
40 pub unsafe trait Copy {}
41
42 unsafe impl Copy for bool {}
43 unsafe impl Copy for u8 {}
44 unsafe impl Copy for u16 {}
45 unsafe impl Copy for u32 {}
46 unsafe impl Copy for u64 {}
47 unsafe impl Copy for u128 {}
48 unsafe impl Copy for usize {}
49 unsafe impl Copy for i8 {}
50 unsafe impl Copy for i16 {}
51 unsafe impl Copy for i32 {}
52 unsafe impl Copy for i128 {}
53 unsafe impl Copy for isize {}
54 unsafe impl Copy for char {}
55 unsafe impl<'a, T: ?Sized> Copy for &'a T {}
56 unsafe impl<T: ?Sized> Copy for *const T {}
57 unsafe impl<T: ?Sized> Copy for *mut T {}
58
59 #[lang = "sync"]
60 pub unsafe trait Sync {}
61
62 unsafe impl Sync for bool {}
63 unsafe impl Sync for u8 {}
64 unsafe impl Sync for u16 {}
65 unsafe impl Sync for u32 {}
66 unsafe impl Sync for u64 {}
67 unsafe impl Sync for usize {}
68 unsafe impl Sync for i8 {}
69 unsafe impl Sync for i16 {}
70 unsafe impl Sync for i32 {}
71 unsafe impl Sync for isize {}
72 unsafe impl Sync for char {}
73 unsafe impl<'a, T: ?Sized> Sync for &'a T {}
74 unsafe impl Sync for [u8; 16] {}
75
76 #[lang = "freeze"]
77 trait Freeze {}
78
79 #[lang = "not"]
80 pub trait Not {
81     type Output;
82
83     fn not(self) -> Self::Output;
84 }
85
86 impl Not for bool {
87     type Output = bool;
88
89     fn not(self) -> bool {
90         !self
91     }
92 }
93
94 #[lang = "mul"]
95 pub trait Mul<RHS = Self> {
96     type Output;
97
98     #[must_use]
99     fn mul(self, rhs: RHS) -> Self::Output;
100 }
101
102 impl Mul for u8 {
103     type Output = Self;
104
105     fn mul(self, rhs: Self) -> Self::Output {
106         self * rhs
107     }
108 }
109
110 impl Mul for usize {
111     type Output = Self;
112
113     fn mul(self, rhs: Self) -> Self::Output {
114         self * rhs
115     }
116 }
117
118 #[lang = "add"]
119 pub trait Add<RHS = Self> {
120     type Output;
121
122     fn add(self, rhs: RHS) -> Self::Output;
123 }
124
125 impl Add for u8 {
126     type Output = Self;
127
128     fn add(self, rhs: Self) -> Self {
129         self + rhs
130     }
131 }
132
133 impl Add for i8 {
134     type Output = Self;
135
136     fn add(self, rhs: Self) -> Self {
137         self + rhs
138     }
139 }
140
141 impl Add for usize {
142     type Output = Self;
143
144     fn add(self, rhs: Self) -> Self {
145         self + rhs
146     }
147 }
148
149 impl Add for u128 {
150     type Output = Self;
151
152     fn add(self, rhs: Self) -> Self {
153         self + rhs
154     }
155 }
156
157 impl Add for i128 {
158     type Output = Self;
159
160     fn add(self, rhs: Self) -> Self {
161         self + rhs
162     }
163 }
164
165 #[lang = "sub"]
166 pub trait Sub<RHS = Self> {
167     type Output;
168
169     fn sub(self, rhs: RHS) -> Self::Output;
170 }
171
172 impl Sub for usize {
173     type Output = Self;
174
175     fn sub(self, rhs: Self) -> Self {
176         self - rhs
177     }
178 }
179
180 impl Sub for u8 {
181     type Output = Self;
182
183     fn sub(self, rhs: Self) -> Self {
184         self - rhs
185     }
186 }
187
188 impl Sub for i8 {
189     type Output = Self;
190
191     fn sub(self, rhs: Self) -> Self {
192         self - rhs
193     }
194 }
195
196 impl Sub for i16 {
197     type Output = Self;
198
199     fn sub(self, rhs: Self) -> Self {
200         self - rhs
201     }
202 }
203
204 #[lang = "bitor"]
205 pub trait BitOr<RHS = Self> {
206     type Output;
207
208     #[must_use]
209     fn bitor(self, rhs: RHS) -> Self::Output;
210 }
211
212 impl BitOr for bool {
213     type Output = bool;
214
215     fn bitor(self, rhs: bool) -> bool {
216         self | rhs
217     }
218 }
219
220 impl<'a> BitOr<bool> for &'a bool {
221     type Output = bool;
222
223     fn bitor(self, rhs: bool) -> bool {
224         *self | rhs
225     }
226 }
227
228 #[lang = "eq"]
229 pub trait PartialEq<Rhs: ?Sized = Self> {
230     fn eq(&self, other: &Rhs) -> bool;
231     fn ne(&self, other: &Rhs) -> bool;
232 }
233
234 impl PartialEq for u8 {
235     fn eq(&self, other: &u8) -> bool {
236         (*self) == (*other)
237     }
238     fn ne(&self, other: &u8) -> bool {
239         (*self) != (*other)
240     }
241 }
242
243 impl PartialEq for u16 {
244     fn eq(&self, other: &u16) -> bool {
245         (*self) == (*other)
246     }
247     fn ne(&self, other: &u16) -> bool {
248         (*self) != (*other)
249     }
250 }
251
252 impl PartialEq for u32 {
253     fn eq(&self, other: &u32) -> bool {
254         (*self) == (*other)
255     }
256     fn ne(&self, other: &u32) -> bool {
257         (*self) != (*other)
258     }
259 }
260
261
262 impl PartialEq for u64 {
263     fn eq(&self, other: &u64) -> bool {
264         (*self) == (*other)
265     }
266     fn ne(&self, other: &u64) -> bool {
267         (*self) != (*other)
268     }
269 }
270
271 impl PartialEq for usize {
272     fn eq(&self, other: &usize) -> bool {
273         (*self) == (*other)
274     }
275     fn ne(&self, other: &usize) -> bool {
276         (*self) != (*other)
277     }
278 }
279
280 impl PartialEq for i32 {
281     fn eq(&self, other: &i32) -> bool {
282         (*self) == (*other)
283     }
284     fn ne(&self, other: &i32) -> bool {
285         (*self) != (*other)
286     }
287 }
288
289 impl PartialEq for i128 {
290     fn eq(&self, other: &i128) -> bool {
291         (*self) == (*other)
292     }
293     fn ne(&self, other: &i128) -> bool {
294         (*self) != (*other)
295     }
296 }
297
298 impl PartialEq for isize {
299     fn eq(&self, other: &isize) -> bool {
300         (*self) == (*other)
301     }
302     fn ne(&self, other: &isize) -> bool {
303         (*self) != (*other)
304     }
305 }
306
307 impl PartialEq for char {
308     fn eq(&self, other: &char) -> bool {
309         (*self) == (*other)
310     }
311     fn ne(&self, other: &char) -> bool {
312         (*self) != (*other)
313     }
314 }
315
316 impl<T: ?Sized> PartialEq for *const T {
317     fn eq(&self, other: &*const T) -> bool {
318         *self == *other
319     }
320     fn ne(&self, other: &*const T) -> bool {
321         *self != *other
322     }
323 }
324
325 #[lang = "neg"]
326 pub trait Neg {
327     type Output;
328
329     fn neg(self) -> Self::Output;
330 }
331
332 impl Neg for i8 {
333     type Output = i8;
334
335     fn neg(self) -> i8 {
336         -self
337     }
338 }
339
340 impl Neg for i16 {
341     type Output = i16;
342
343     fn neg(self) -> i16 {
344         self
345     }
346 }
347
348 impl Neg for i128 {
349     type Output = i128;
350
351     fn neg(self) -> i128 {
352         -self
353     }
354 }
355
356 impl Neg for isize {
357     type Output = isize;
358
359     fn neg(self) -> isize {
360         -self
361     }
362 }
363
364 pub enum Option<T> {
365     Some(T),
366     None,
367 }
368
369 pub use Option::*;
370
371 #[lang = "phantom_data"]
372 pub struct PhantomData<T: ?Sized>;
373
374 #[lang = "fn_once"]
375 #[rustc_paren_sugar]
376 pub trait FnOnce<Args> {
377     type Output;
378
379     extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
380 }
381
382 #[lang = "fn_mut"]
383 #[rustc_paren_sugar]
384 pub trait FnMut<Args>: FnOnce<Args> {
385     extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output;
386 }
387
388 #[lang = "panic"]
389 // Make it available to jited mini_core_hello_world
390 // FIXME remove next line when jit supports linking rlibs
391 #[inline(always)]
392 pub fn panic(&(_msg, _file, _line, _col): &(&'static str, &'static str, u32, u32)) -> ! {
393     unsafe {
394         libc::puts("Panicking\0" as *const str as *const u8);
395         intrinsics::abort();
396     }
397 }
398
399 #[lang = "eh_personality"]
400 fn eh_personality() -> ! {
401     loop {}
402 }
403
404 #[lang = "drop_in_place"]
405 #[allow(unconditional_recursion)]
406 pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
407     // Code here does not matter - this is replaced by the
408     // real drop glue by the compiler.
409     drop_in_place(to_drop);
410 }
411
412 #[lang = "deref"]
413 pub trait Deref {
414     type Target: ?Sized;
415
416     fn deref(&self) -> &Self::Target;
417 }
418
419 #[lang = "owned_box"]
420 pub struct Box<T: ?Sized>(*mut T);
421
422 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Box<U>> for Box<T> {}
423
424 impl<T: ?Sized> Drop for Box<T> {
425     fn drop(&mut self) {
426         // drop is currently performed by compiler.
427     }
428 }
429
430 impl<T> Deref for Box<T> {
431     type Target = T;
432
433     fn deref(&self) -> &Self::Target {
434         &**self
435     }
436 }
437
438 #[lang = "exchange_malloc"]
439 // Make it available to jited mini_core_hello_world
440 // FIXME remove next line when jit supports linking rlibs
441 #[inline(always)]
442 unsafe fn allocate(size: usize, _align: usize) -> *mut u8 {
443     libc::malloc(size)
444 }
445
446 #[lang = "box_free"]
447 #[inline(always)]
448 unsafe fn box_free<T: ?Sized>(ptr: *mut T) {
449     libc::free(ptr as *mut u8);
450 }
451
452 #[lang = "drop"]
453 pub trait Drop {
454     fn drop(&mut self);
455 }
456
457 pub union MaybeUninit<T> {
458     pub uninit: (),
459     pub value: T,
460 }
461
462 pub mod intrinsics {
463     extern "rust-intrinsic" {
464         pub fn abort() -> !;
465         pub fn size_of<T>() -> usize;
466         pub fn size_of_val<T: ?::Sized>(val: &T) -> usize;
467         pub fn min_align_of<T>() -> usize;
468         pub fn min_align_of_val<T: ?::Sized>(val: &T) -> usize;
469         pub fn copy<T>(src: *const T, dst: *mut T, count: usize);
470         pub fn transmute<T, U>(e: T) -> U;
471         pub fn init<T>() -> T;
472         pub fn ctlz_nonzero<T>(x: T) -> T;
473         pub fn needs_drop<T>() -> bool;
474         pub fn bitreverse<T>(x: T) -> T;
475         pub fn bswap<T>(x: T) -> T;
476         pub fn unchecked_div<T>(lhs: T, rhs: T) -> T;
477     }
478 }
479
480 pub mod libc {
481     #[link(name = "c")]
482     extern "C" {
483         pub fn puts(s: *const u8);
484         pub fn printf(format: *const i8, ...) -> i32;
485         pub fn malloc(size: usize) -> *mut u8;
486         pub fn free(ptr: *mut u8);
487         pub fn memcpy(dst: *mut u8, src: *const u8, size: usize);
488         pub fn memmove(dst: *mut u8, src: *const u8, size: usize);
489         pub fn strncpy(dst: *mut u8, src: *const u8, size: usize);
490     }
491 }
492
493 #[lang = "index"]
494 pub trait Index<Idx: ?Sized> {
495     type Output: ?Sized;
496     fn index(&self, index: Idx) -> &Self::Output;
497 }
498
499 impl<T> Index<usize> for [T; 3] {
500     type Output = T;
501
502     fn index(&self, index: usize) -> &Self::Output {
503         &self[index]
504     }
505 }
506
507 impl<T> Index<usize> for [T] {
508     type Output = T;
509
510     fn index(&self, index: usize) -> &Self::Output {
511         &self[index]
512     }
513 }
514
515 extern {
516     type VaListImpl;
517 }
518
519 #[lang = "va_list"]
520 #[repr(transparent)]
521 pub struct VaList<'a>(&'a mut VaListImpl);