]> git.lizzy.rs Git - rust.git/blob - example/mini_core.rs
Implement checked binops
[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 isize {
322     type Output = isize;
323
324     fn neg(self) -> isize {
325         -self
326     }
327 }
328
329 pub enum Option<T> {
330     Some(T),
331     None,
332 }
333
334 pub use Option::*;
335
336 #[lang = "phantom_data"]
337 pub struct PhantomData<T: ?Sized>;
338
339 #[lang = "fn_once"]
340 #[rustc_paren_sugar]
341 pub trait FnOnce<Args> {
342     type Output;
343
344     extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
345 }
346
347 #[lang = "fn_mut"]
348 #[rustc_paren_sugar]
349 pub trait FnMut<Args>: FnOnce<Args> {
350     extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output;
351 }
352
353 #[lang = "panic"]
354 // Make it available to jited mini_core_hello_world
355 // FIXME remove next line when jit supports linking rlibs
356 #[inline(always)]
357 pub fn panic(&(_msg, _file, _line, _col): &(&'static str, &'static str, u32, u32)) -> ! {
358     unsafe {
359         libc::puts("Panicking\0" as *const str as *const u8);
360         intrinsics::abort();
361     }
362 }
363
364 #[lang = "eh_personality"]
365 fn eh_personality() -> ! {
366     loop {}
367 }
368
369 #[lang = "drop_in_place"]
370 #[allow(unconditional_recursion)]
371 pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
372     // Code here does not matter - this is replaced by the
373     // real drop glue by the compiler.
374     drop_in_place(to_drop);
375 }
376
377 #[lang = "deref"]
378 pub trait Deref {
379     type Target: ?Sized;
380
381     fn deref(&self) -> &Self::Target;
382 }
383
384 #[lang = "owned_box"]
385 pub struct Box<T: ?Sized>(*mut T);
386
387 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Box<U>> for Box<T> {}
388
389 impl<T: ?Sized> Drop for Box<T> {
390     fn drop(&mut self) {
391         // drop is currently performed by compiler.
392     }
393 }
394
395 impl<T> Deref for Box<T> {
396     type Target = T;
397
398     fn deref(&self) -> &Self::Target {
399         &**self
400     }
401 }
402
403 #[lang = "exchange_malloc"]
404 // Make it available to jited mini_core_hello_world
405 // FIXME remove next line when jit supports linking rlibs
406 #[inline(always)]
407 unsafe fn allocate(size: usize, _align: usize) -> *mut u8 {
408     libc::malloc(size)
409 }
410
411 #[lang = "box_free"]
412 #[inline(always)]
413 unsafe fn box_free<T: ?Sized>(ptr: *mut T) {
414     libc::free(ptr as *mut u8);
415 }
416
417 #[lang = "drop"]
418 pub trait Drop {
419     fn drop(&mut self);
420 }
421
422 pub union MaybeUninit<T> {
423     pub uninit: (),
424     pub value: T,
425 }
426
427 pub mod intrinsics {
428     extern "rust-intrinsic" {
429         pub fn abort() -> !;
430         pub fn size_of<T>() -> usize;
431         pub fn size_of_val<T: ?::Sized>(val: &T) -> usize;
432         pub fn min_align_of<T>() -> usize;
433         pub fn min_align_of_val<T: ?::Sized>(val: &T) -> usize;
434         pub fn copy<T>(src: *const T, dst: *mut T, count: usize);
435         pub fn transmute<T, U>(e: T) -> U;
436         pub fn init<T>() -> T;
437         pub fn ctlz_nonzero<T>(x: T) -> T;
438         pub fn needs_drop<T>() -> bool;
439         pub fn bitreverse<T>(x: T) -> T;
440         pub fn bswap<T>(x: T) -> T;
441     }
442 }
443
444 pub mod libc {
445     #[link(name = "c")]
446     extern "C" {
447         pub fn puts(s: *const u8);
448         pub fn printf(format: *const i8, ...) -> i32;
449         pub fn malloc(size: usize) -> *mut u8;
450         pub fn free(ptr: *mut u8);
451         pub fn memcpy(dst: *mut u8, src: *const u8, size: usize);
452         pub fn memmove(dst: *mut u8, src: *const u8, size: usize);
453         pub fn strncpy(dst: *mut u8, src: *const u8, size: usize);
454     }
455 }
456
457 #[lang = "index"]
458 pub trait Index<Idx: ?Sized> {
459     type Output: ?Sized;
460     fn index(&self, index: Idx) -> &Self::Output;
461 }
462
463 impl<T> Index<usize> for [T; 3] {
464     type Output = T;
465
466     fn index(&self, index: usize) -> &Self::Output {
467         &self[index]
468     }
469 }
470
471 impl<T> Index<usize> for [T] {
472     type Output = T;
473
474     fn index(&self, index: usize) -> &Self::Output {
475         &self[index]
476     }
477 }
478
479 extern {
480     type VaListImpl;
481 }
482
483 #[lang = "va_list"]
484 #[repr(transparent)]
485 pub struct VaList<'a>(&'a mut VaListImpl);