-
-
Notifications
You must be signed in to change notification settings - Fork 14.8k
Add "# Safety" and "# Examples" section in std::mem::uninitialized
#151033
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
base: main
Are you sure you want to change the base?
Changes from all commits
45d065c
2c7cad5
de12e31
c7b6315
47a2785
0a9127d
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 |
|---|---|---|
|
|
@@ -705,9 +705,24 @@ pub const unsafe fn zeroed<T>() -> T { | |
| /// Therefore, it is immediate undefined behavior to call this function on nearly all types, | ||
| /// including integer types and arrays of integer types, and even if the result is unused. | ||
| /// | ||
| /// # Safety | ||
| /// | ||
| /// This function is highly unsafe, as calling this function on nearly all types causes | ||
| /// undefined behavior. You should **always** prefer using [`MaybeUninit<T>`] instead. | ||
| /// | ||
| /// If you absolutely must use this function, the following conditions must be upheld: | ||
| /// | ||
| /// - `T` must be *valid* with any sequence of bytes of the appropriate length, | ||
| /// initialized or uninitialized. | ||
|
Comment on lines
+715
to
+716
Contributor
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 think we should hint that non-ZST "... That is, if
Contributor
Author
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 agree, but wouldn't that suggest a correct usage of this function? I removed that part because it would suggest that using
Contributor
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 think mentioning the |
||
| /// - `T` must be *[inhabited]*, i.e. possible to construct. This means that types | ||
| /// like zero-variant enums and [`!`] are unsound to construct with this function. | ||
| /// - You must use the value only in ways which do not violate any *safety* | ||
| /// invariants of the type. | ||
| /// | ||
| /// [uninit]: MaybeUninit::uninit | ||
| /// [assume_init]: MaybeUninit::assume_init | ||
| /// [inv]: MaybeUninit#initialization-invariant | ||
| /// [inhabited]: https://doc.rust-lang.org/reference/glossary.html#inhabited | ||
| #[inline(always)] | ||
| #[must_use] | ||
| #[deprecated(since = "1.39.0", note = "use `mem::MaybeUninit` instead")] | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change from "If you absolutely must use this function ..." to "On legacy codebases that use this function ..." or something similar, because nobody should have an absolute need to use it today :)