Skip to content

Commit

Permalink
Merge pull request #1171 from md2468/try_cast
Browse files Browse the repository at this point in the history
Added `try_cast()` to `Matrix`.
  • Loading branch information
sebcrozet authored Oct 27, 2022
2 parents 2f1f441 + c9fa7a3 commit 5389212
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/base/matrix.rs
Original file line number Diff line number Diff line change
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

0 comments on commit 5389212

Please sign in to comment.