rust/example.rs

173 lines
3.1 KiB
Rust
Raw Normal View History

2018-07-20 07:20:37 -05:00
#![feature(no_core, lang_items, intrinsics)]
2018-06-17 11:05:11 -05:00
#![no_core]
#![allow(dead_code)]
2018-06-17 11:05:11 -05:00
#[lang="sized"]
pub trait Sized {}
2018-06-17 11:05:11 -05:00
#[lang="copy"]
2018-07-18 08:17:22 -05:00
unsafe trait Copy {}
unsafe impl Copy for u8 {}
2018-07-18 09:22:29 -05:00
unsafe impl Copy for u16 {}
unsafe impl Copy for u32 {}
unsafe impl Copy for u64 {}
unsafe impl Copy for usize {}
unsafe impl Copy for i8 {}
unsafe impl Copy for i16 {}
unsafe impl Copy for i32 {}
unsafe impl Copy for isize {}
2018-07-18 08:17:22 -05:00
unsafe impl<'a, T: ?Sized> Copy for &'a T {}
unsafe impl<T: ?Sized> Copy for *const T {}
2018-06-17 11:05:11 -05:00
#[lang="freeze"]
trait Freeze {}
#[lang="mul"]
trait Mul<RHS = Self> {
type Output;
#[must_use]
fn mul(self, rhs: RHS) -> Self::Output;
}
impl Mul for u8 {
type Output = Self;
2018-06-17 11:05:11 -05:00
fn mul(self, rhs: Self) -> Self {
2018-06-17 11:05:11 -05:00
self * rhs
}
}
2018-07-18 08:17:22 -05:00
#[lang = "eq"]
pub trait PartialEq<Rhs: ?Sized = Self> {
fn eq(&self, other: &Rhs) -> bool;
fn ne(&self, other: &Rhs) -> bool;
}
impl PartialEq for u8 {
fn eq(&self, other: &u8) -> bool { (*self) == (*other) }
fn ne(&self, other: &u8) -> bool { (*self) != (*other) }
}
impl<T: ?Sized> PartialEq for *const T {
fn eq(&self, other: &*const T) -> bool { *self == *other }
fn ne(&self, other: &*const T) -> bool { *self != *other }
}
2018-06-17 11:05:11 -05:00
#[lang="panic"]
fn panic(_expr_file_line_col: &(&'static str, &'static str, u32, u32)) -> ! {
loop {}
}
2018-06-23 11:26:54 -05:00
#[lang = "drop_in_place"]
#[allow(unconditional_recursion)]
unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
// Code here does not matter - this is replaced by the
// real drop glue by the compiler.
drop_in_place(to_drop);
}
mod intrinsics {
extern "rust-intrinsic" {
pub fn size_of<T>() -> usize;
pub fn copy<T>(src: *const T, dst: *mut T, count: usize);
}
2018-07-20 07:20:37 -05:00
}
fn abc(a: u8) -> u8 {
2018-06-17 11:05:11 -05:00
a * 2
}
fn bcd(b: bool, a: u8) -> u8 {
2018-06-17 11:05:11 -05:00
if b {
a * 2
} else {
a * 3
}
}
2018-06-17 12:10:00 -05:00
2018-06-23 11:26:54 -05:00
// FIXME make calls work
fn call() {
2018-06-17 12:10:00 -05:00
abc(42);
}
2018-06-18 11:39:07 -05:00
fn indirect_call() {
let f: fn() = call;
f();
}
2018-06-23 11:26:54 -05:00
enum BoolOption {
Some(bool),
None,
}
fn option_unwrap_or(o: BoolOption, d: bool) -> bool {
match o {
BoolOption::Some(b) => b,
BoolOption::None => d,
}
}
fn ret_42() -> u8 {
2018-06-23 11:26:54 -05:00
42
2018-06-18 11:39:07 -05:00
}
fn return_str() -> &'static str {
"hello world"
}
fn promoted_val() -> &'static u8 {
&(1 * 2)
}
fn cast_ref_to_raw_ptr(abc: &u8) -> *const u8 {
abc as *const u8
}
2018-07-18 08:17:22 -05:00
fn cmp_raw_ptr(a: *const u8, b: *const u8) -> bool {
a == b
}
2018-07-18 09:22:29 -05:00
fn int_cast(a: u16, b: i16) -> (u8, u16, u32, usize, i8, i16, i32, isize) {
(
a as u8,
a as u16,
a as u32,
a as usize,
a as i8,
a as i16,
a as i32,
a as isize,
)
}
2018-07-18 10:07:10 -05:00
2018-07-19 12:37:34 -05:00
fn char_cast(c: char) -> u8 {
c as u8
}
2018-07-18 10:07:10 -05:00
struct DebugTuple(());
fn debug_tuple() -> DebugTuple {
DebugTuple(())
}
2018-07-20 07:20:37 -05:00
fn size_of<T>() -> usize {
unsafe {
intrinsics::size_of::<T>()
}
}
fn use_size_of() -> usize {
size_of::<u64>()
2018-07-20 07:20:37 -05:00
}
/*unsafe fn use_copy_intrinsic(src: *const u8, dst: *mut u8) {
intrinsics::copy::<u8>(src, dst, 1);
}*/
2018-07-20 07:20:37 -05:00
/*unsafe fn use_copy_intrinsic_ref(src: *const u8, dst: *mut u8) {
let copy2 = &copy::<u8>;
copy2(src, dst, 1);
}*/