vecmat/matrix/base/
convert.rs

1use crate::{vector::GroupIter, Matrix, Vector};
2use core::convert::TryFrom;
3
4impl<T, const M: usize, const N: usize> Matrix<T, M, N> {
5    pub fn from_vector_of_vectors(a: Vector<Vector<T, N>, M>) -> Self {
6        Self { data: a }
7    }
8    pub fn from_array_of_vectors(a: [Vector<T, N>; M]) -> Self {
9        Self::from_vector_of_vectors(Vector::from_array(a))
10    }
11    pub fn from_array_of_arrays(a: [[T; N]; M]) -> Self {
12        Self::from_vector_of_vectors(Vector::from_array(a).map(Vector::from_array))
13    }
14    pub fn into_vector_of_vectors(self) -> Vector<Vector<T, N>, M> {
15        self.data
16    }
17    pub fn into_array_of_vectors(self) -> [Vector<T, N>; M] {
18        self.into_vector_of_vectors().into_array()
19    }
20    pub fn into_array_of_arrays(self) -> [[T; N]; M] {
21        self.into_vector_of_vectors()
22            .map(|a| a.into_array())
23            .into_array()
24    }
25}
26
27impl<T, const M: usize, const N: usize> From<Vector<Vector<T, N>, M>> for Matrix<T, M, N> {
28    fn from(a: Vector<Vector<T, N>, M>) -> Self {
29        Self::from_vector_of_vectors(a)
30    }
31}
32impl<T, const M: usize, const N: usize> From<[Vector<T, N>; M]> for Matrix<T, M, N> {
33    fn from(a: [Vector<T, N>; M]) -> Self {
34        Self::from_array_of_vectors(a)
35    }
36}
37impl<T, const M: usize, const N: usize> From<[[T; N]; M]> for Matrix<T, M, N> {
38    fn from(a: [[T; N]; M]) -> Self {
39        Self::from_array_of_arrays(a)
40    }
41}
42impl<T, const M: usize, const N: usize> From<Matrix<T, M, N>> for Vector<Vector<T, N>, M> {
43    fn from(mat: Matrix<T, M, N>) -> Self {
44        mat.into_vector_of_vectors()
45    }
46}
47impl<T, const M: usize, const N: usize> From<Matrix<T, M, N>> for [Vector<T, N>; M] {
48    fn from(mat: Matrix<T, M, N>) -> Self {
49        mat.into_array_of_vectors()
50    }
51}
52impl<T, const M: usize, const N: usize> From<Matrix<T, M, N>> for [[T; N]; M] {
53    fn from(mat: Matrix<T, M, N>) -> Self {
54        mat.into_array_of_arrays()
55    }
56}
57
58impl<T, const M: usize, const N: usize> From<&Vector<Vector<T, N>, M>> for Matrix<T, M, N>
59where
60    T: Copy,
61{
62    fn from(ar: &Vector<Vector<T, N>, M>) -> Self {
63        Self::from_vector_of_vectors(*ar)
64    }
65}
66impl<T, const M: usize, const N: usize> From<&[Vector<T, N>; M]> for Matrix<T, M, N>
67where
68    T: Copy,
69{
70    fn from(ar: &[Vector<T, N>; M]) -> Self {
71        Self::from_array_of_vectors(*ar)
72    }
73}
74impl<T, const M: usize, const N: usize> From<&[[T; N]; M]> for Matrix<T, M, N>
75where
76    T: Copy,
77{
78    fn from(ar: &[[T; N]; M]) -> Self {
79        Self::from_array_of_arrays(*ar)
80    }
81}
82impl<T, const M: usize, const N: usize> Matrix<T, M, N> {
83    pub fn as_vector_of_vectors(&self) -> &Vector<Vector<T, N>, M> {
84        &self.data
85    }
86    pub fn as_mut_vector_of_vectors(&mut self) -> &mut Vector<Vector<T, N>, M> {
87        &mut self.data
88    }
89    pub fn as_array_of_vectors(&self) -> &[Vector<T, N>; M] {
90        self.as_vector_of_vectors().as_array()
91    }
92    pub fn as_mut_array_of_vectors(&mut self) -> &mut [Vector<T, N>; M] {
93        self.as_mut_vector_of_vectors().as_mut_array()
94    }
95    pub fn as_array_of_arrays(&self) -> &[[T; N]; M] {
96        unsafe { (self as *const _ as *const [[T; N]; M]).as_ref().unwrap() }
97    }
98    pub fn as_mut_array_of_arrays(&mut self) -> &mut [[T; N]; M] {
99        unsafe { (self as *mut _ as *mut [[T; N]; M]).as_mut().unwrap() }
100    }
101}
102impl<T, const M: usize, const N: usize> AsRef<Vector<Vector<T, N>, M>> for Matrix<T, M, N> {
103    fn as_ref(&self) -> &Vector<Vector<T, N>, M> {
104        self.as_vector_of_vectors()
105    }
106}
107impl<T, const M: usize, const N: usize> AsMut<Vector<Vector<T, N>, M>> for Matrix<T, M, N> {
108    fn as_mut(&mut self) -> &mut Vector<Vector<T, N>, M> {
109        self.as_mut_vector_of_vectors()
110    }
111}
112/*
113impl<T, const M: usize, const N: usize> AsRef<[Vector<T, N>; M]> for Matrix<T, M, N> {
114    fn as_ref(&self) -> &[Vector<T, N>; M] {
115        self.as_array_of_vectors()
116    }
117}
118impl<T, const M: usize, const N: usize> AsMut<[Vector<T, N>; M]> for Matrix<T, M, N> {
119    fn as_mut(&mut self) -> &mut [Vector<T, N>; M] {
120        self.as_mut_array_of_vectors()
121    }
122}
123impl<T, const M: usize, const N: usize> AsRef<[[T; N]; M]> for Matrix<T, M, N> {
124    fn as_ref(&self) -> &[[T; N]; M] {
125        self.as_array_of_arrays()
126    }
127}
128impl<T, const M: usize, const N: usize> AsMut<[[T; N]; M]> for Matrix<T, M, N> {
129    fn as_mut(&mut self) -> &mut [[T; N]; M] {
130        self.as_mut_array_of_arrays()
131    }
132}
133*/
134
135impl<'a, T, const M: usize, const N: usize> From<&'a Matrix<T, M, N>>
136    for &'a Vector<Vector<T, N>, M>
137{
138    fn from(mr: &'a Matrix<T, M, N>) -> Self {
139        mr.as_vector_of_vectors()
140    }
141}
142impl<'a, T, const M: usize, const N: usize> From<&'a mut Matrix<T, M, N>>
143    for &'a mut Vector<Vector<T, N>, M>
144{
145    fn from(mr: &'a mut Matrix<T, M, N>) -> Self {
146        mr.as_mut_vector_of_vectors()
147    }
148}
149impl<'a, T, const M: usize, const N: usize> From<&'a Matrix<T, M, N>> for &'a [[T; N]; M] {
150    fn from(mr: &'a Matrix<T, M, N>) -> Self {
151        mr.as_array_of_arrays()
152    }
153}
154impl<'a, T, const M: usize, const N: usize> From<&'a mut Matrix<T, M, N>> for &'a mut [[T; N]; M] {
155    fn from(mr: &'a mut Matrix<T, M, N>) -> Self {
156        mr.as_mut_array_of_arrays()
157    }
158}
159
160impl<T, const M: usize, const N: usize> Matrix<T, M, N> {
161    pub fn try_from_iter_of_vectors<I>(i: I) -> Option<Self>
162    where
163        I: Iterator<Item = Vector<T, N>>,
164    {
165        <Vector<Vector<T, N>, M>>::try_from_iter(i).map(Self::from_vector_of_vectors)
166    }
167    pub fn try_from_iter<I>(i: I) -> Option<Self>
168    where
169        I: Iterator<Item = T>,
170    {
171        Self::try_from_iter_of_vectors(GroupIter::new(i))
172    }
173}
174impl<'a, T, const M: usize, const N: usize> TryFrom<&'a [Vector<T, N>]> for Matrix<T, M, N>
175where
176    T: Copy,
177{
178    type Error = ();
179    fn try_from(s: &'a [Vector<T, N>]) -> Result<Self, ()> {
180        if s.len() == M {
181            Ok(Matrix::try_from_iter_of_vectors(s.iter().cloned()).unwrap())
182        } else {
183            Err(())
184        }
185    }
186}
187impl<'a, T, const M: usize, const N: usize> TryFrom<&'a [T]> for Matrix<T, M, N>
188where
189    T: Copy,
190{
191    type Error = ();
192    fn try_from(s: &'a [T]) -> Result<Self, ()> {
193        if s.len() == M * N {
194            Ok(Matrix::try_from_iter(s.iter().cloned()).unwrap())
195        } else {
196            Err(())
197        }
198    }
199}