Skip to content
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

Merged
merged 7 commits into from
Aug 21, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/libcore/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,16 +609,27 @@ unsafe impl<'a, T: ?Sized> Freeze for &'a mut T {}
/// 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`],
/// 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.
Copy link
Member

@RalfJung RalfJung Aug 17, 2018

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.

///
/// 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`].
/// which then allows it to move out of said pointers, with functions such as [`replace`].
///
/// So this, for example, can only be done on types implementing `Unpin`:
///
/// ```rust
/// #![feature(pin)]
/// use std::mem::{PinMut, replace};
///
/// let mut string = "this".to_string();
/// let mut pinned_string = PinMut::new(&mut string);
/// replace(&mut *pinned_string, "other".to_string());
Copy link
Member

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

/// ```
///
/// This trait is automatically implemented for almost every type.
///
/// [`PinMut`]: ../mem/struct.PinMut.html
/// [`swap`]: ../mem/fn.swap.html
/// [`replace`]: ../mem/fn.replace.html
#[unstable(feature = "pin", issue = "49150")]
pub auto trait Unpin {}

Expand Down