Merge pull request #1120 from Ralith/unit-vector-cast

Implement `cast` for `Unit<Vector<T, D, S>>`
This commit is contained in:
Sébastien Crozet 2022-08-14 15:04:50 +02:00 committed by GitHub
commit d09d06858f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 25 additions and 0 deletions

View File

@ -2186,3 +2186,28 @@ where
} }
} }
} }
impl<T, D, S> Unit<Vector<T, D, S>>
where
T: Scalar,
D: Dim,
S: RawStorage<T, D, U1>,
{
/// Cast the components of `self` to another type.
///
/// # Example
/// ```
/// # use nalgebra::Vector3;
/// let v = Vector3::<f64>::y_axis();
/// let v2 = v.cast::<f32>();
/// assert_eq!(v2, Vector3::<f32>::y_axis());
/// ```
pub fn cast<T2: Scalar>(self) -> Unit<OVector<T2, D>>
where
T: Scalar,
OVector<T2, D>: SupersetOf<Vector<T, D, S>>,
DefaultAllocator: Allocator<T2, D, U1>,
{
Unit::new_unchecked(crate::convert_ref(self.as_ref()))
}
}