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