-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add MaybeUninit
methods uninit_array
, slice_get_ref
, slice_get_mut
#65580
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -258,6 +258,43 @@ impl<T> MaybeUninit<T> { | |
MaybeUninit { uninit: () } | ||
} | ||
|
||
/// Create a new array of `MaybeUninit<T>` items, in an uninitialized state. | ||
/// | ||
/// Note: in a future Rust version this method may become unnecessary | ||
/// when array literal syntax allows | ||
/// [repeating const expressions](https://github.com/rust-lang/rust/issues/49147). | ||
/// The example below could then use `let mut buf = [MaybeUninit::<u8>::uninit(); 32];`. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```no_run | ||
/// #![feature(maybe_uninit_uninit_array, maybe_uninit_extra, maybe_uninit_slice_assume_init)] | ||
/// | ||
/// use std::mem::MaybeUninit; | ||
/// | ||
/// extern "C" { | ||
/// fn read_into_buffer(ptr: *mut u8, max_len: usize) -> usize; | ||
/// } | ||
/// | ||
/// /// Returns a (possibly smaller) slice of data that was actually read | ||
/// fn read(buf: &mut [MaybeUninit<u8>]) -> &[u8] { | ||
/// unsafe { | ||
/// let len = read_into_buffer(buf.as_mut_ptr() as *mut u8, buf.len()); | ||
/// MaybeUninit::slice_get_ref(&buf[..len]) | ||
/// } | ||
/// } | ||
/// | ||
/// let mut buf: [MaybeUninit<u8>; 32] = MaybeUninit::uninit_array(); | ||
/// let data = read(&mut buf); | ||
/// ``` | ||
#[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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a bit of a bummer. We should be able to use: [Self::UNINIT; LEN] here were it not for the fact that we would get "error: array lengths can't depend on generic parameters" (solvable by e.g. allowing Can you leave a FIXME for now? |
||
} | ||
} | ||
|
||
/// A promotable constant, equivalent to `uninit()`. | ||
#[unstable(feature = "internal_uninit_const", issue = "0", | ||
reason = "hack to work around promotability")] | ||
|
@@ -690,6 +727,32 @@ impl<T> MaybeUninit<T> { | |
&mut *self.value | ||
} | ||
|
||
/// Assuming all the elements are initialized, get a slice to them. | ||
/// | ||
/// # Safety | ||
/// | ||
/// It is up to the caller to guarantee that the `MaybeUninit<T>` elements | ||
/// 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]) | ||
} | ||
|
||
/// Assuming all the elements are initialized, get a mutable slice to them. | ||
/// | ||
/// # Safety | ||
/// | ||
/// It is up to the caller to guarantee that the `MaybeUninit<T>` elements | ||
/// 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)] | ||
|
Uh oh!
There was an error while loading. Please reload this page.