Skip to main content

Crate wref

Crate wref 

Source
Expand description

Write-only references.

A library solution to a useful language feature: write-only references. At the moment, there is a gap in the functionality of Rust’s borrows:

ReadWrite
&mut T
&T
WRef<'_, T>
don’t write any code

This kind of functionality is useful because it doesn’t care whether the reference points to initialized memory; it’s always sound to write to memory regardless of whether it’s initialized, and it’s only unsound to read uninitialized memory.

You might think this is possible with MaybeUninit, but it’s actually not: with &mut MaybeUninit<T>, you can write a value to the memory to initialize it, but you can also de-initialize it by writing a value of type MaybeUninit<T> directly:

// initialized
let data = &mut MaybeUninit::new(1);

// now de-initialized!
*data = MaybeUninit::uninit();

Since &mut MaybeUninit<T> can de-initialize data, it cannot be used as a general write-reference for data that must be initialized. So, this crate exists.

While the crate itself uses unsafe code to ensure its invariants without triggering undefined behaviour, it can be used by other code to safely generalize between &mut T and &mut MaybeUninit<T> in cases where only writing is needed.

Structs§

Iter
Iterator returned by <WRef<'_, [T]>>::iter.
WRef
Write-only reference type.

Functions§

from_maybe_uninit_mut
Alias for WRef::from_maybe_uninit_mut.
from_maybe_uninit_slice_mut
Alias for WRef::from_maybe_uninit_slice_mut.
from_mut
Alias for WRef::from_mut.