Rust Structs don't appear in the correct order as a pointer

Hello, I've been trying to use Rust with OpenGL but noticed a weird issue.

Normally, when using OpenGL with another language like C you could directly pass a struct like:

struct xyz{
  glm::vec3 thing,
  glm::vec3 thing2,
  glm::vec2 thing3,
}

and then OpenGL could interpret the struct as

vec3 thing,
vec3 thing2,
vec2 thing3

However, attempting to do this in rust results in some weird behavior.

If I store my struct xyz in a vector

struct Xyz{
  thing: glm::Vec3,
  thing2: glm::Vec3,
  thing3: glm::Vec2
}

let stuff: Vec<xyz> = std::Vec![ /* DATA */ ];

then the data that get's passed to OpenGL looks like:


stuff.as_ptr() -> passed to an OpenGL (C) function
OpenGL reads as the first element: thing3[0], thing3[1], thing[0], thing[2] (a strange order)

How can I get rust to order the data in a struct the same way it is ordered in the definition such that I could pass it to a C library and get the expected behavior?

I forgot about reprs

answer:

#[repr(C)]
struct Xyz{
  thing: glm::Vec3,
  thing2: glm::Vec3,
  thing3: glm::Vec2
}
1 Like