-
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
Add examples for std::pin::Pin new() and into_inner() methods #104195
Changes from 3 commits
fe2f40a
8b5379e
dfa90e7
fd8ebea
6f48b60
a4ee02b
06c2ce0
0b1ead3
7a47e2c
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 |
---|---|---|
|
@@ -485,6 +485,16 @@ impl<P: Deref<Target: Unpin>> Pin<P> { | |
/// | ||
/// Unlike `Pin::new_unchecked`, this method is safe because the pointer | ||
/// `P` dereferences to an [`Unpin`] type, which cancels the pinning guarantees. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use std::pin::Pin; | ||
/// | ||
/// let val: u8 = 5; | ||
/// // Wrap the value in a pin to make sure it doesn't move | ||
/// let pinned: Pin<&u8> = Pin::new(&val); | ||
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. It seems like an example of Pin with a shared reference is probably not the best bet -- most users will not want that. I think we should demonstrate with a &mut reference at least. 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. let mut val: u8 = 5;
let mut pinned: Pin<&mut u8> = Pin::new(&mut val);
println!("{}", pinned); // 5
pinned.as_mut().set(10);
println!("{}", pinned); // 10 Would this work? |
||
/// ``` | ||
#[inline(always)] | ||
#[rustc_const_unstable(feature = "const_pin", issue = "76654")] | ||
#[stable(feature = "pin", since = "1.33.0")] | ||
|
@@ -498,6 +508,17 @@ impl<P: Deref<Target: Unpin>> Pin<P> { | |
/// | ||
/// This requires that the data inside this `Pin` is [`Unpin`] so that we | ||
ch-iv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// can ignore the pinning invariants when unwrapping it. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use std::pin::Pin; | ||
/// | ||
/// let pinned: Pin<&u8> = Pin::new(&5); | ||
/// // Unwrap the pin to get a reference to the value | ||
/// let r = Pin::into_inner(pinned); | ||
/// assert_eq!(*r, 5); | ||
/// ``` | ||
#[inline(always)] | ||
#[rustc_const_unstable(feature = "const_pin", issue = "76654")] | ||
#[stable(feature = "pin_into_inner", since = "1.39.0")] | ||
|
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 isn't quite accurate. Since
Target: Unpin
, the value can move again.I don't like my wording here a lot, feel free to improve it, but something like this is better.
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 guess that a better option is
Pin the value in memory to make sure that during the program lifecycle the value is not moved in another memory address
?