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