From 55ac044c39d7bb81880a2c83a60e726f25d25cc6 Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Sat, 27 May 2023 18:59:09 -0500 Subject: [PATCH] change: Change syntax between MatrixFrom and MatrixInto so that it'll be easier to integrate into std::From later --- src/lib.rs | 88 ++++++++++++++++++++++++++-------------------------- src/tests.rs | 2 +- 2 files changed, 45 insertions(+), 45 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 6644769..f09a978 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -557,48 +557,14 @@ impl Sub for Matrix { } /// Trait for conversion between matrices of different types. -/// It only has a [`matrix_into()`](Self::matrix_into()) method. +/// It only has a [`matrix_from()`](Self::matrix_from()) method. /// This is needed since negative trait bound are not supported in stable Rust /// yet, so we'll have a conflict trying to implement [`From`]. /// I plan to change this to the default From trait as soon as some sort /// of specialization system is implemented. /// You can track this issue [here](https://github.com/rust-lang/rust/issues/42721). -pub trait MatrixInto { - /// Method for converting a matrix into a matrix of type [`Matrix`]. - /// # Example - /// ``` - /// use matrix_basic::Matrix; - /// use matrix_basic::MatrixInto; - /// - /// 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 = a.matrix_into(); // Type annotation is needed here - /// - /// assert_eq!(c, b); - /// ``` - fn matrix_into(self) -> Matrix; -} - -/// Blanket implementation of [`MatrixInto`] for converting [`Matrix`] to [`Matrix`] whenever -/// `S` implements [`Into(T)`]. Look at [`matrix_into`](Self::matrix_into()). -impl> MatrixInto for Matrix { - fn matrix_into(self) -> Matrix { - let mut out = Vec::new(); - for row in self.entries { - let mut new_row: Vec = Vec::new(); - for entry in row { - new_row.push(entry.into()); - } - out.push(new_row) - } - Matrix { entries: out } - } -} - -/// Sister trait of [`MatrixInto`]. Basically does the same thing, just with a -/// different syntax. -pub trait MatrixFrom { - /// Method for getting a matrix from another matrix of type [`Matrix`]. +pub trait MatrixFrom { + /// Method for getting a matrix of a new type from a matrix of type [`Matrix`]. /// # Example /// ``` /// use matrix_basic::Matrix; @@ -610,14 +576,48 @@ pub trait MatrixFrom { /// /// assert_eq!(c, b); /// ``` - fn matrix_from(input: T) -> Self; + fn matrix_from(input: Matrix) -> Self; } -/// Blanket implementation of [`MatrixFrom`] for [`Matrix`] whenever `T` (which is actually some)[`Matrix`] implements -/// [`MatrixInto`]. -impl, S: ToMatrix> MatrixFrom for Matrix { - fn matrix_from(input: T) -> Self { - let out: Matrix = input.matrix_into(); - out +/// Blanket implementation of [`MatrixFrom`] for converting [`Matrix`] to [`Matrix`] whenever +/// `S` implements [`From(T)`]. Look at [`matrix_into`](Self::matrix_into()). +impl> MatrixFrom for Matrix { + fn matrix_from(input: Matrix) -> Self { + let mut out = Vec::new(); + for row in input.entries { + let mut new_row: Vec = Vec::new(); + for entry in row { + new_row.push(entry.into()); + } + out.push(new_row) + } + Matrix { entries: out } + } +} + +/// Sister trait of [`MatrixFrom`]. Basically does the same thing, just with a +/// different syntax. +pub trait MatrixInto { + /// Method for converting a matrix [`Matrix`] to another type. + /// # Example + /// ``` + /// use matrix_basic::Matrix; + /// use matrix_basic::MatrixInto; + /// + /// 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 = a.matrix_into(); // Type annotation is needed here + /// + /// + /// assert_eq!(c, b); + /// ``` + fn matrix_into(self) -> T; +} + +/// Blanket implementation of [`MatrixInto`] for [`Matrix`] whenever `T` +/// (which is actually some)[`Matrix`] implements [`MatrixFrom`]. +impl, S: ToMatrix> MatrixInto for Matrix { + fn matrix_into(self) -> T { + T::matrix_from(self) } } diff --git a/src/tests.rs b/src/tests.rs index 388f9c4..4b15862 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -72,7 +72,7 @@ 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.clone().matrix_into(), b); + assert_eq!(b, a.clone().matrix_into()); use crate::MatrixFrom; let c = Matrix::::matrix_from(a);