removed potentially unsound unsafe code

This commit is contained in:
Joshua Smith 2024-03-28 21:08:11 -04:00
parent 5a1f9a5236
commit 3235751526
1 changed files with 21 additions and 13 deletions

View File

@ -113,7 +113,6 @@ where
#[inline] #[inline]
#[must_use] #[must_use]
pub fn slerp(&self, other: &Self, t: T) -> Self { pub fn slerp(&self, other: &Self, t: T) -> Self {
use std::mem::transmute;
//The best option here would be to use #[feature(specialization)], but until //The best option here would be to use #[feature(specialization)], but until
//that's stabilized, this is the best we can do. Theoretically, the compiler should //that's stabilized, this is the best we can do. Theoretically, the compiler should
@ -124,21 +123,30 @@ where
//FIXME: this doesn't really work in 1D since we can't interp between -1 and 1 //FIXME: this doesn't really work in 1D since we can't interp between -1 and 1
1 => self.clone(), 1 => self.clone(),
//NOTE: Not pretty, but without refactoring the API, this is the best we can do 2 => {
//NOTE: This is safe because we directly check the dimension first let self2d = Rotation2::from_matrix_unchecked(
2 => unsafe { self.clone().into_inner().fixed_resize(T::zero())
let (self2d, other2d) = (
transmute::<&Self, &Rotation2<T>>(self),
transmute::<&Self, &Rotation2<T>>(other),
); );
transmute::<&Rotation2<T>, &Self>(&self2d.slerp_2d(other2d, t)).clone() let other2d = Rotation2::from_matrix_unchecked(
other.clone().into_inner().fixed_resize(T::zero())
);
Self::from_matrix_unchecked(
self2d.slerp_2d(&other2d, t).into_inner().fixed_resize(T::zero())
)
}, },
3 => unsafe {
let (self3d, other3d) = ( 3 => {
transmute::<&Self, &Rotation3<T>>(self), let self3d = Rotation3::from_matrix_unchecked(
transmute::<&Self, &Rotation3<T>>(other), self.clone().into_inner().fixed_resize(T::zero())
); );
transmute::<&Rotation3<T>, &Self>(&self3d.slerp_3d(other3d, t)).clone() let other3d = Rotation3::from_matrix_unchecked(
other.clone().into_inner().fixed_resize(T::zero())
);
Self::from_matrix_unchecked(
self3d.slerp_3d(&other3d, t).into_inner().fixed_resize(T::zero())
)
}, },
//the multiplication order matters here //the multiplication order matters here