From c9fa7a35e7d7613cac738f102e85000402b43ba0 Mon Sep 17 00:00:00 2001 From: _ <> Date: Thu, 27 Oct 2022 19:56:38 +0000 Subject: [PATCH] Added try_cast() to Matrix --- src/base/matrix.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/base/matrix.rs b/src/base/matrix.rs index e9cc5258..659d3a6e 100644 --- a/src/base/matrix.rs +++ b/src/base/matrix.rs @@ -739,6 +739,24 @@ impl> Matrix { crate::convert(self) } + /// Attempts to cast the components of `self` to another type. + /// + /// # Example + /// ``` + /// # use nalgebra::Vector3; + /// let q = Vector3::new(1.0f64, 2.0, 3.0); + /// let q2 = q.try_cast::(); + /// assert_eq!(q2, Some(Vector3::new(1, 2, 3))); + /// ``` + pub fn try_cast(self) -> Option> + where + T: Scalar, + Self: SupersetOf>, + DefaultAllocator: Allocator, + { + crate::try_convert(self) + } + /// Similar to `self.iter().fold(init, f)` except that `init` is replaced by a closure. /// /// The initialization closure is given the first component of this matrix: