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