-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Document how MaybeUninit<Struct>
can be initialized.
#81580
Conversation
r? @dtolnay (rust-highfive has picked a reviewer for you, use r? to override) |
The job Click to see the possible cause of the failure (guessed by this bot)
|
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.
Nice!
@bors r+ |
📌 Commit 0974026 has been approved by |
The job Click to see the possible cause of the failure (guessed by this bot)
|
^ @bors r- |
library/core/src/mem/maybe_uninit.rs
Outdated
/// unsafe { addr_of_mut!((*ptr).name).write("Bob".to_string()); } | ||
/// | ||
/// // Initializing the `list` field | ||
/// // If there was a panic here, then the `String` in the `name` field would be leaked. |
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.
Language nitpick: would be leaked
--> leaks
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.
Maybe the leak can be avoided by reordering the code a bit?
let name = ...
let list = ...
unsafe {
addr_of_mut!((*ptr).name).write(name);
addr_of_mut!((*ptr).list).write(list);
}
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.
@the8472 In this case yes, but I wanted this to translate to how one would initialize a large struct field by field.
If the struct had been a heap allocated struct with a large array field instead of a Vec
, it wouldn't be possible to construct the array on the stack before moving it to the field in debug builds, since it would overflow the stack.
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.
Well, afaik currently there is no guarantee that passing a large array directly to function arguments gets optimized any better than first assigning it to a named variable. You're at the mercy of the optimizer in either case since there's no placement API. rust-lang/rfcs#2884 is still under discussion.
I think to initialize a large array without blowing the stack in debug mode you would have to loop over each item.
@bors r+ |
📌 Commit 21c2343 has been approved by |
Rollup of 7 pull requests Successful merges: - rust-lang#80011 (Stabilize `peekable_next_if`) - rust-lang#81580 (Document how `MaybeUninit<Struct>` can be initialized.) - rust-lang#81610 (BTreeMap: make Ord bound explicit, compile-test its absence) - rust-lang#81664 (Avoid a hir access inside get_static) - rust-lang#81675 (Make rustdoc respect `--error-format short` in doctests) - rust-lang#81753 (Never MIR inline functions with a different instruction set) - rust-lang#81795 (Small refactor with Iterator::reduce) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
No description provided.