Skip to content

Instantly share code, notes, and snippets.

@Ciantic
Created April 14, 2021 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ciantic/89b9f84c97b5dfe21dbe342200d3bd4d to your computer and use it in GitHub Desktop.
Save Ciantic/89b9f84c97b5dfe21dbe342200d3bd4d to your computer and use it in GitHub Desktop.
Tuple layout
#[repr(C)]
struct Foo(f32, f32, f32);
fn main() {
let a = [Foo(1.0,2.0,3.0), Foo(4.0,5.0,6.0)];
let b = [1.0f32,2.0,3.0,4.0,5.0,6.0];
let c = [(1.0f32,2.0f32,3.0f32), (4.0f32,5.0f32,6.0f32)];
let amem = unsafe { std::slice::from_raw_parts((&a as *const _) as *const u8, std::mem::size_of_val(&a) )};
let bmem = unsafe { std::slice::from_raw_parts((&b as *const _) as *const u8, std::mem::size_of_val(&b) )};
let cmem = unsafe { std::slice::from_raw_parts((&c as *const _) as *const u8, std::mem::size_of_val(&c) )};
// These are all equal
println!("{:?}", amem);
println!("{:?}", bmem);
println!("{:?}", cmem);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment