From 90155f3504cff2aaafb735594595387d5dec3212 Mon Sep 17 00:00:00 2001 From: johannesvollmer Date: Tue, 25 Sep 2018 16:31:07 +0200 Subject: [PATCH 1/2] Added functionality to convert between mutable slices --- src/lib.rs | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 793601a..3fed18e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -818,14 +818,32 @@ mod convert { } } -/// Contains utility functions to convert between slices of u16 bits and f16 numbers. +/// Contains utility functions to convert between slices of `u16` bits and `f16` numbers. pub mod slice { use super::f16; use core::slice; - // `from_bits_mut` and `to_bits_mut` would result in - // multiple mutable slices (the original and the reinterpreted) - // pointing to the same block of data, which violates basic mutability rules. + /// Reinterpret a mutable slice of `u16` bits as a mutable slice of `f16` numbers. + // the transmuted slice has the same life time as the original, + // which prevents mutating the borrowed `mut [u16]` argument + // as long as the returned `mut [f16]` is borrowed. + #[inline] + pub fn from_bits_mut<'s>(bits: &'s mut [u16]) -> &'s mut [f16] { + let pointer = bits.as_ptr() as *mut f16; + let length = bits.len(); + unsafe { slice::from_raw_parts_mut(pointer, length) } + } + + /// Reinterpret a mutable slice of `f16` bits as a mutable slice of `u16` numbers. + // the transmuted slice has the same life time as the original, + // which prevents mutating the borrowed `mut [f16]` argument + // as long as the returned `mut [u16]` is borrowed. + #[inline] + pub fn to_bits_mut<'s>(bits: &'s mut [f16]) -> &'s mut [u16] { + let pointer = bits.as_ptr() as *mut u16; + let length = bits.len(); + unsafe { slice::from_raw_parts_mut(pointer, length) } + } /// Reinterpret a slice of `u16` bits as a slice of `f16` numbers. // the transmuted slice has the same life time as the original @@ -1225,4 +1243,22 @@ mod test { assert_eq!(a, b); } } + + #[test] + fn test_mutablility(){ + use consts::*; + let mut bits_array = [ PI.to_bits() ]; + let bits = &mut bits_array[..]; + + { // would not compile without these braces + // TODO: add automated test to check that it does not compile without braces + let numbers = slice::from_bits_mut(bits); + numbers[0] = E; + } + + assert_eq!(bits, &[ E.to_bits() ]); + + bits[0] = LN_2.to_bits(); + assert_eq!(bits, &[ LN_2.to_bits() ]); + } } From 7205555afc3ff47f320a3b726df6c55f79d5f9eb Mon Sep 17 00:00:00 2001 From: johannesvollmer Date: Wed, 26 Sep 2018 21:05:21 +0200 Subject: [PATCH 2/2] Fixed documentation and improved comment formatting --- src/lib.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 3fed18e..a492478 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -824,9 +824,9 @@ pub mod slice { use core::slice; /// Reinterpret a mutable slice of `u16` bits as a mutable slice of `f16` numbers. - // the transmuted slice has the same life time as the original, - // which prevents mutating the borrowed `mut [u16]` argument - // as long as the returned `mut [f16]` is borrowed. + // The transmuted slice has the same life time as the original, + // Which prevents mutating the borrowed `mut [u16]` argument + // As long as the returned `mut [f16]` is borrowed. #[inline] pub fn from_bits_mut<'s>(bits: &'s mut [u16]) -> &'s mut [f16] { let pointer = bits.as_ptr() as *mut f16; @@ -834,10 +834,10 @@ pub mod slice { unsafe { slice::from_raw_parts_mut(pointer, length) } } - /// Reinterpret a mutable slice of `f16` bits as a mutable slice of `u16` numbers. - // the transmuted slice has the same life time as the original, - // which prevents mutating the borrowed `mut [f16]` argument - // as long as the returned `mut [u16]` is borrowed. + /// Reinterpret a mutable slice of `f16` numbers as a mutable slice of `u16` bits. + // The transmuted slice has the same life time as the original, + // Which prevents mutating the borrowed `mut [f16]` argument + // As long as the returned `mut [u16]` is borrowed. #[inline] pub fn to_bits_mut<'s>(bits: &'s mut [f16]) -> &'s mut [u16] { let pointer = bits.as_ptr() as *mut u16; @@ -846,7 +846,7 @@ pub mod slice { } /// Reinterpret a slice of `u16` bits as a slice of `f16` numbers. - // the transmuted slice has the same life time as the original + // The transmuted slice has the same life time as the original #[inline] pub fn from_bits<'s>(bits: &'s [u16]) -> &'s [f16] { let pointer = bits.as_ptr() as *const f16; @@ -855,7 +855,7 @@ pub mod slice { } /// Reinterpret a slice of `f16` numbers as a slice of `u16` bits. - // the transmuted slice has the same life time as the original + // The transmuted slice has the same life time as the original #[inline] pub fn to_bits<'s>(bits: &'s [f16]) -> &'s [u16] { let pointer = bits.as_ptr() as *const u16;