1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use crate::math::traits::{MathContainer, AsSliceExt};

//all the as_slice impls are very fast, no copy

macro_rules! impl_as_slice_repr {
    ( $( $x:ty ),* ) => {
        $(
            impl AsSliceExt for $x {
                fn as_slice(&self) -> &[f64] {
                    let pointer = self as *const Self as *const f64;
                    let slice: &[f64] = unsafe { std::slice::from_raw_parts(pointer, self.len()) };
                    slice
                }

                fn as_slice_mut(&mut self) -> &mut [f64] {
                    let pointer = self as *const Self as *mut f64;
                    let slice: &mut [f64] = unsafe { std::slice::from_raw_parts_mut(pointer, self.len()) };
                    slice
                }
            }
        )*
    };
}

macro_rules! impl_as_slice_backing_data {
    ( $( $x:ty ),* ) => {
        $(
            impl AsSliceExt for $x {
                fn as_slice(&self) -> &[f64] {
                    &self.0
                }

                fn as_slice_mut(&mut self) -> &mut [f64] {
                    &mut self.0
                }
            }
        )*
    };
}
impl_as_slice_repr!{super::vec3::Vec3, super::quat::Quat}
impl_as_slice_backing_data!{super::matrix::Matrix4}