Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added try_cast() to Matrix. #1171

Merged
merged 1 commit into from Oct 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/base/matrix.rs
Expand Up @@ -739,6 +739,24 @@ impl<T, R: Dim, C: Dim, S: RawStorage<T, R, C>> Matrix<T, R, C, S> {
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::<i32>();
/// assert_eq!(q2, Some(Vector3::new(1, 2, 3)));
/// ```
pub fn try_cast<T2: Scalar>(self) -> Option<OMatrix<T2, R, C>>
where
T: Scalar,
Self: SupersetOf<OMatrix<T2, R, C>>,
DefaultAllocator: Allocator<T2, R, C>,
{
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:
Expand Down