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