From 8aa10b819c465eb17b3fd4558512469cfab6cd98 Mon Sep 17 00:00:00 2001 From: Benjamin Saunders Date: Sat, 11 Jun 2022 11:07:24 -0700 Subject: [PATCH] Implement `cast` for `Unit>` Currently, `cast` is resolved via `Unit`'s `Deref` impl, which leads to it confusingly stripping the `Unit` from `UnitVector`s. Add an inherent impl which takes precedence, similar to the existing specialization for `UnitQuaternion`. --- src/base/matrix.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/base/matrix.rs b/src/base/matrix.rs index 8f8786c1..d9294c9e 100644 --- a/src/base/matrix.rs +++ b/src/base/matrix.rs @@ -2186,3 +2186,28 @@ where } } } + +impl Unit> +where + T: Scalar, + D: Dim, + S: RawStorage, +{ + /// Cast the components of `self` to another type. + /// + /// # Example + /// ``` + /// # use nalgebra::Vector3; + /// let v = Vector3::::y_axis(); + /// let v2 = v.cast::(); + /// assert_eq!(v2, Vector3::::y_axis()); + /// ``` + pub fn cast(self) -> Unit> + where + T: Scalar, + OVector: SupersetOf>, + DefaultAllocator: Allocator, + { + Unit::new_unchecked(crate::convert_ref(self.as_ref())) + } +}