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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use std::convert::{AsMut, AsRef, From, Into};
use std::mem::{self, MaybeUninit};
use std::ptr;

use crate::base::allocator::Allocator;
use crate::base::dimension::{Const, DimName, U1, U2, U3, U4};
use crate::base::storage::{IsContiguous, RawStorage, RawStorageMut};
use crate::base::{DefaultAllocator, Matrix, OMatrix, Scalar};

macro_rules! impl_from_into_mint_1D(
    ($($NRows: ident => $VT:ident [$SZ: expr]);* $(;)*) => {$(
        impl<T> From<mint::$VT<T>> for OMatrix<T, $NRows, U1>
        where T: Scalar,
              DefaultAllocator: Allocator<T, $NRows, U1> {
            #[inline]
            fn from(v: mint::$VT<T>) -> Self {
                unsafe {
                    let mut res = Matrix::uninit(<$NRows>::name(), Const::<1>);
                    // Copy the data.
                    ptr::copy_nonoverlapping(&v.x, res.data.ptr_mut() as *mut T, $SZ);
                    // Prevent from being dropped the originals we just copied.
                    mem::forget(v);
                    // The result is now fully initialized.
                    res.assume_init()
                }
            }
        }

        impl<T, S> Into<mint::$VT<T>> for Matrix<T, $NRows, U1, S>
        where T: Scalar,
              S: RawStorage<T, $NRows, U1> + IsContiguous {
            #[inline]
            fn into(self) -> mint::$VT<T> {
                // SAFETY: this is OK thanks to the IsContiguous bound.
                unsafe {
                    let mut res: MaybeUninit<mint::$VT<T>> = MaybeUninit::uninit();
                    // Copy the data.
                    ptr::copy_nonoverlapping(self.data.ptr(), res.as_mut_ptr() as *mut T, $SZ);
                    // Prevent from being dropped the originals we just copied.
                    mem::forget(self);
                    // The result is now fully initialized.
                    res.assume_init()
                }
            }
        }

        impl<T, S> AsRef<mint::$VT<T>> for Matrix<T, $NRows, U1, S>
        where T: Scalar,
              S: RawStorage<T, $NRows, U1> + IsContiguous {
            #[inline]
            fn as_ref(&self) -> &mint::$VT<T> {
                // SAFETY: this is OK thanks to the IsContiguous bound.
                unsafe {
                    mem::transmute(self.data.ptr())
                }
            }
        }

        impl<T, S> AsMut<mint::$VT<T>> for Matrix<T, $NRows, U1, S>
        where T: Scalar,
              S: RawStorageMut<T, $NRows, U1> + IsContiguous {
            #[inline]
            fn as_mut(&mut self) -> &mut mint::$VT<T> {
                // SAFETY: this is OK thanks to the IsContiguous bound.
                unsafe {
                    mem::transmute(self.data.ptr_mut())
                }
            }
        }
    )*}
);

// Implement for vectors of dimension 2 .. 4.
impl_from_into_mint_1D!(
    U2 => Vector2[2];
    U3 => Vector3[3];
    U4 => Vector4[4];
);

macro_rules! impl_from_into_mint_2D(
    ($(($NRows: ty, $NCols: ty) => $MV:ident{ $($component:ident),* }[$SZRows: expr]);* $(;)*) => {$(
        impl<T> From<mint::$MV<T>> for OMatrix<T, $NRows, $NCols>
        where T: Scalar,
              DefaultAllocator: Allocator<T, $NRows, $NCols> {
            #[inline]
            fn from(m: mint::$MV<T>) -> Self {
                unsafe {
                    let mut res = Matrix::uninit(<$NRows>::name(), <$NCols>::name());
                    let mut ptr = res.data.ptr_mut();
                    $(
                        ptr::copy_nonoverlapping(&m.$component.x, ptr as *mut T, $SZRows);
                        ptr = ptr.offset($SZRows);
                    )*
                    let _ = ptr; // Just to avoid some unused assignment warnings.
                    // Forget the original data to avoid double-free.
                    mem::forget(m);
                    res.assume_init()
                }
            }
        }

        impl<T> Into<mint::$MV<T>> for OMatrix<T, $NRows, $NCols>
        where T: Scalar,
              DefaultAllocator: Allocator<T, $NRows, $NCols> {
            #[inline]
            fn into(self) -> mint::$MV<T> {
                unsafe {
                    let mut res: MaybeUninit<mint::$MV<T>> = MaybeUninit::uninit();
                    let mut ptr = self.data.ptr();
                    $(
                        ptr::copy_nonoverlapping(ptr, ptr::addr_of_mut!((*res.as_mut_ptr()).$component) as *mut T, $SZRows);
                        ptr = ptr.offset($SZRows);
                    )*
                    let _ = ptr;
                    // Forget the original data to avoid double-free.
                    mem::forget(self);
                    res.assume_init()
                }
            }
        }
    )*}
);

// Implement for matrices with shape 2x2 .. 4x4.
impl_from_into_mint_2D!(
    (U2, U2) => ColumnMatrix2{x, y}[2];
    (U2, U3) => ColumnMatrix2x3{x, y, z}[2];
    (U3, U3) => ColumnMatrix3{x, y, z}[3];
    (U3, U4) => ColumnMatrix3x4{x, y, z, w}[3];
    (U4, U4) => ColumnMatrix4{x, y, z, w}[4];
);