Skip to content

Commit

Permalink
Implement cast for Unit<Vector<T, D, S>>
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
Ralith committed Jun 11, 2022
1 parent dd80156 commit 8aa10b8
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/base/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
}

0 comments on commit 8aa10b8

Please sign in to comment.