Created
April 25, 2016 17:49
-
-
Save pravic/7b72019ae8d1efe4c09b30e0cc54a9a2 to your computer and use it in GitHub Desktop.
size-of-option
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::mem::size_of; | |
#[repr(C)] | |
pub enum POption<T> { | |
PSome(T), | |
} | |
#[repr(C)] | |
pub struct SOption<T>(T); | |
#[repr(C)] | |
struct S | |
{ | |
mem1: i32, | |
o1: Option<i32>, | |
o2: Option<*const i32>, | |
o3: Option<extern "system" fn (i32, i32) -> i32>, | |
} | |
extern "system" fn func1(arg: *const i32) -> i32 { | |
unsafe { *arg + 1 } | |
} | |
extern "system" fn func2(arg: &i32) -> i32 { | |
*arg + 1 | |
} | |
extern "system" fn func3(arg: Option<i32>) -> i32 { | |
if let Some(x) = arg { | |
x + 1 | |
} else { | |
-8 | |
} | |
} | |
extern "system" fn func4(arg: Option<*const i32>) -> i32 { | |
if let Some(x) = arg { | |
unsafe { *x + 1 } | |
} else { | |
-8 | |
} | |
} | |
extern "system" fn func5(arg: &S) -> i32 { | |
let mut ret = 0; | |
ret += arg.mem1; | |
if let Some(x) = arg.o1 { | |
ret += x; | |
} | |
if let Some(x) = arg.o2 { | |
ret += unsafe { *x }; | |
} | |
return ret; | |
} | |
fn main() | |
{ | |
println!("sizeof S {}", size_of::<S>()); | |
println!("sizeof Option<i32> {}", size_of::<Option<i32>>()); | |
println!("sizeof Option<&i32> {}", size_of::<Option<&i32>>()); | |
println!("sizeof Option<*i32> {}", size_of::<Option<*const i32>>()); | |
println!("sizeof Option<fn> {}\n", size_of::<Option<extern "system" fn () -> i32>>()); | |
println!("sizeof POption<i32> {}", size_of::<POption<i32>>()); | |
println!("sizeof POption<&i32> {}", size_of::<POption<&i32>>()); | |
println!("sizeof POption<*i32> {}", size_of::<POption<*const i32>>()); | |
println!("sizeof POption<fn> {}\n", size_of::<POption<extern "system" fn () -> i32>>()); | |
println!("sizeof SOption<i32> {}", size_of::<SOption<i32>>()); | |
println!("sizeof SOption<&i32> {}", size_of::<SOption<&i32>>()); | |
println!("sizeof SOption<*i32> {}", size_of::<SOption<*const i32>>()); | |
println!("sizeof SOption<fn> {}\n", size_of::<SOption<extern "system" fn () -> i32>>()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment