-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Tracking issue for #![feature(maybe_uninit_slice)]
#63569
Comments
Note: #63291 contains some APIs for creating uninitialized slices, but still does not have a good story for working with them, AFAIK. Cc @SimonSapin |
…fJung Adjust tracking issues for `MaybeUninit<T>` gates cc rust-lang#63566 rust-lang#63567 rust-lang#63568 rust-lang#63569 r? @RalfJung
…fJung Adjust tracking issues for `MaybeUninit<T>` gates cc rust-lang#63566 rust-lang#63567 rust-lang#63568 rust-lang#63569 r? @RalfJung
I think another missing function is a way to create a slice of uninitialized data from a Vec. Like this: impl<'a, T> Vec<T> {
pub fn as_uninit(&'a mut self) -> &'a mut [MaybeUninit<T>] {...}
} Which would be used like this: let v = Vec::<u8>::with_capacity(100);
let mut sl = v.as_uninit();
if 0 != some_ffi_func(sl.first_ptr_mut(), sl.len()) {
return Err<()>;
} else {
v.set_len(sl.len());
return Ok(v);
} There's no need for |
Cc @Shnatsel see above -- seems related to rust-lang/rust-clippy#4483 |
@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 Would an API that returns two disjoint |
Back to the APIs tracked in this issue, could they be methods of We have precedent for a lang item for |
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 impl<T> Vec<MaybeUninit<T>> {
/// Transmute this `Vec` of possibly uninitialized data into a normal `Vec`, assuming that the
/// first `len` elements are properly initialized.
pub unsafe fn assume_init(self, len: usize) -> Vec<T> {...}
}
/// I'm not 100% sure it's necessary, but define this for symmetry's sake:
impl<T> Vec<T> {
/// Transmute this `Vec` into a `Vec` of possibly uninitialized data whose length is equal to
/// the original `Vec`'s capacity.
pub fn into_uninit(self) -> Vec<MaybeUninit<T>> {...}
} And it would be used like this: let v_uninit = Vec::<MaybeUninit<u8>>::with_capacity(1024);
let result = some_ffi_func(v_uninit.as_mut_ptr(), v_uninit.capacity());
match result {
Ok(len) => Ok(v_uninit.assume_init(len)),
Err(e) => Err(e)
} Also, regarding the original methods, should the return type be |
That’s |
Why would you use |
@Shnatsel because I need something that owns uninitialized memory, unlike a Another option might be if |
Ah, looks like you want |
Is there any reason these are not directly implemented on the slice?
This would allow |
As far as I know, such inherent impls for primitive types don't actually work -- and making them work requires some magic to be added inside the compiler. I am not sure though. |
Yes, trait coherence rules would normally require this impl to be in the crate that defines the Lines 2592 to 2594 in 21ed505
So it’s technically possible and there is precedent, but I don’t know if the Lang team is keen on expanding that pattern. |
The language change https://blog.rust-lang.org/2020/01/30/Rust-1.41.0.html#relaxed-restrictions-when-implementing-traits doesn’t apply to inherent methods, does it? |
I've just submitted PR #69319 to implement |
rename MaybeUninit slice methods The `first` methods conceptually point to the whole slice, not just its first element, so rename them to be consistent with the raw ptr methods on ref-slices. Also, do the equivalent of rust-lang#76047 for the slice reference getters, and make them part of rust-lang#63569 (so far they somehow had no tracking issue). * first_ptr -> slice_as_ptr * first_ptr_mut -> slice_as_mut_ptr * slice_get_ref -> slice_assume_init_ref * slice_get_mut -> slice_assume_init_mut
rename MaybeUninit slice methods The `first` methods conceptually point to the whole slice, not just its first element, so rename them to be consistent with the raw ptr methods on ref-slices. Also, do the equivalent of rust-lang#76047 for the slice reference getters, and make them part of rust-lang#63569 (so far they somehow had no tracking issue). * first_ptr -> slice_as_ptr * first_ptr_mut -> slice_as_mut_ptr * slice_get_ref -> slice_assume_init_ref * slice_get_mut -> slice_assume_init_mut
rename MaybeUninit slice methods The `first` methods conceptually point to the whole slice, not just its first element, so rename them to be consistent with the raw ptr methods on ref-slices. Also, do the equivalent of rust-lang#76047 for the slice reference getters, and make them part of rust-lang#63569 (so far they somehow had no tracking issue). * first_ptr -> slice_as_ptr * first_ptr_mut -> slice_as_mut_ptr * slice_get_ref -> slice_assume_init_ref * slice_get_mut -> slice_assume_init_mut
rename MaybeUninit slice methods The `first` methods conceptually point to the whole slice, not just its first element, so rename them to be consistent with the raw ptr methods on ref-slices. Also, do the equivalent of rust-lang#76047 for the slice reference getters, and make them part of rust-lang#63569 (so far they somehow had no tracking issue). * first_ptr -> slice_as_ptr * first_ptr_mut -> slice_as_mut_ptr * slice_get_ref -> slice_assume_init_ref * slice_get_mut -> slice_assume_init_mut
The
I suppose this applies to |
Why do the |
Yeah those methods are objectively bad. Nuke: #103133 |
Any update on this feature? |
It's stalled because we wish the language could generalize over containers: rust-lang/libs-team#122. Depending on how long that takes, it might make sense to stabilize the slice methods anyway, but these features haven't been discussed under the assumption that container generalization exists. That's probably the next step: see what makes sense to stabilize even if we get language features that makes some of the methods unnecessary. Then we can evaluate the tradeoffs of adding methods that will probably be pointless in the future but are useful now. |
Ahhh, I was just thinking it would be nice to implement this for Vec too. Anyone who really needs this functionality (might be me, idk) could just copy that one line of source code, so it probably makes sense to wait for the cleaner, container-based implementation. 👍 |
…hat-got-away, r=scottmcm library: Const-stabilize `MaybeUninit::assume_init_mut` FCP completed in rust-lang#86722 (comment) Also moves const-ness of an unstable fn under the `maybe_uninit_slice` gate, Cc rust-lang#63569
…hat-got-away, r=scottmcm library: Const-stabilize `MaybeUninit::assume_init_mut` FCP completed in rust-lang#86722 (comment) Also moves const-ness of an unstable fn under the `maybe_uninit_slice` gate, Cc rust-lang#63569
Rollup merge of rust-lang#131274 - workingjubilee:stabilize-the-one-that-got-away, r=scottmcm library: Const-stabilize `MaybeUninit::assume_init_mut` FCP completed in rust-lang#86722 (comment) Also moves const-ness of an unstable fn under the `maybe_uninit_slice` gate, Cc rust-lang#63569
…way, r=scottmcm library: Const-stabilize `MaybeUninit::assume_init_mut` FCP completed in rust-lang/rust#86722 (comment) Also moves const-ness of an unstable fn under the `maybe_uninit_slice` gate, Cc rust-lang/rust#63569
This is a tracking issue for the RFC "Deprecate
uninitialized
in favor of a newMaybeUninit
type" (rust-lang/rfcs#1892).Most of this has been stabilized, this issue now only tracks the following unstable methods:
Open questions:
slice_as_ptr
/slice_as_mut_ptr
be methods (with someSelf
parameter) instead of functions?The text was updated successfully, but these errors were encountered: