Repository navigation
Tracking issue for #![feature(maybe_uninit_slice)] #63569
Copy link
Copy link
Closed
Labels
A-raw-pointersArea: raw pointers, MaybeUninit, NonNullArea: raw pointers, MaybeUninit, NonNullA-sliceArea: `[T]`Area: `[T]`B-unstableBlocker: Implemented in the nightly compiler and unstable.Blocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCI-libs-radarLibs issues that are tracked on the team's radar.Libs issues that are tracked on the team's radar.T-libs-api[DEPRECATED; DO NOT USE][DEPRECATED; DO NOT USE]disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.This issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.The final comment period is finished for this PR / Issue.requires-nightlyThis issue requires a nightly compiler in some way. When possible, use a F-* label instead.This issue requires a nightly compiler in some way. When possible, use a F-* label instead.
Milestone
Description
This is a tracking issue for the RFC "Deprecate uninitialized in favor of a new MaybeUninit type" (rust-lang/rfcs#1892).
Most of this has been stabilized, this issue now only tracks the below unstable methods.
Public API
impl<T> [MaybeUninit<T>] {
pub unsafe fn assume_init_drop(&mut self);
pub const unsafe fn assume_init_ref(&self) -> &[T];
pub const unsafe fn assume_init_mut(&mut self) -> &mut [T];
pub const fn slice_as_ptr(this: &[MaybeUninit<T>]) -> *const T;
pub const fn slice_as_mut_ptr(this: &mut [MaybeUninit<T>]) -> *mut T;
}Steps / History
- Implementation
- Make slice methods inherent: Add inherent versions of MaybeUninit methods for slices #129259
- Ensure documentation has examples
- Final comment period (FCP)
- Stabilization PR
Unresolved Questions
- Should
slice_as_ptr/slice_as_mut_ptrbe methods (with someSelfparameter) instead of functions? slice_as_ptr/slice_as_mut_ptrget rid of the slice length, so.add(n)needs to be used on the result instead of a safer[n]. See accepted ACP RemoveMaybeUninit::slice_as_(mut_)ptrand optionally add*const/*mut MaybeUninit<T> -> *const/*mut Ttype safe conversions libs-team#245 and its incomplete implementation Nuke slice_as{,_mut}_ptr methods of MaybeUninit #103133
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
A-raw-pointersArea: raw pointers, MaybeUninit, NonNullArea: raw pointers, MaybeUninit, NonNullA-sliceArea: `[T]`Area: `[T]`B-unstableBlocker: Implemented in the nightly compiler and unstable.Blocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCI-libs-radarLibs issues that are tracked on the team's radar.Libs issues that are tracked on the team's radar.T-libs-api[DEPRECATED; DO NOT USE][DEPRECATED; DO NOT USE]disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.This issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.The final comment period is finished for this PR / Issue.requires-nightlyThis issue requires a nightly compiler in some way. When possible, use a F-* label instead.This issue requires a nightly compiler in some way. When possible, use a F-* label instead.
Activity
RalfJung commentedon Aug 14, 2019
Note: #63291 contains some APIs for creating uninitialized slices, but still does not have a good story for working with them, AFAIK.
Cc @SimonSapin
Rollup merge of rust-lang#63570 - rust-lang:maybe-uninit-gates, r=Ral…
Rollup merge of rust-lang#63570 - rust-lang:maybe-uninit-gates, r=Ral…
asomers commentedon Sep 1, 2019
I think another missing function is a way to create a slice of uninitialized data from a Vec. Like this:
Which would be used like this:
There's no need for
Vec::uninit()orVec::assume_init(), becauseVec::with_capacityandVec::set_lenalready do pretty much the same thing. And for simple cases like this oneVec::as_mut_ptr()would suffice without the formality of creating a slice ofMaybeUninits. However, the slice has the advantage that it knows its own length and it knows the lifetime of its data. The latter is especially useful when working with FFI functions that initialize the data asynchronously, such asaio_read.RalfJung commentedon Sep 1, 2019
Cc @Shnatsel see above -- seems related to rust-lang/rust-clippy#4483
SimonSapin commentedon Sep 2, 2019
@asomers I agree that something like this would be good to have. However your proposal seems ambiguous: does the returned slice include the entire allocation (indices
0..capacity) or only the range that is not known to be initialized (indiceslen..capacity)?Would an API that returns two disjoint
(&mut [T], &mut [MaybeUninit<T>])slices for indices0..lenandlen..capacityalso be useful?SimonSapin commentedon Sep 2, 2019
Back to the APIs tracked in this issue, could they be methods of
[MaybeUninit<T>]that take&selfinstead of associated functions ofMaybeUninit<T>? (Though it’s not obvious what they should be called in that case.)We have precedent for a lang item for
impl [u8] {…}asomers commentedon Sep 3, 2019
Good questions @SimonSapin. I think that the returned slice should include the entire allocation (after all, elements that are already initialized can still be represented as
MaybeUninit. Also, I realized that the method I proposed isn't even good enough for my particular use case. I need a way to create an owned, maybe-uninitialized slice in a known memory location, and then transform that into an initialized vec. This is important for Tokio, whose asynchronous nature can thwart the borrow-checker.And it would be used like this:
Also, regarding the original methods, should the return type be
Option<NonNull<T>>? That way they could returnNonefor 0-length slices.SimonSapin commentedon Sep 3, 2019
That’s
Box::new_uninit#63291Shnatsel commentedon Sep 3, 2019
Why would you use
Vec<MaybeUninit<T>>?Vecalready encapsulates work with uninitialized memory - you can write to its capacity and then.set_len()to update the length. Or just use one of the myriad of methods that append to it safely.asomers commentedon Sep 3, 2019
@Shnatsel because I need something that owns uninitialized memory, unlike a
*mut u8. I'm usingaio_read, which asynchronously initializes the memory. There's no way for me to supply a&mut Vecreference toaio_readand persuade the borrow checker that theVecwill still be around by the time that the operation is complete (with notification delivered via kqueue). Making matters more complicated is the fact that my stack is 5 crates deep.Another option might be if
[T]implementedBorrowMut<[MaybeUninit<T>]>. Then I think I could make things work.52 remaining items
rust-rfcbot commentedon Nov 2, 2025
The final comment period, with a disposition to merge, as per the review above, is now complete.
As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.
This will be merged soon.
Auto merge of #149102 - bend-n:maybe_uninit_slice, r=joboet
Rollup merge of rust-lang#149102 - bend-n:maybe_uninit_slice, r=joboet
Unrolled build for #149102
Rollup merge of #149102 - bend-n:maybe_uninit_slice, r=joboet
Rollup merge of rust-lang#149102 - bend-n:maybe_uninit_slice, r=joboet
Upgrade to Rust 1.95.0-nightly 1/3
Remove MaybeUninit slice helpers now stabilized in std (binius-zk#1606)
Rollup merge of #149102 - bend-n:maybe_uninit_slice, r=joboet