Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add MaybeUninit methods uninit_array, slice_get_ref, `slice_get…
…_mut`
  • Loading branch information
SimonSapin authored and Centril committed Nov 7, 2019
commit 1906c6f714502b1a0de46b9c217dc02570c3fd3e
57 changes: 57 additions & 0 deletions src/libcore/mem/maybe_uninit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,37 @@ impl<T> MaybeUninit<T> {
MaybeUninit { uninit: () }
}

/// Create a new array of `MaybeUninit<T>` items, in an uninitialized state.
///
/// # Examples
///
/// ```
/// #![feature(maybe_uninit_uninit_array, maybe_uninit_extra, maybe_uninit_slice_assume_init)]
/// use std::mem::MaybeUninit;
///
/// let input = b"Foo";
/// let f = u8::to_ascii_uppercase;
///
/// let mut buffer: [MaybeUninit<u8>; 32] = MaybeUninit::uninit_array();
/// let vec;
/// let output = if let Some(buffer) = buffer.get_mut(..input.len()) {
/// buffer.iter_mut().zip(input).for_each(|(a, b)| { a.write(f(b)); });
/// unsafe { MaybeUninit::slice_get_ref(buffer) }
/// } else {
/// vec = input.iter().map(f).collect::<Vec<u8>>();
/// &vec
/// };
///
/// assert_eq!(output, b"FOO");
/// ```
#[unstable(feature = "maybe_uninit_uninit_array", issue = "0")]
#[inline(always)]
pub fn uninit_array<const LEN: usize>() -> [Self; LEN] {
unsafe {
MaybeUninit::<[MaybeUninit<T>; LEN]>::uninit().assume_init()
}
}

/// A promotable constant, equivalent to `uninit()`.
#[unstable(feature = "internal_uninit_const", issue = "0",
reason = "hack to work around promotability")]
Expand Down Expand Up @@ -690,6 +721,32 @@ impl<T> MaybeUninit<T> {
&mut *self.value
}

/// Get a slice of assume-initialized items.
///
/// # Safety
///
/// It is up to the caller to guarantee that the `MaybeUninit<T>` items
/// really are in an initialized state.
/// Calling this when the content is not yet fully initialized causes undefined behavior.
#[unstable(feature = "maybe_uninit_slice_assume_init", issue = "0")]
#[inline(always)]
pub unsafe fn slice_get_ref(slice: &[Self]) -> &[T] {
&*(slice as *const [Self] as *const [T])
}

/// Get a mutable slice of assume-initialized items.
///
/// # Safety
///
/// It is up to the caller to guarantee that the `MaybeUninit<T>` items
/// really are in an initialized state.
/// Calling this when the content is not yet fully initialized causes undefined behavior.
#[unstable(feature = "maybe_uninit_slice_assume_init", issue = "0")]
#[inline(always)]
pub unsafe fn slice_get_mut(slice: &mut [Self]) -> &mut [T] {
&mut *(slice as *mut [Self] as *mut [T])
}

/// Gets a pointer to the first element of the array.
#[unstable(feature = "maybe_uninit_slice", issue = "63569")]
#[inline(always)]
Expand Down