Getting an Array from Matrix4

According to this issue:

This should work:

let mat:Matrix4<f32> = Matrix4::identity();
let values:[f32;16] = mat.into();

But it doesn’t… am I do something wrong? Is there a way to get the underlying stack-allocated matrix values as an array?

Ah okay I think I need to use as_ref(), but for Matrix4 (for example) that gives me back a &[[f32;4]; 4] instead of a &[f32;16] … any tips for getting to the next step?

nm… just realized that doesn’t really make sense since an Array can’t be moved and so can only be passed by reference. Sorry!

The as_ref() seems a little magical in that regard, I have a lot to learn about Rust :sweat_smile:


The Into trait is only implemented for [[f32; 4]; 4] so you can do let values: [[f32; 4]; 4] = mat.into(). I guess we could also implement it for [f32; 16].

Hmm but I see it’s using mem::transmute() under the hood… does that not do a copy?

Transmute may copy, yes. So transmuting a reference & may do a copy of the reference (but not of what it points to).

1 Like