-
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
expand the documentation on the Unpin
trait
#53104
Conversation
provides an overview of the Pin API which the trait is for, and show how it can be used in making self referencial structs part of rust-lang#49150
(rust_highfive has picked a reviewer for you, use r? to override) |
6374346
to
6845dc4
Compare
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
r? @RalfJung |
src/libcore/marker.rs
Outdated
@@ -603,15 +603,99 @@ unsafe impl<T: ?Sized> Freeze for *mut T {} | |||
unsafe impl<'a, T: ?Sized> Freeze for &'a T {} | |||
unsafe impl<'a, T: ?Sized> Freeze for &'a mut T {} | |||
|
|||
/// Types which can be moved out of a `PinMut`. | |||
/// A trait that indicates that it is safe to move an object of a type implementing it. |
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.
Rustdoc docs really like
/// A single sentence summary
///
/// Real docs start here...
The old code was in this style, this patch throws it out.
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.
formatting/capitalization/punctuation nits
Thanks for this!
src/libcore/marker.rs
Outdated
/// [`Deref`]: ../ops/trait.Deref.html | ||
/// [`swap`]: ../mem/fn.swap.html | ||
/// | ||
/// # example |
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.
This should be /// # Examples
src/libcore/marker.rs
Outdated
/// use std::marker::Pinned; | ||
/// use std::ptr::NonNull; | ||
/// | ||
/// // this is a self referencial struct since the slice field points to the data field. |
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.
This
src/libcore/marker.rs
Outdated
/// use std::ptr::NonNull; | ||
/// | ||
/// // this is a self referencial struct since the slice field points to the data field. | ||
/// // we cannot inform the compiler about that with a normal reference, |
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.
We
src/libcore/marker.rs
Outdated
/// // this is a self referencial struct since the slice field points to the data field. | ||
/// // we cannot inform the compiler about that with a normal reference, | ||
/// // since this pattern cannot be described with the usual borrowing rules. | ||
/// // instead we use a raw pointer, though one which is known to not be null, |
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.
Instead
src/libcore/marker.rs
Outdated
/// // we cannot inform the compiler about that with a normal reference, | ||
/// // since this pattern cannot be described with the usual borrowing rules. | ||
/// // instead we use a raw pointer, though one which is known to not be null, | ||
/// // since we know its pointing at the string. |
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.
it's
src/libcore/marker.rs
Outdated
/// } | ||
/// | ||
/// impl Unmovable { | ||
/// // to ensure the data doesn't move when the function returns, |
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.
To
src/libcore/marker.rs
Outdated
/// impl Unmovable { | ||
/// // to ensure the data doesn't move when the function returns, | ||
/// // we place it in the heap where it will stay for the lifetime of the object, | ||
/// // and the only way to access it would be through a pointer to it |
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.
needs a period at the end
src/libcore/marker.rs
Outdated
/// } | ||
/// | ||
/// let unmoved = Unmovable::new("hello".to_string()); | ||
/// // the pointer should point to the correct location, |
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
src/libcore/marker.rs
Outdated
/// let unmoved = Unmovable::new("hello".to_string()); | ||
/// // the pointer should point to the correct location, | ||
/// // so long as the struct hasn't moved. | ||
/// // meanwhile, we are free to move the pointer around |
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.
Meanwhile, and a period at the end
src/libcore/marker.rs
Outdated
/// let mut still_unmoved = unmoved; | ||
/// assert_eq!(still_unmoved.slice, NonNull::from(&still_unmoved.data)); | ||
/// | ||
/// // now the only way to access to data (safely) is immutably, |
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.
Now
I disagree with the changed introduction: However, I think the examples are very useful. :) And we certainly need that explanation about If you want to help push pinning over the stabilization line, I think that a good place to start: Having a central location (the |
removed the things that should go into module-level documentation. will try to help with the |
src/libcore/marker.rs
Outdated
/// this trait cannot prevent types from moving by itself. | ||
/// | ||
/// Instead it can be used to prevent moves through the type system, | ||
/// by controlling the behavior of special pointers types like [`PinMut`], |
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.
"pointer types" (spurious "s")
src/libcore/marker.rs
Outdated
/// | ||
/// Instead it can be used to prevent moves through the type system, | ||
/// by controlling the behavior of special pointers types like [`PinMut`], | ||
/// which "pin" the type in place by wrapping it in a type which can only be dereferenced immutably. |
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.
Does the "by ..." part help? It doesn't really explain anything, does it?
Maybe just link to the PinMut docs. Or maybe say something along the lines of "by not allowing it to be moved out e.g. through mutable references (unless T: Unpin
)".
src/libcore/marker.rs
Outdated
/// which "pin" the type in place by wrapping it in a type which can only be dereferenced immutably. | ||
/// | ||
/// Implementing this trait lifts the restrictions of pinning off a type, | ||
/// which then allows it to move out of said pointers with functions such as [`swap`]. |
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 remark to swap
is a bit mysterious. Maybe add an example?
LGTM! Just some minor comments. |
src/libcore/marker.rs
Outdated
/// by controlling the behavior of special pointers types like [`PinMut`], | ||
/// which "pin" the type in place by wrapping it in a type which can only be dereferenced immutably. | ||
/// by controlling the behavior of special pointer types like [`PinMut`], | ||
/// which "pin" the type in place by not allowing it to be moved out via mutable references. |
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 would remove the "via" part or make it a "for example" -- after all, PinMut
prevents all ways to move something out. It doesn't just prevent moving out via &mut
.
src/libcore/marker.rs
Outdated
/// | ||
/// let mut string = "this".to_string(); | ||
/// let mut pinned_string = PinMut::new(&mut string); | ||
/// replace(&mut *pinned_string, "other".to_string()); |
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'd add a comment in the code here saying that the &mut *
is where we make use of the Unpin
Awesome, thanks! @bors r+ |
📌 Commit 6ae915b has been approved by |
@bors p=rollup |
@bors r+ rollup |
💡 This pull request was already approved, no need to approve it again.
|
📌 Commit 6ae915b has been approved by |
expand the documentation on the `Unpin` trait provides an overview of the Pin API which the trait is for, and show how it can be used in making self referencial structs part of rust-lang#49150
Rollup of 17 pull requests Successful merges: - #53030 (Updated RELEASES.md for 1.29.0) - #53104 (expand the documentation on the `Unpin` trait) - #53213 (Stabilize IP associated constants) - #53296 (When closure with no arguments was expected, suggest wrapping) - #53329 (Replace usages of ptr::offset with ptr::{add,sub}.) - #53363 (add individual docs to `core::num::NonZero*`) - #53370 (Stabilize macro_vis_matcher) - #53393 (Mark libserialize functions as inline) - #53405 (restore the page title after escaping out of a search) - #53452 (Change target triple used to check for lldb in build-manifest) - #53462 (Document Box::into_raw returns non-null ptr) - #53465 (Remove LinkMeta struct) - #53492 (update lld submodule to include RISCV patch) - #53496 (Fix typos found by codespell.) - #53521 (syntax: Optimize some literal parsing) - #53540 (Moved issue-53157.rs into src/test/ui/consts/const-eval/) - #53551 (Avoid some Place clones.) Failed merges: r? @ghost
move the Pin API into its own module for centralized documentation This implements the change proposed by @withoutboats in #49150, as suggested by @RalfJung in the review of #53104, along with the documentation that was originally in it, that was deemed more appropriate in module-level documentation. r? @RalfJung
provides an overview of the Pin API which the trait is for,
and show how it can be used in making self referencial structs
part of #49150