Rust, repr, struct


#1

I have the following struct


pub struct Float3 {
    pub x: f32,
    pub y: f32,
    put z: f32,
}


I need the following properties:

In a Vec with n elements,

x's are at:data[3*i+0]
y's are at data[3*i+1]
z's are at data[3*i+1]

I need this in order I can easily pass the entire vec to OpenGL.

Question: what repl tag / other config I need to do on the struct to ensure this?


#2

assuming that you meant z's are at data[3*i+2] and pub z: f32,

Would #[repr(C)] do the trick? I did a small test on playground and it looks like it will.


#3

Sorry, yes, z at [3i+2]


#4

Do I just do it like:


#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct Float3 {
    pub x: f32,
    pub y: f32,
    pub z: f32,
}

?


#5

Yes, that should work


#6

In general when workokg with ffi code, anything that needs to be used by another language should be #[repr(C)], because the default representation is unspecified. You can see more about this in the rust nomicon.