Skip to content

Instantly share code, notes, and snippets.

@aosoft
Created September 18, 2020 16:49
Show Gist options
  • Save aosoft/41e3dd2975039287acaef12413a445ca to your computer and use it in GitHub Desktop.
Save aosoft/41e3dd2975039287acaef12413a445ca to your computer and use it in GitHub Desktop.
Rust Representation / Function pointer
macro_rules! size_of {
($x:ty) => {
(stringify!($x), std::mem::size_of::<$x>())
};
}
pub enum EnumA<T> {
A(T),
B,
}
pub enum EnumB<T> {
A(T),
B,
C,
}
#[repr(i8)]
pub enum EnumC<T, U> {
A(T),
B(U),
C,
}
pub enum EnumD {
A(i32, i32),
B(i64),
C,
}
pub enum EnumE {
A(i32, i32),
B(i64, i32),
C,
}
pub enum EnumF {
A(i32, i32),
B(i64, i64),
C,
}
#[repr(packed(2))]
pub struct StructG {
a: i8,
b: i16,
c: i32,
}
pub enum EnumG {
A(StructG),
B,
}
#[repr(align(2))]
pub struct StructH {
a: i8,
b: i16,
c: i32,
}
pub enum EnumH {
A(StructH),
B,
}
pub struct StructI1 {
a: i8,
b: i16,
c: i32,
}
#[repr(C)]
pub struct StructI2 {
a: i8,
b: i16,
c: i32,
}
fn main() {
let s = [
size_of!(i8),
size_of!(Option<i8>),
size_of!(i16),
size_of!(Option<i16>),
size_of!(i32),
size_of!(Option<i32>),
size_of!(i64),
size_of!(Option<i64>),
size_of!(EnumA<i64>),
size_of!(EnumB<i64>),
size_of!(*const i64),
size_of!(Option<*const i64>),
size_of!(EnumA<*const i64>),
size_of!(EnumB<*const i64>),
size_of!(&i64),
size_of!(Option<&i64>),
size_of!(EnumA<&i64>),
size_of!(EnumB<&i64>),
size_of!(()),
size_of!(Option<()>),
size_of!(fn()),
size_of!(Option<fn()>),
size_of!(&fn()),
size_of!(Option<&fn()>),
size_of!(*const fn()),
size_of!(Option<*const fn()>),
size_of!(Box<fn()>),
size_of!(Option<Box<fn()>>),
size_of!(Box<dyn Fn()>),
size_of!(Option<Box<dyn Fn()>>),
size_of!(EnumC<i32, i32>),
size_of!(EnumC<i32, i64>),
size_of!(EnumC<i64, i32>),
size_of!(EnumC<&i32, &i32>),
size_of!(EnumC<&i32, &i64>),
size_of!(EnumD),
size_of!(EnumE),
size_of!(EnumF),
size_of!(StructG),
size_of!(EnumG),
size_of!(StructH),
size_of!(EnumH),
size_of!(StructI1),
size_of!(StructI2),
];
s.iter().for_each(|x| println!("{:24} size = {}", x.0, x.1));
unsafe {
let a = StructI1 {
a: 7,
b: 8,
c: 9
};
let b = StructI2 {
a: 7,
b: 8,
c: 9
};
let p1: *const _ = &sum;
let p2 = sum as *const ();
let p3 = std::mem::transmute::<_, Option::<fn(i32, i32) -> i32>>(p2).unwrap();
println!("{}", (*p1)(10, 10));
println!("{}", p3(20, 20));
}
}
fn sum(a: i32, b: i32) -> i32 {
a + b
}
@aosoft
Copy link
Author

aosoft commented Sep 18, 2020

Result (stable-x86_64-pc-windows-msvc):

i8                       size = 1
Option<i8>               size = 2
i16                      size = 2
Option<i16>              size = 4
i32                      size = 4
Option<i32>              size = 8
i64                      size = 8
Option<i64>              size = 16
EnumA<i64>               size = 16
EnumB<i64>               size = 16
*const i64               size = 8
Option<*const i64>       size = 16
EnumA<*const i64>        size = 16
EnumB<*const i64>        size = 16
&i64                     size = 8
Option<&i64>             size = 8
EnumA<&i64>              size = 8
EnumB<&i64>              size = 16
()                       size = 0
Option<()>               size = 1
fn()                     size = 8
Option<fn()>             size = 8
&fn()                    size = 8
Option<&fn()>            size = 8
*const fn()              size = 8
Option<*const fn()>      size = 16
Box<fn()>                size = 8
Option<Box<fn()>>        size = 8
Box<dyn Fn()>            size = 16
Option<Box<dyn Fn()>>    size = 16
EnumC<i32, i32>          size = 8
EnumC<i32, i64>          size = 16
EnumC<i64, i32>          size = 16
EnumC<&i32, &i32>        size = 16
EnumC<&i32, &i64>        size = 16
EnumD                    size = 16
EnumE                    size = 16
EnumF                    size = 24
StructG                  size = 8
EnumG                    size = 10
StructH                  size = 8
EnumH                    size = 12
StructI1                 size = 8
StructI2                 size = 8
20
40

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment