-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Expand CloneToUninit
documentation.
#133055
Conversation
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.
The unsafe
code in the impl CloneToUninit for MyDst<T>
example looks correct to me, with one note about a comment.
d1b4992
to
ac4eda8
Compare
This comment has been minimized.
This comment has been minimized.
ac4eda8
to
64ec867
Compare
☔ The latest upstream changes (presumably #138155) made this pull request unmergeable. Please resolve the merge conflicts. |
* Clarify relationship to `dyn` after rust-lang#133003. * Add an example of using it with `dyn` as rust-lang#133003 enabled. * Add an example of implementing it. * Add links to Rust Reference for the mentioned concepts. * Mention that its method should rarely be called. * Replace parameter name `dst` with `dest` to avoids confusion between “DeSTination” and “Dynamically-Sized Type”. * Various small corrections.
Rerolling due to lack of response. r? libs |
library/core/src/clone.rs
Outdated
/// | ||
/// // Since `flag` implements `Copy`, we can just copy it. | ||
/// // We use `pointer::write()` instead of assignment because the destination must be | ||
/// // assumed to be uninitialized, whereas an assignment assumes it is initialized. |
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.
I don't think assignment assumes initialized memory for Copy types, and Miri is fine with it: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=2d5d4affa1262eff4a7c5d1a2a7fd14b
The unsound operation is dropping the memory behind the pointer, but for Copy types that doesn't happen.
I'd probably still write it this way, but I don't think we should write something misleading in the comments.
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.
I tried to write a good comment, and was unhappy with all the versions I came up with without also changing the code. I ended up changing the code to .clone_to_uninit()
the flag
too, because it's easier to justify as “most general”. Not sure that makes sense.
— actually, it doesn't make sense, because the easiest way to ensure the desirable no-memory-leaks property would be to strictly use .clone()
on all the sized fields first (so the values are held in drop-tracked locals), then clone the uninit field. And then I don't have to hedge anything about leaking. Please wait while I rewrite again…
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.
I've now rewritten the example so that
- the sized field is not
Copy
, and - neither field is leaked on panic,
and there is one straightforward correct way to do that, so we no longer have any reason to talk about the other options. What do you think?
It also now demonstrates how to avoid memory leaks.
* Give example of how to get the offset of an unsized tail field (prompted by discussion <rust-lang#133055 (comment)>). * Specify the return type. * Add section headings. * Reduce “Visibility is respected…”, to a single sentence.
@bors r+ I think this is good. |
…acrum Expand and organize `offset_of!` documentation. * Give example of how to get the offset of an unsized tail field (prompted by discussion <rust-lang#133055 (comment)>). * Specify the return type. * Add section headings. * Reduce “Visibility is respected…”, to a single sentence. * Move `offset_of_enum` documentation to unstable book (with link to it). * Add `offset_of_slice` documentation in unstable book. r? Mark-Simulacrum
Rollup of 16 pull requests Successful merges: - rust-lang#133055 (Expand `CloneToUninit` documentation.) - rust-lang#137147 (Add exclude to config.toml) - rust-lang#137864 (Don't drop `Rvalue::WrapUnsafeBinder` during GVN) - rust-lang#137890 (doc: clarify that consume can be called after BufReader::peek) - rust-lang#137956 (Add RTN support to rustdoc) - rust-lang#137968 (Properly escape regexes in Python scripts) - rust-lang#138082 (Remove `#[cfg(not(test))]` gates in `core`) - rust-lang#138275 (expose `is_s390x_feature_detected!` from `std::arch`) - rust-lang#138303 (Fix Ptr inconsistency in {Rc,Arc}) - rust-lang#138309 (Add missing doc for intrinsic (Fix PR135334)) - rust-lang#138323 (Expand and organize `offset_of!` documentation.) - rust-lang#138329 (debug-assert that the size_hint is well-formed in `collect`) - rust-lang#138465 (linkchecker: bump html5ever) - rust-lang#138471 (Clean up some tests in tests/ui) - rust-lang#138472 (Add codegen test for rust-lang#129795) - rust-lang#138484 (Use lit span when suggesting suffix lit cast) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of rust-lang#138323 - kpreid:offset-of-doc, r=Mark-Simulacrum Expand and organize `offset_of!` documentation. * Give example of how to get the offset of an unsized tail field (prompted by discussion <rust-lang#133055 (comment)>). * Specify the return type. * Add section headings. * Reduce “Visibility is respected…”, to a single sentence. * Move `offset_of_enum` documentation to unstable book (with link to it). * Add `offset_of_slice` documentation in unstable book. r? Mark-Simulacrum
Rollup merge of rust-lang#133055 - kpreid:clone-uninit-doc, r=Mark-Simulacrum Expand `CloneToUninit` documentation. * Clarify relationship to `dyn` after rust-lang#133003. * Add an example of using it with `dyn` as rust-lang#133003 enabled. * Replace parameter name `dst` with `dest` to avoid confusion between abbreviations for “DeSTination” and “Dynamically-Sized Type”. * Add an example of implementing it. * Add links to Rust Reference for the mentioned concepts. * Mention that its method should rarely be called. * Various small corrections. Please review the `unsafe` code closely, as I am not an expert in the best possible ways to express these operations. (It might also be better to omit the implementation example entirely.) cc `@zachs18` rust-lang#126799
* Give example of how to get the offset of an unsized tail field (prompted by discussion <rust-lang#133055 (comment)>). * Specify the return type. * Add section headings. * Reduce “Visibility is respected…”, to a single sentence.
…mulacrum Expand `CloneToUninit` documentation. * Clarify relationship to `dyn` after rust-lang#133003. * Add an example of using it with `dyn` as rust-lang#133003 enabled. * Replace parameter name `dst` with `dest` to avoid confusion between abbreviations for “DeSTination” and “Dynamically-Sized Type”. * Add an example of implementing it. * Add links to Rust Reference for the mentioned concepts. * Mention that its method should rarely be called. * Various small corrections. Please review the `unsafe` code closely, as I am not an expert in the best possible ways to express these operations. (It might also be better to omit the implementation example entirely.) cc `@zachs18` rust-lang#126799
…acrum Expand and organize `offset_of!` documentation. * Give example of how to get the offset of an unsized tail field (prompted by discussion <rust-lang#133055 (comment)>). * Specify the return type. * Add section headings. * Reduce “Visibility is respected…”, to a single sentence. * Move `offset_of_enum` documentation to unstable book (with link to it). * Add `offset_of_slice` documentation in unstable book. r? Mark-Simulacrum
* Give example of how to get the offset of an unsized tail field (prompted by discussion <rust-lang#133055 (comment)>). * Specify the return type. * Add section headings. * Reduce “Visibility is respected…”, to a single sentence.
dyn
after MakeCloneToUninit
dyn-compatible #133003.dyn
as MakeCloneToUninit
dyn-compatible #133003 enabled.dst
withdest
to avoid confusion between abbreviations for “DeSTination” and “Dynamically-Sized Type”.Please review the
unsafe
code closely, as I am not an expert in the best possible ways to express these operations. (It might also be better to omit the implementation example entirely.)cc @zachs18 #126799