From 2f03857017ce9a6fa619c4747039f72eeaea002a Mon Sep 17 00:00:00 2001 From: Benjamin Saunders Date: Mon, 24 Sep 2018 18:04:57 -0700 Subject: [PATCH] Introduce Unit::from_ref_unchecked --- src/base/unit.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/base/unit.rs b/src/base/unit.rs index b49bc83c..2b466c04 100644 --- a/src/base/unit.rs +++ b/src/base/unit.rs @@ -16,7 +16,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. -#[repr(C)] +#[repr(transparent)] #[derive(Eq, PartialEq, Clone, Hash, Debug, Copy)] pub struct Unit { value: T, @@ -104,13 +104,17 @@ impl Unit { impl Unit { /// Wraps the given value, assuming it is already normalized. - /// - /// This function is not safe because `value` is not verified to be actually normalized. #[inline] pub fn new_unchecked(value: T) -> Self { Unit { value: value } } + /// Wraps the given reference, assuming it is already normalized. + #[inline] + pub fn from_ref_unchecked<'a>(value: &'a T) -> &'a Self { + unsafe { mem::transmute(value) } + } + /// Retrieves the underlying value. #[inline] pub fn unwrap(self) -> T {