mirror of
https://github.com/SinTan1729/matrix-basic.git
synced 2024-12-25 21:48:35 -06:00
new: Added the MatrixFrom trait
This commit is contained in:
parent
5ac1e60597
commit
e9b21e330c
2 changed files with 46 additions and 15 deletions
43
src/lib.rs
43
src/lib.rs
|
@ -482,12 +482,7 @@ impl<T: ToMatrix> Sub for Matrix<T> {
|
|||
/// of specialization system is implemented.
|
||||
/// You can track this issue [here](https://github.com/rust-lang/rust/issues/42721).
|
||||
pub trait MatrixInto<T: ToMatrix> {
|
||||
/// Method for converting a matrix into a matrix of type [`Matrix<T>`]
|
||||
fn matrix_into(self) -> Matrix<T>;
|
||||
}
|
||||
|
||||
/// Blanket implementation of [`MatrixInto`] for converting [`Matrix<S>`] to [`Matrix<T>`] whenever
|
||||
/// `T` implements [`From(S)`].
|
||||
/// Method for converting a matrix into a matrix of type [`Matrix<T>`].
|
||||
/// # Example
|
||||
/// ```
|
||||
/// use matrix_basic::Matrix;
|
||||
|
@ -495,11 +490,16 @@ pub trait MatrixInto<T: ToMatrix> {
|
|||
///
|
||||
/// let a = Matrix::from(vec![vec![1, 2, 3], vec![0, 1, 2]]).unwrap();
|
||||
/// let b = Matrix::from(vec![vec![1.0, 2.0, 3.0], vec![0.0, 1.0, 2.0]]).unwrap();
|
||||
/// let c: Matrix<f64> = a.matrix_into();
|
||||
/// let c: Matrix<f64> = a.matrix_into(); // Type annotation is needed here
|
||||
///
|
||||
/// assert_eq!(c, b);
|
||||
/// ```
|
||||
impl<T: ToMatrix + From<S>, S: ToMatrix> MatrixInto<T> for Matrix<S> {
|
||||
fn matrix_into(self) -> Matrix<T>;
|
||||
}
|
||||
|
||||
/// Blanket implementation of [`MatrixInto`] for converting [`Matrix<S>`] to [`Matrix<T>`] whenever
|
||||
/// `S` implements [`Into(T)`]. Look at [`matrix_into`](Self::matrix_into()).
|
||||
impl<T: ToMatrix, S: ToMatrix + Into<T>> MatrixInto<T> for Matrix<S> {
|
||||
fn matrix_into(self) -> Matrix<T> {
|
||||
let mut out = Vec::new();
|
||||
for row in self.entries {
|
||||
|
@ -512,3 +512,30 @@ impl<T: ToMatrix + From<S>, S: ToMatrix> MatrixInto<T> for Matrix<S> {
|
|||
Matrix { entries: out }
|
||||
}
|
||||
}
|
||||
|
||||
/// Sister trait of [`MatrixInto`]. Basically does the same thing, just with a
|
||||
/// different syntax.
|
||||
pub trait MatrixFrom<T> {
|
||||
/// Method for getting a matrix from another matrix of type [`Matrix<T>`].
|
||||
/// # Example
|
||||
/// ```
|
||||
/// use matrix_basic::Matrix;
|
||||
/// use matrix_basic::MatrixFrom;
|
||||
///
|
||||
/// let a = Matrix::from(vec![vec![1, 2, 3], vec![0, 1, 2]]).unwrap();
|
||||
/// let b = Matrix::from(vec![vec![1.0, 2.0, 3.0], vec![0.0, 1.0, 2.0]]).unwrap();
|
||||
/// let c = Matrix::<f64>::matrix_from(a); // Type annotation is needed here
|
||||
///
|
||||
/// assert_eq!(c, b);
|
||||
/// ```
|
||||
fn matrix_from(input: T) -> Self;
|
||||
}
|
||||
|
||||
/// Blanket implementation of [`MatrixFrom`] for [`Matrix<S>`] whenever `T` (which is actually some)[`Matrix<U>`] implements
|
||||
/// [`MatrixInto<S>`].
|
||||
impl<T: MatrixInto<S>, S: ToMatrix> MatrixFrom<T> for Matrix<S> {
|
||||
fn matrix_from(input: T) -> Self {
|
||||
let out: Matrix<S> = input.matrix_into();
|
||||
out
|
||||
}
|
||||
}
|
||||
|
|
|
@ -72,5 +72,9 @@ fn conversion_test() {
|
|||
let b = Matrix::from(vec![vec![1.0, 2.0, 3.0], vec![0.0, 1.0, 2.0]]).unwrap();
|
||||
|
||||
use crate::MatrixInto;
|
||||
assert_eq!(a.matrix_into(), b);
|
||||
assert_eq!(a.clone().matrix_into(), b);
|
||||
|
||||
use crate::MatrixFrom;
|
||||
let c = Matrix::<f64>::matrix_from(a);
|
||||
assert_eq!(c, b);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue