]> git.lizzy.rs Git - rust.git/blob - example.rs
Implement int casts
[rust.git] / example.rs
1 #![feature(no_core, lang_items)]
2 #![no_core]
3 #![allow(dead_code)]
4
5 #[lang="sized"]
6 trait Sized {}
7
8 #[lang="copy"]
9 unsafe trait Copy {}
10
11 unsafe impl Copy for u8 {}
12 unsafe impl Copy for u16 {}
13 unsafe impl Copy for u32 {}
14 unsafe impl Copy for u64 {}
15 unsafe impl Copy for usize {}
16 unsafe impl Copy for i8 {}
17 unsafe impl Copy for i16 {}
18 unsafe impl Copy for i32 {}
19 unsafe impl Copy for isize {}
20 unsafe impl<'a, T: ?Sized> Copy for &'a T {}
21 unsafe impl<T: ?Sized> Copy for *const T {}
22
23 #[lang="freeze"]
24 trait Freeze {}
25
26 #[lang="mul"]
27 trait Mul<RHS = Self> {
28     type Output;
29
30     #[must_use]
31     fn mul(self, rhs: RHS) -> Self::Output;
32 }
33
34 impl Mul for u8 {
35     type Output = Self;
36
37     fn mul(self, rhs: Self) -> Self {
38         self * rhs
39     }
40 }
41
42 #[lang = "eq"]
43 pub trait PartialEq<Rhs: ?Sized = Self> {
44     fn eq(&self, other: &Rhs) -> bool;
45     fn ne(&self, other: &Rhs) -> bool;
46 }
47
48 impl PartialEq for u8 {
49     fn eq(&self, other: &u8) -> bool { (*self) == (*other) }
50     fn ne(&self, other: &u8) -> bool { (*self) != (*other) }
51 }
52
53 impl<T: ?Sized> PartialEq for *const T {
54     fn eq(&self, other: &*const T) -> bool { *self == *other }
55     fn ne(&self, other: &*const T) -> bool { *self != *other }
56 }
57
58 #[lang="panic"]
59 fn panic(_expr_file_line_col: &(&'static str, &'static str, u32, u32)) -> ! {
60     loop {}
61 }
62
63 #[lang = "drop_in_place"]
64 #[allow(unconditional_recursion)]
65 unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
66     // Code here does not matter - this is replaced by the
67     // real drop glue by the compiler.
68     drop_in_place(to_drop);
69 }
70
71 fn abc(a: u8) -> u8 {
72     a * 2
73 }
74
75 fn bcd(b: bool, a: u8) -> u8 {
76     if b {
77         a * 2
78     } else {
79         a * 3
80     }
81 }
82
83 // FIXME make calls work
84 fn call() {
85     abc(42);
86 }
87
88 fn indirect_call() {
89     let f: fn() = call;
90     f();
91 }
92
93 enum BoolOption {
94     Some(bool),
95     None,
96 }
97
98 fn option_unwrap_or(o: BoolOption, d: bool) -> bool {
99     match o {
100         BoolOption::Some(b) => b,
101         BoolOption::None => d,
102     }
103 }
104
105 fn ret_42() -> u8 {
106     42
107 }
108
109 fn return_str() -> &'static str {
110     "hello world"
111 }
112
113 fn promoted_val() -> &'static u8 {
114     &(1 * 2)
115 }
116
117 fn cast_ref_to_raw_ptr(abc: &u8) -> *const u8 {
118     abc as *const u8
119 }
120
121 fn cmp_raw_ptr(a: *const u8, b: *const u8) -> bool {
122     a == b
123 }
124
125 fn int_cast(a: u16, b: i16) -> (u8, u16, u32, usize, i8, i16, i32, isize) {
126     (
127         a as u8,
128         a as u16,
129         a as u32,
130         a as usize,
131         a as i8,
132         a as i16,
133         a as i32,
134         a as isize,
135     )
136 }