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