Introduce Unit::from_ref_unchecked

This commit is contained in:
Benjamin Saunders 2018-09-24 18:04:57 -07:00 committed by Sébastien Crozet
parent e73701b3a8
commit 2f03857017
1 changed files with 7 additions and 3 deletions

View File

@ -16,7 +16,7 @@ use alga::linear::NormedSpace;
/// A wrapper that ensures the underlying algebraic entity has a unit norm. /// 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 `.unwrap()` to obtain the underlying value by-reference or by-move.
#[repr(C)] #[repr(transparent)]
#[derive(Eq, PartialEq, Clone, Hash, Debug, Copy)] #[derive(Eq, PartialEq, Clone, Hash, Debug, Copy)]
pub struct Unit<T> { pub struct Unit<T> {
value: T, value: T,
@ -104,13 +104,17 @@ impl<T: NormedSpace> Unit<T> {
impl<T> Unit<T> { impl<T> Unit<T> {
/// Wraps the given value, assuming it is already normalized. /// Wraps the given value, assuming it is already normalized.
///
/// This function is not safe because `value` is not verified to be actually normalized.
#[inline] #[inline]
pub fn new_unchecked(value: T) -> Self { pub fn new_unchecked(value: T) -> Self {
Unit { value: value } 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. /// Retrieves the underlying value.
#[inline] #[inline]
pub fn unwrap(self) -> T { pub fn unwrap(self) -> T {