rust/example/example.rs

209 lines
3.5 KiB
Rust
Raw Normal View History

#![feature(no_core, unboxed_closures)]
2018-06-17 16:05:11 +00:00
#![no_core]
#![allow(dead_code)]
2018-06-17 16:05:11 +00:00
2018-07-24 12:10:53 +00:00
extern crate mini_core;
2018-06-17 16:05:11 +00:00
2018-07-24 12:10:53 +00:00
use mini_core::*;
2018-07-20 12:20:37 +00:00
2020-07-11 09:02:23 +00:00
pub fn abc(a: u8) -> u8 {
2018-06-17 16:05:11 +00:00
a * 2
}
2020-07-11 09:02:23 +00:00
pub fn bcd(b: bool, a: u8) -> u8 {
2018-06-17 16:05:11 +00:00
if b {
a * 2
} else {
a * 3
}
}
2018-06-17 17:10:00 +00:00
2020-07-11 09:02:23 +00:00
pub fn call() {
2018-06-17 17:10:00 +00:00
abc(42);
}
2018-06-18 16:39:07 +00:00
2020-07-11 09:02:23 +00:00
pub fn indirect_call() {
2018-06-18 16:39:07 +00:00
let f: fn() = call;
f();
}
2018-06-23 16:26:54 +00:00
2020-07-11 09:02:23 +00:00
pub enum BoolOption {
2018-06-23 16:26:54 +00:00
Some(bool),
None,
}
2020-07-11 09:02:23 +00:00
pub fn option_unwrap_or(o: BoolOption, d: bool) -> bool {
2018-06-23 16:26:54 +00:00
match o {
BoolOption::Some(b) => b,
BoolOption::None => d,
}
}
2020-07-11 09:02:23 +00:00
pub fn ret_42() -> u8 {
2018-06-23 16:26:54 +00:00
42
2018-06-18 16:39:07 +00:00
}
2020-07-11 09:02:23 +00:00
pub fn return_str() -> &'static str {
"hello world"
}
2020-07-11 09:02:23 +00:00
pub fn promoted_val() -> &'static u8 {
&(1 * 2)
}
2020-07-11 09:02:23 +00:00
pub fn cast_ref_to_raw_ptr(abc: &u8) -> *const u8 {
abc as *const u8
}
2018-07-18 13:17:22 +00:00
2020-07-11 09:02:23 +00:00
pub fn cmp_raw_ptr(a: *const u8, b: *const u8) -> bool {
2018-07-18 13:17:22 +00:00
a == b
}
2018-07-18 14:22:29 +00:00
2020-07-11 09:02:23 +00:00
pub fn int_cast(a: u16, b: i16) -> (u8, u16, u32, usize, i8, i16, i32, isize, u8, u32) {
2018-07-18 14:22:29 +00:00
(
a as u8, a as u16, a as u32, a as usize, a as i8, a as i16, a as i32, a as isize, b as u8,
b as u32,
2018-07-18 14:22:29 +00:00
)
}
2018-07-18 15:07:10 +00:00
2020-07-11 09:02:23 +00:00
pub fn char_cast(c: char) -> u8 {
2018-07-19 17:37:34 +00:00
c as u8
}
pub struct DebugTuple(());
2018-07-18 15:07:10 +00:00
2020-07-11 09:02:23 +00:00
pub fn debug_tuple() -> DebugTuple {
2018-07-18 15:07:10 +00:00
DebugTuple(())
}
2018-07-20 12:20:37 +00:00
2020-07-11 09:02:23 +00:00
pub fn size_of<T>() -> usize {
2019-02-06 17:49:59 +00:00
intrinsics::size_of::<T>()
}
2020-07-11 09:02:23 +00:00
pub fn use_size_of() -> usize {
size_of::<u64>()
2018-07-20 12:20:37 +00:00
}
2020-07-11 09:02:23 +00:00
pub unsafe fn use_copy_intrinsic(src: *const u8, dst: *mut u8) {
intrinsics::copy::<u8>(src, dst, 1);
}
2020-07-11 09:02:23 +00:00
pub unsafe fn use_copy_intrinsic_ref(src: *const u8, dst: *mut u8) {
let copy2 = &intrinsics::copy::<u8>;
2018-07-20 12:20:37 +00:00
copy2(src, dst, 1);
}
2018-07-21 16:44:34 +00:00
2020-07-11 09:02:23 +00:00
pub const ABC: u8 = 6 * 7;
2018-07-21 16:44:34 +00:00
2020-07-11 09:02:23 +00:00
pub fn use_const() -> u8 {
2019-02-06 17:49:59 +00:00
ABC
2018-07-21 16:44:34 +00:00
}
2018-07-20 11:38:49 +00:00
pub fn call_closure_3arg() {
(|_, _, _| {})(0u8, 42u16, 0u8)
}
pub fn call_closure_2arg() {
(|_, _| {})(0u8, 42u16)
2018-07-20 11:38:49 +00:00
}
2018-07-26 08:48:50 +00:00
2020-07-11 09:02:23 +00:00
pub struct IsNotEmpty;
impl<'a, 'b> FnOnce<(&'a &'b [u16],)> for IsNotEmpty {
type Output = (u8, u8);
#[inline]
extern "rust-call" fn call_once(mut self, arg: (&'a &'b [u16],)) -> (u8, u8) {
self.call_mut(arg)
}
}
impl<'a, 'b> FnMut<(&'a &'b [u16],)> for IsNotEmpty {
#[inline]
2019-02-06 17:49:59 +00:00
extern "rust-call" fn call_mut(&mut self, _arg: (&'a &'b [u16],)) -> (u8, u8) {
(0, 42)
}
}
pub fn call_is_not_empty() {
IsNotEmpty.call_once((&(&[0u16] as &[_]),));
}
2020-07-11 09:02:23 +00:00
pub fn eq_char(a: char, b: char) -> bool {
2018-07-26 08:48:50 +00:00
a == b
}
2020-07-11 09:02:23 +00:00
pub unsafe fn transmute(c: char) -> u32 {
2018-07-26 08:48:50 +00:00
intrinsics::transmute(c)
}
2018-07-29 15:22:40 +00:00
2020-07-11 09:02:23 +00:00
pub unsafe fn deref_str_ptr(s: *const str) -> &'static str {
2018-08-08 08:39:10 +00:00
&*s
2018-08-22 13:38:10 +00:00
}
2018-08-08 10:22:16 +00:00
2020-07-11 09:02:23 +00:00
pub fn use_array(arr: [u8; 3]) -> u8 {
2018-08-08 10:22:16 +00:00
arr[1]
}
2018-08-08 10:30:25 +00:00
2020-07-11 09:02:23 +00:00
pub fn repeat_array() -> [u8; 3] {
2018-08-08 10:30:25 +00:00
[0; 3]
}
2020-07-11 09:02:23 +00:00
pub fn array_as_slice(arr: &[u8; 3]) -> &[u8] {
arr
}
2020-07-11 09:02:23 +00:00
pub unsafe fn use_ctlz_nonzero(a: u16) -> u16 {
intrinsics::ctlz_nonzero(a)
}
2018-08-09 09:07:10 +00:00
2020-07-11 09:02:23 +00:00
pub fn ptr_as_usize(ptr: *const u8) -> usize {
2018-08-09 09:07:10 +00:00
ptr as usize
}
2020-07-11 09:02:23 +00:00
pub fn float_cast(a: f32, b: f64) -> (f64, f32) {
(a as f64, b as f32)
}
2020-07-11 09:02:23 +00:00
pub fn int_to_float(a: u8, b: i32) -> (f64, f32) {
(a as f64, b as f32)
}
2020-07-11 09:02:23 +00:00
pub fn make_array() -> [u8; 3] {
[42, 0, 5]
}
2020-07-11 09:02:23 +00:00
pub fn some_promoted_tuple() -> &'static (&'static str, &'static str) {
&("abc", "some")
}
2018-08-25 09:22:48 +00:00
2020-07-11 09:02:23 +00:00
pub fn index_slice(s: &[u8]) -> u8 {
2018-08-25 09:22:48 +00:00
s[2]
}
pub struct StrWrapper {
s: str,
}
2020-07-11 09:02:23 +00:00
pub fn str_wrapper_get(w: &StrWrapper) -> &str {
&w.s
}
2020-07-11 09:02:23 +00:00
pub fn i16_as_i8(a: i16) -> i8 {
a as i8
}
2020-07-11 09:02:23 +00:00
pub struct Unsized(u8, str);
2020-07-11 09:02:23 +00:00
pub fn get_sized_field_ref_from_unsized_type(u: &Unsized) -> &u8 {
&u.0
}
2020-07-11 09:02:23 +00:00
pub fn get_unsized_field_ref_from_unsized_type(u: &Unsized) -> &str {
&u.1
}
pub fn reuse_byref_argument_storage(a: (u8, u16, u32)) -> u8 {
a.0
}