From 35d2381a2a9a1f59bb0cd34f69a42441bc7e94e7 Mon Sep 17 00:00:00 2001 From: Jack Wrenn Date: Sun, 9 Dec 2018 15:08:14 -0500 Subject: [PATCH] Rename `Unit::unwrap` to `Unit::into_inner` and deprecate `Unit::unwrap` See #460 --- src/base/unit.rs | 12 ++++++++++-- src/geometry/quaternion.rs | 14 +++++++------- src/geometry/quaternion_construction.rs | 20 ++++++++++---------- src/geometry/quaternion_ops.rs | 4 ++-- src/geometry/reflection.rs | 2 +- src/geometry/rotation_ops.rs | 4 ++-- src/geometry/rotation_specialization.rs | 2 +- src/geometry/unit_complex_ops.rs | 14 +++++++------- src/linalg/schur.rs | 2 +- 9 files changed, 41 insertions(+), 33 deletions(-) diff --git a/src/base/unit.rs b/src/base/unit.rs index 1c2fa1e2..af400389 100644 --- a/src/base/unit.rs +++ b/src/base/unit.rs @@ -15,7 +15,7 @@ use alga::linear::NormedSpace; /// A wrapper that ensures the underlying algebraic entity has a unit norm. /// -/// Use `.as_ref()` or `.unwrap()` to obtain the underlying value by-reference or by-move. +/// Use `.as_ref()` or `.into_inner()` to obtain the underlying value by-reference or by-move. #[repr(transparent)] #[derive(Eq, PartialEq, Clone, Hash, Debug, Copy)] pub struct Unit { @@ -113,6 +113,14 @@ impl Unit { /// Retrieves the underlying value. #[inline] + pub fn into_inner(self) -> T { + self.value + } + + /// Retrieves the underlying value. + /// Deprecated: use [Unit::into_inner] instead. + #[deprecated(note="use `.into_inner()` instead")] + #[inline] pub fn unwrap(self) -> T { self.value } @@ -143,7 +151,7 @@ where T::Field: RelativeEq { #[inline] fn to_superset(&self) -> T { - self.clone().unwrap() + self.clone().into_inner() } #[inline] diff --git a/src/geometry/quaternion.rs b/src/geometry/quaternion.rs index fc82e061..0d5275dc 100644 --- a/src/geometry/quaternion.rs +++ b/src/geometry/quaternion.rs @@ -156,7 +156,7 @@ impl Quaternion { /// let inv_q = q.try_inverse(); /// /// assert!(inv_q.is_some()); - /// assert_relative_eq!(inv_q.unwrap() * q, Quaternion::identity()); + /// assert_relative_eq!(inv_q.into_inner() * q, Quaternion::identity()); /// /// //Non-invertible case /// let q = Quaternion::new(0.0, 0.0, 0.0, 0.0); @@ -747,7 +747,7 @@ impl UnitQuaternion { Unit::new_unchecked(Quaternion::from( Unit::new_unchecked(self.coords) .slerp(&Unit::new_unchecked(other.coords), t) - .unwrap(), + .into_inner(), )) } @@ -771,7 +771,7 @@ impl UnitQuaternion { { Unit::new_unchecked(self.coords) .try_slerp(&Unit::new_unchecked(other.coords), t, epsilon) - .map(|q| Unit::new_unchecked(Quaternion::from(q.unwrap()))) + .map(|q| Unit::new_unchecked(Quaternion::from(q.into_inner()))) } /// Compute the conjugate of this unit quaternion in-place. @@ -837,7 +837,7 @@ impl UnitQuaternion { #[inline] pub fn scaled_axis(&self) -> Vector3 { if let Some(axis) = self.axis() { - axis.unwrap() * self.angle() + axis.into_inner() * self.angle() } else { Vector3::zero() } @@ -894,7 +894,7 @@ impl UnitQuaternion { #[inline] pub fn ln(&self) -> Quaternion { if let Some(v) = self.axis() { - Quaternion::from_parts(N::zero(), v.unwrap() * self.angle()) + Quaternion::from_parts(N::zero(), v.into_inner() * self.angle()) } else { Quaternion::zero() } @@ -914,7 +914,7 @@ impl UnitQuaternion { /// let angle = 1.2; /// let rot = UnitQuaternion::from_axis_angle(&axis, angle); /// let pow = rot.powf(2.0); - /// assert_relative_eq!(pow.axis().unwrap(), axis, epsilon = 1.0e-6); + /// assert_relative_eq!(pow.axis().into_inner(), axis, epsilon = 1.0e-6); /// assert_eq!(pow.angle(), 2.4); /// ``` #[inline] @@ -1029,7 +1029,7 @@ impl UnitQuaternion { impl fmt::Display for UnitQuaternion { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { if let Some(axis) = self.axis() { - let axis = axis.unwrap(); + let axis = axis.into_inner(); write!( f, "UnitQuaternion angle: {} − axis: ({}, {}, {})", diff --git a/src/geometry/quaternion_construction.rs b/src/geometry/quaternion_construction.rs index 65f206ef..f99c5ec6 100644 --- a/src/geometry/quaternion_construction.rs +++ b/src/geometry/quaternion_construction.rs @@ -76,7 +76,7 @@ impl Quaternion { where SB: Storage { let rot = UnitQuaternion::::from_axis_angle(&axis, theta * ::convert(2.0f64)); - rot.unwrap() * scale + rot.into_inner() * scale } /// The quaternion multiplicative identity. @@ -176,7 +176,7 @@ impl UnitQuaternion { /// let vec = Vector3::new(4.0, 5.0, 6.0); /// let q = UnitQuaternion::from_axis_angle(&axis, angle); /// - /// assert_eq!(q.axis().unwrap(), axis); + /// assert_eq!(q.axis().into_inner(), axis); /// assert_eq!(q.angle(), angle); /// assert_relative_eq!(q * pt, Point3::new(6.0, 5.0, -4.0), epsilon = 1.0e-6); /// assert_relative_eq!(q * vec, Vector3::new(6.0, 5.0, -4.0), epsilon = 1.0e-6); @@ -244,7 +244,7 @@ impl UnitQuaternion { /// let rot = Rotation3::from_axis_angle(&axis, angle); /// let q = UnitQuaternion::from_rotation_matrix(&rot); /// assert_relative_eq!(q.to_rotation_matrix(), rot, epsilon = 1.0e-6); - /// assert_relative_eq!(q.axis().unwrap(), rot.axis().unwrap(), epsilon = 1.0e-6); + /// assert_relative_eq!(q.axis().into_inner(), rot.axis().into_inner(), epsilon = 1.0e-6); /// assert_relative_eq!(q.angle(), rot.angle(), epsilon = 1.0e-6); /// ``` #[inline] @@ -306,7 +306,7 @@ impl UnitQuaternion { /// # use nalgebra::{Vector3, UnitQuaternion}; /// let a = Vector3::new(1.0, 2.0, 3.0); /// let b = Vector3::new(3.0, 1.0, 2.0); - /// let q = UnitQuaternion::rotation_between(&a, &b).unwrap(); + /// let q = UnitQuaternion::rotation_between(&a, &b).into_inner(); /// assert_relative_eq!(q * a, b); /// assert_relative_eq!(q.inverse() * b, a); /// ``` @@ -329,8 +329,8 @@ impl UnitQuaternion { /// # use nalgebra::{Vector3, UnitQuaternion}; /// let a = Vector3::new(1.0, 2.0, 3.0); /// let b = Vector3::new(3.0, 1.0, 2.0); - /// let q2 = UnitQuaternion::scaled_rotation_between(&a, &b, 0.2).unwrap(); - /// let q5 = UnitQuaternion::scaled_rotation_between(&a, &b, 0.5).unwrap(); + /// let q2 = UnitQuaternion::scaled_rotation_between(&a, &b, 0.2).into_inner(); + /// let q5 = UnitQuaternion::scaled_rotation_between(&a, &b, 0.5).into_inner(); /// assert_relative_eq!(q2 * q2 * q2 * q2 * q2 * a, b, epsilon = 1.0e-6); /// assert_relative_eq!(q5 * q5 * a, b, epsilon = 1.0e-6); /// ``` @@ -365,7 +365,7 @@ impl UnitQuaternion { /// # use nalgebra::{Unit, Vector3, UnitQuaternion}; /// let a = Unit::new_normalize(Vector3::new(1.0, 2.0, 3.0)); /// let b = Unit::new_normalize(Vector3::new(3.0, 1.0, 2.0)); - /// let q = UnitQuaternion::rotation_between(&a, &b).unwrap(); + /// let q = UnitQuaternion::rotation_between(&a, &b).into_inner(); /// assert_relative_eq!(q * a, b); /// assert_relative_eq!(q.inverse() * b, a); /// ``` @@ -391,8 +391,8 @@ impl UnitQuaternion { /// # use nalgebra::{Unit, Vector3, UnitQuaternion}; /// let a = Unit::new_normalize(Vector3::new(1.0, 2.0, 3.0)); /// let b = Unit::new_normalize(Vector3::new(3.0, 1.0, 2.0)); - /// let q2 = UnitQuaternion::scaled_rotation_between(&a, &b, 0.2).unwrap(); - /// let q5 = UnitQuaternion::scaled_rotation_between(&a, &b, 0.5).unwrap(); + /// let q2 = UnitQuaternion::scaled_rotation_between(&a, &b, 0.2).into_inner(); + /// let q5 = UnitQuaternion::scaled_rotation_between(&a, &b, 0.5).into_inner(); /// assert_relative_eq!(q2 * q2 * q2 * q2 * q2 * a, b, epsilon = 1.0e-6); /// assert_relative_eq!(q5 * q5 * a, b, epsilon = 1.0e-6); /// ``` @@ -701,7 +701,7 @@ mod tests { let mut rng = rand::prng::XorShiftRng::from_seed([0xAB; 16]); for _ in 0..1000 { let x = rng.gen::>(); - assert!(relative_eq!(x.unwrap().norm(), 1.0)) + assert!(relative_eq!(x.into_inner().norm(), 1.0)) } } } diff --git a/src/geometry/quaternion_ops.rs b/src/geometry/quaternion_ops.rs index c4ccdf13..842b4edd 100644 --- a/src/geometry/quaternion_ops.rs +++ b/src/geometry/quaternion_ops.rs @@ -469,7 +469,7 @@ quaternion_op_impl!( (U4, U1), (U3, U1) for SB: Storage ; self: &'a UnitQuaternion, rhs: Unit>, Output = Unit> => U3, U4; - Unit::new_unchecked(self * rhs.unwrap()); + Unit::new_unchecked(self * rhs.into_inner()); 'a); quaternion_op_impl!( @@ -485,7 +485,7 @@ quaternion_op_impl!( (U4, U1), (U3, U1) for SB: Storage ; self: UnitQuaternion, rhs: Unit>, Output = Unit> => U3, U4; - Unit::new_unchecked(self * rhs.unwrap()); + Unit::new_unchecked(self * rhs.into_inner()); ); macro_rules! scalar_op_impl( diff --git a/src/geometry/reflection.rs b/src/geometry/reflection.rs index bd4da0cd..fa08dcdd 100644 --- a/src/geometry/reflection.rs +++ b/src/geometry/reflection.rs @@ -20,7 +20,7 @@ impl> Reflection { /// represents a plane that passes through the origin. pub fn new(axis: Unit>, bias: N) -> Reflection { Reflection { - axis: axis.unwrap(), + axis: axis.into_inner(), bias: bias, } } diff --git a/src/geometry/rotation_ops.rs b/src/geometry/rotation_ops.rs index 94bf6da2..e7cccd4b 100644 --- a/src/geometry/rotation_ops.rs +++ b/src/geometry/rotation_ops.rs @@ -125,8 +125,8 @@ md_impl_all!( where DefaultAllocator: Allocator where ShapeConstraint: AreMultipliable; self: Rotation, right: Unit>, Output = Unit>; - [val val] => Unit::new_unchecked(self.unwrap() * right.unwrap()); - [ref val] => Unit::new_unchecked(self.matrix() * right.unwrap()); + [val val] => Unit::new_unchecked(self.unwrap() * right.into_inner()); + [ref val] => Unit::new_unchecked(self.matrix() * right.into_inner()); [val ref] => Unit::new_unchecked(self.unwrap() * right.as_ref()); [ref ref] => Unit::new_unchecked(self.matrix() * right.as_ref()); ); diff --git a/src/geometry/rotation_specialization.rs b/src/geometry/rotation_specialization.rs index a8947b96..fa75ca04 100644 --- a/src/geometry/rotation_specialization.rs +++ b/src/geometry/rotation_specialization.rs @@ -616,7 +616,7 @@ impl Rotation3 { #[inline] pub fn scaled_axis(&self) -> Vector3 { if let Some(axis) = self.axis() { - axis.unwrap() * self.angle() + axis.into_inner() * self.angle() } else { Vector::zero() } diff --git a/src/geometry/unit_complex_ops.rs b/src/geometry/unit_complex_ops.rs index 8f154423..0a8306d2 100644 --- a/src/geometry/unit_complex_ops.rs +++ b/src/geometry/unit_complex_ops.rs @@ -50,7 +50,7 @@ impl Mul> for UnitComplex { #[inline] fn mul(self, rhs: UnitComplex) -> UnitComplex { - Unit::new_unchecked(self.unwrap() * rhs.unwrap()) + Unit::new_unchecked(self.into_inner() * rhs.into_inner()) } } @@ -59,7 +59,7 @@ impl<'a, N: Real> Mul> for &'a UnitComplex { #[inline] fn mul(self, rhs: UnitComplex) -> UnitComplex { - Unit::new_unchecked(self.complex() * rhs.unwrap()) + Unit::new_unchecked(self.complex() * rhs.into_inner()) } } @@ -68,7 +68,7 @@ impl<'b, N: Real> Mul<&'b UnitComplex> for UnitComplex { #[inline] fn mul(self, rhs: &'b UnitComplex) -> UnitComplex { - Unit::new_unchecked(self.unwrap() * rhs.complex()) + Unit::new_unchecked(self.into_inner() * rhs.complex()) } } @@ -87,7 +87,7 @@ impl Div> for UnitComplex { #[inline] fn div(self, rhs: UnitComplex) -> UnitComplex { - Unit::new_unchecked(self.unwrap() * rhs.conjugate().unwrap()) + Unit::new_unchecked(self.into_inner() * rhs.conjugate().into_inner()) } } @@ -96,7 +96,7 @@ impl<'a, N: Real> Div> for &'a UnitComplex { #[inline] fn div(self, rhs: UnitComplex) -> UnitComplex { - Unit::new_unchecked(self.complex() * rhs.conjugate().unwrap()) + Unit::new_unchecked(self.complex() * rhs.conjugate().into_inner()) } } @@ -105,7 +105,7 @@ impl<'b, N: Real> Div<&'b UnitComplex> for UnitComplex { #[inline] fn div(self, rhs: &'b UnitComplex) -> UnitComplex { - Unit::new_unchecked(self.unwrap() * rhs.conjugate().unwrap()) + Unit::new_unchecked(self.into_inner() * rhs.conjugate().into_inner()) } } @@ -114,7 +114,7 @@ impl<'a, 'b, N: Real> Div<&'b UnitComplex> for &'a UnitComplex { #[inline] fn div(self, rhs: &'b UnitComplex) -> UnitComplex { - Unit::new_unchecked(self.complex() * rhs.conjugate().unwrap()) + Unit::new_unchecked(self.complex() * rhs.conjugate().into_inner()) } } diff --git a/src/linalg/schur.rs b/src/linalg/schur.rs index a3bd5404..66299c07 100644 --- a/src/linalg/schur.rs +++ b/src/linalg/schur.rs @@ -412,7 +412,7 @@ where rot.rotate_rows(&mut m); if compute_q { - let c = rot.unwrap(); + let c = rot.into_inner(); // XXX: we have to build the matrix manually because // rot.to_rotation_matrix().unwrap() causes an ICE. q = Some(MatrixN::from_column_slice_generic(