]> git.lizzy.rs Git - rust.git/blob - example/mini_core.rs
Use cranelift_frontend::Switch for switchInt
[rust.git] / example / mini_core.rs
1 #![feature(no_core, lang_items, intrinsics, unboxed_closures)]
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<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *const T {}
16 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for *mut T {}
17
18 #[lang = "copy"]
19 pub unsafe trait Copy {}
20
21 unsafe impl Copy for bool {}
22 unsafe impl Copy for u8 {}
23 unsafe impl Copy for u16 {}
24 unsafe impl Copy for u32 {}
25 unsafe impl Copy for u64 {}
26 unsafe impl Copy for usize {}
27 unsafe impl Copy for i8 {}
28 unsafe impl Copy for i16 {}
29 unsafe impl Copy for i32 {}
30 unsafe impl Copy for isize {}
31 unsafe impl Copy for char {}
32 unsafe impl<'a, T: ?Sized> Copy for &'a T {}
33 unsafe impl<T: ?Sized> Copy for *const T {}
34
35 #[lang = "sync"]
36 pub unsafe trait Sync {}
37
38 unsafe impl Sync for bool {}
39 unsafe impl Sync for u8 {}
40 unsafe impl Sync for u16 {}
41 unsafe impl Sync for u32 {}
42 unsafe impl Sync for u64 {}
43 unsafe impl Sync for usize {}
44 unsafe impl Sync for i8 {}
45 unsafe impl Sync for i16 {}
46 unsafe impl Sync for i32 {}
47 unsafe impl Sync for isize {}
48 unsafe impl Sync for char {}
49 unsafe impl<'a, T: ?Sized> Sync for &'a T {}
50 unsafe impl Sync for [u8; 16] {}
51
52 #[lang = "freeze"]
53 trait Freeze {}
54
55 #[lang = "not"]
56 pub trait Not {
57     type Output;
58
59     fn not(self) -> Self::Output;
60 }
61
62 impl Not for bool {
63     type Output = bool;
64
65     fn not(self) -> bool {
66         !self
67     }
68 }
69
70 #[lang = "mul"]
71 pub trait Mul<RHS = Self> {
72     type Output;
73
74     #[must_use]
75     fn mul(self, rhs: RHS) -> Self::Output;
76 }
77
78 impl Mul for u8 {
79     type Output = Self;
80
81     fn mul(self, rhs: Self) -> Self::Output {
82         self * rhs
83     }
84 }
85
86 #[lang = "add"]
87 pub trait Add<RHS = Self> {
88     type Output;
89
90     fn add(self, rhs: RHS) -> Self::Output;
91 }
92
93 impl Add for u8 {
94     type Output = Self;
95
96     fn add(self, rhs: Self) -> Self {
97         self + rhs
98     }
99 }
100
101 #[lang = "sub"]
102 pub trait Sub<RHS = Self> {
103     type Output;
104
105     fn sub(self, rhs: RHS) -> Self::Output;
106 }
107
108 impl Sub for usize {
109     type Output = Self;
110
111     fn sub(self, rhs: Self) -> Self {
112         self - rhs
113     }
114 }
115
116 #[lang = "bitor"]
117 pub trait BitOr<RHS = Self> {
118     type Output;
119
120     #[must_use]
121     fn bitor(self, rhs: RHS) -> Self::Output;
122 }
123
124 impl BitOr for bool {
125     type Output = bool;
126
127     fn bitor(self, rhs: bool) -> bool {
128         self | rhs
129     }
130 }
131
132 impl<'a> BitOr<bool> for &'a bool {
133     type Output = bool;
134
135     fn bitor(self, rhs: bool) -> bool {
136         *self | rhs
137     }
138 }
139
140 #[lang = "eq"]
141 pub trait PartialEq<Rhs: ?Sized = Self> {
142     fn eq(&self, other: &Rhs) -> bool;
143     fn ne(&self, other: &Rhs) -> bool;
144 }
145
146 impl PartialEq for u8 {
147     fn eq(&self, other: &u8) -> bool {
148         (*self) == (*other)
149     }
150     fn ne(&self, other: &u8) -> bool {
151         (*self) != (*other)
152     }
153 }
154
155 impl PartialEq for char {
156     fn eq(&self, other: &char) -> bool {
157         (*self) == (*other)
158     }
159     fn ne(&self, other: &char) -> bool {
160         (*self) != (*other)
161     }
162 }
163
164 impl<T: ?Sized> PartialEq for *const T {
165     fn eq(&self, other: &*const T) -> bool {
166         *self == *other
167     }
168     fn ne(&self, other: &*const T) -> bool {
169         *self != *other
170     }
171 }
172
173 #[lang = "neg"]
174 pub trait Neg {
175     type Output;
176
177     fn neg(self) -> Self::Output;
178 }
179
180 impl Neg for isize {
181     type Output = isize;
182
183     fn neg(self) -> isize {
184         -self
185     }
186 }
187
188 pub enum Option<T> {
189     Some(T),
190     None,
191 }
192
193 pub use Option::*;
194
195 #[lang = "phantom_data"]
196 pub struct PhantomData<T: ?Sized>;
197
198 #[lang = "fn_once"]
199 #[rustc_paren_sugar]
200 pub trait FnOnce<Args> {
201     type Output;
202
203     extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
204 }
205
206 #[lang = "fn_mut"]
207 #[rustc_paren_sugar]
208 pub trait FnMut<Args>: FnOnce<Args> {
209     extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output;
210 }
211
212 #[lang = "panic"]
213 pub fn panic(_expr_file_line_col: &(&'static str, &'static str, u32, u32)) -> ! {
214     unsafe {
215         intrinsics::abort();
216     }
217 }
218
219 #[lang = "eh_personality"]
220 fn eh_personality() -> ! {
221     loop {}
222 }
223
224 #[lang = "drop_in_place"]
225 #[allow(unconditional_recursion)]
226 pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
227     // Code here does not matter - this is replaced by the
228     // real drop glue by the compiler.
229     drop_in_place(to_drop);
230 }
231
232 #[lang = "owned_box"]
233 pub struct Box<T: ?Sized>(*mut T);
234
235 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Box<U>> for Box<T> {}
236
237 static mut MY_TINY_HEAP: [u8; 16] = [0; 16];
238
239 #[lang = "exchange_malloc"]
240 unsafe fn allocate(size: usize, _align: usize) -> *mut u8 {
241     &mut MY_TINY_HEAP as *mut [u8; 16] as *mut u8
242 }
243
244 #[lang = "drop"]
245 pub trait Drop {
246     fn drop(&mut self);
247 }
248
249 pub mod intrinsics {
250     extern "rust-intrinsic" {
251         pub fn abort() -> !;
252         pub fn size_of<T>() -> usize;
253         pub fn size_of_val<T: ?::Sized>(val: &T) -> usize;
254         pub fn min_align_of<T>() -> usize;
255         pub fn min_align_of_val<T: ?::Sized>(val: &T) -> usize;
256         pub fn copy<T>(src: *const T, dst: *mut T, count: usize);
257         pub fn transmute<T, U>(e: T) -> U;
258         pub fn uninit<T>() -> T;
259         pub fn ctlz_nonzero<T>(x: T) -> T;
260         pub fn needs_drop<T>() -> bool;
261     }
262 }
263
264 #[lang = "index"]
265 pub trait Index<Idx: ?Sized> {
266     type Output: ?Sized;
267     fn index(&self, index: Idx) -> &Self::Output;
268 }
269
270 impl<T> Index<usize> for [T; 3] {
271     type Output = T;
272
273     fn index(&self, index: usize) -> &Self::Output {
274         &self[index]
275     }
276 }
277
278 impl<T> Index<usize> for [T] {
279     type Output = T;
280
281     fn index(&self, index: usize) -> &Self::Output {
282         &self[index]
283     }
284 }