-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Deprecate uninit_array #101179
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
Deprecate uninit_array #101179
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 | ||||
---|---|---|---|---|---|---|
|
@@ -879,7 +879,7 @@ where | |||||
} | ||||||
} | ||||||
|
||||||
let mut array = MaybeUninit::uninit_array::<N>(); | ||||||
let mut array = MaybeUninit::uninit().transpose(); | ||||||
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. Is it worth adding a type annotation?
Suggested change
There's a lot of inference going on here making the code quite hard to follow. 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. Hmmm, yeah I was wondering about this too. My reasoning for this being ok was that if the compiler manages to infer the types then I don't need to think about them. I'm guessing this is flawed because if you have no constraints at all, then the compiler will happily give you an i32. An interesting question arises: are integers and floats the only two places where the compiler will ever pull a type out of its 🍑 if there are no constraints? If so, my gut feeling is that we should get rid of that behavior (separate discussion, I know) since not having 🍑 types would let you do what I'm doing now where I say, "well, the compiler did some inferences so whatever the types are they must be correct". Or maybe I'm thinking too hard about this and type annotations would make human consumption easier (though I'm still curious about the above questions). 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. In safe code I tend to follow the same approach -- if the compiler found a type that makes it all work, then that seems right (but I will still sometimes add type annotations to make the code more readable). In unsafe code though, I don't like trusting the compiler like that. Types are much less meaningful in unsafe code (that's why the code is unsafe to begin with), so IMO type inference should only be relied upon for absolutely obvious cases -- which this one is not. 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. Good point, will do. |
||||||
let mut guard = Guard { array_mut: &mut array, initialized: 0 }; | ||||||
|
||||||
for _ in 0..N { | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -138,7 +138,7 @@ use crate::slice; | |
/// unsafe { mem::transmute::<_, [Vec<u32>; 1000]>(data) } | ||
/// }; | ||
/// | ||
/// assert_eq!(&data[0], &[42]); | ||
/// assert_eq!(&data[42], &[42]); | ||
/// ``` | ||
/// | ||
/// You can also work with partially initialized arrays, which could | ||
|
@@ -148,10 +148,9 @@ use crate::slice; | |
/// use std::mem::MaybeUninit; | ||
/// use std::ptr; | ||
/// | ||
/// // Create an uninitialized array of `MaybeUninit`. The `assume_init` is | ||
/// // safe because the type we are claiming to have initialized here is a | ||
/// // bunch of `MaybeUninit`s, which do not require initialization. | ||
/// let mut data: [MaybeUninit<String>; 1000] = unsafe { MaybeUninit::uninit().assume_init() }; | ||
/// // Create an uninitialized array of `MaybeUninit`. | ||
/// const UNINIT_STRING: MaybeUninit<String> = MaybeUninit::uninit(); | ||
/// let mut data = [UNINIT_STRING; 1000]; | ||
/// // Count the number of elements we have assigned. | ||
/// let mut data_len: usize = 0; | ||
/// | ||
|
@@ -348,6 +347,10 @@ impl<T> MaybeUninit<T> { | |
#[rustc_const_unstable(feature = "const_maybe_uninit_uninit_array", issue = "96097")] | ||
#[must_use] | ||
#[inline(always)] | ||
#[deprecated( | ||
since = "nightly", | ||
note = "Use const array initialization instead or the transpose methods." | ||
)] | ||
pub const fn uninit_array<const N: usize>() -> [Self; N] { | ||
// SAFETY: An uninitialized `[MaybeUninit<_>; LEN]` is valid. | ||
unsafe { MaybeUninit::<[MaybeUninit<T>; N]>::uninit().assume_init() } | ||
|
@@ -1297,7 +1300,8 @@ impl<T, const N: usize> MaybeUninit<[T; N]> { | |
/// let data: [MaybeUninit<u8>; 1000] = MaybeUninit::uninit().transpose(); | ||
/// ``` | ||
#[unstable(feature = "maybe_uninit_uninit_array_transpose", issue = "96097")] | ||
pub fn transpose(self) -> [MaybeUninit<T>; N] { | ||
#[inline(always)] | ||
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. Please just use Then we can discuss changing it to 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. I don't like the scope creep anyway, so lemme just pull this out into a seperate PR. |
||
pub const fn transpose(self) -> [MaybeUninit<T>; N] { | ||
// SAFETY: T and MaybeUninit<T> have the same layout | ||
unsafe { super::transmute_copy(&ManuallyDrop::new(self)) } | ||
} | ||
|
@@ -1316,7 +1320,8 @@ impl<T, const N: usize> [MaybeUninit<T>; N] { | |
/// let data: MaybeUninit<[u8; 1000]> = data.transpose(); | ||
/// ``` | ||
#[unstable(feature = "maybe_uninit_uninit_array_transpose", issue = "96097")] | ||
pub fn transpose(self) -> MaybeUninit<[T; N]> { | ||
#[inline(always)] | ||
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. (here too) |
||
pub const fn transpose(self) -> MaybeUninit<[T; N]> { | ||
// SAFETY: T and MaybeUninit<T> have the same layout | ||
unsafe { super::transmute_copy(&ManuallyDrop::new(self)) } | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.