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