[−][src]Struct possibly_uninit::RefMut
Mutable reference wrapper that only allows writing valid values to the memory location.
This is used for values that might be borowed from either T
or
MaybeUninit<T>
.
One would normally expect this to be &mut MaybeUninit<T>
, however,
that wouldn't be safe. Consider this code:
use core::mem::MaybeUninit; fn cast_mut<T>(val: &mut T) -> &mut MaybeUninit<T> { unsafe { core::mem::transmute(val) } } // No unsafe code here fn main() { let mut message = "Hello world!".to_string(); core::mem::replace(cast_mut(&mut message), MaybeUninit::uninit()); println!("This is now garbage: {}", message); }
The code above triggers UB. Thus the users of the reference must be prevented from writing invalid values into the memory location. That's only possible by creating a newtype like this one.
While the newtype itself doesn't track initializedness, so its use may be limited in safe code, it's a base building block allowing sound implementations of wrappers tracking initializedness.
Methods
impl<'a, T> RefMut<'a, T>
[src]
pub fn write(self, value: T) -> &'a mut T
[src]
Writes a valid value to given memory location, initializing it.
pub fn write_mut(&mut self, value: T) -> &mut T
[src]
Writes a valid value to given memory location, initializing it.
This method is same as write
, it just reborows the reference instead
of consuming it.
pub unsafe fn into_assume_init(self) -> &'a mut T
[src]
Turns the wrapper into reference assuming the valuewas initialized.
Safety
Calling this function if no value was written is UB.
pub fn as_non_null(&mut self) -> NonNull<T>
[src]
Returns mutable non-null pointer to the value.
pub fn as_mut_ptr(&mut self) -> *mut T
[src]
Returns mutable raw pointer to the value.
Note that this is in fact non-null, it's just sometimes more useful than
as_non_null
.
pub fn write_zeroes(&mut self) -> &mut T where
T: ZeroValid,
[src]
T: ZeroValid,
Overwrites the value with all zeroes.
pub fn into_zeroed(self) -> &'a mut T where
T: ZeroValid,
[src]
T: ZeroValid,
Overwrites the value with all zeroes.
Consumes the referenceto to presere the lifetime
Trait Implementations
impl<'a, T> BorrowUninit<T> for RefMut<'a, T>
[src]
fn borrow_uninit(&self) -> &MaybeUninit<T>
[src]
unsafe fn assume_init_ref(&self) -> &Borrowed
[src]
impl<'a, T> BorrowUninitMut<T> for RefMut<'a, T>
[src]
fn borrow_uninit_mut(&mut self) -> RefMut<T>
[src]
unsafe fn assume_init_mut(&mut self) -> &mut Borrowed
[src]
fn init(&mut self, item: Borrowed) -> &mut Borrowed
[src]
fn default_if_needed(&mut self) -> &mut Borrowed where
Borrowed: Default,
[src]
Borrowed: Default,
fn zeroed_if_needed(&mut self) -> &mut Borrowed where
Borrowed: ZeroValid,
[src]
Borrowed: ZeroValid,
impl<'a, T> From<&'a mut T> for RefMut<'a, T>
[src]
impl<'a, T> From<&'a mut MaybeUninit<T>> for RefMut<'a, T>
[src]
fn from(value: &'a mut MaybeUninit<T>) -> Self
[src]
Auto Trait Implementations
impl<'a, T> Unpin for RefMut<'a, T>
impl<'a, T> Send for RefMut<'a, T> where
T: Send,
T: Send,
impl<'a, T> Sync for RefMut<'a, T> where
T: Sync,
T: Sync,
Blanket Implementations
impl<T> AnyValid for T
[src]
impl<T> PtrCount for T
[src]
type Item = T
Either Self or type of item in the slice.
fn ptr_count(&Self) -> (*const <T as PtrCount>::Item, usize)
[src]
fn ptr_count_mut(&mut Self) -> (*mut <T as PtrCount>::Item, usize)
[src]
fn as_slice(&self) -> &[Self::Item]
[src]
fn as_slice_mut(&mut self) -> &mut [Self::Item] where
Self: AnyValid,
[src]
Self: AnyValid,
impl<T> TakeItem<T> for T
[src]
impl<T> From<T> for T
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,