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