Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9997114

Browse files
fu5haManishearth
authored andcommittedJan 7, 2024
improve Pin::new_unchecked docs
1 parent d7a886a commit 9997114

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed
 

Diff for: ‎library/core/src/pin.rs

+16-10
Original file line numberDiff line numberDiff line change
@@ -1226,21 +1226,24 @@ impl<Ptr: Deref<Target: Unpin>> Pin<Ptr> {
12261226

12271227
impl<Ptr: Deref> Pin<Ptr> {
12281228
/// Construct a new `Pin<Ptr>` around a reference to some data of a type that
1229-
/// may or may not implement `Unpin`.
1229+
/// may or may not implement [`Unpin`].
12301230
///
1231-
/// If `pointer` dereferences to an `Unpin` type, `Pin::new` should be used
1231+
/// If `pointer` dereferences to an [`Unpin`] type, [`Pin::new`] should be used
12321232
/// instead.
12331233
///
12341234
/// # Safety
12351235
///
12361236
/// This constructor is unsafe because we cannot guarantee that the data
1237-
/// pointed to by `pointer` is pinned, meaning that the data will not be moved or
1238-
/// its storage invalidated until it gets dropped. If the constructed `Pin<Ptr>` does
1239-
/// not guarantee that the data `Ptr` points to is pinned, that is a violation of
1240-
/// the API contract and may lead to undefined behavior in later (safe) operations.
1237+
/// pointed to by `pointer` is pinned. At its core, pinning a value means making the
1238+
/// guarantee that the value's data will not be moved nor have its storage invalidated until
1239+
/// it gets dropped. For a more thorough explanation of pinning, see the [`pin` module docs].
12411240
///
1242-
/// By using this method, you are making a promise about the `Ptr::Deref` and
1243-
/// `Ptr::DerefMut` implementations, if they exist. Most importantly, they
1241+
/// If the caller that is constructing this `Pin<Ptr>` does not ensure that the data `Ptr`
1242+
/// points to is pinned, that is a violation of the API contract and may lead to undefined
1243+
/// behavior in later (even safe) operations.
1244+
///
1245+
/// By using this method, you are also making a promise about the [`Deref`] and
1246+
/// [`DerefMut`] implementations of `Ptr`, if they exist. Most importantly, they
12441247
/// must not move out of their `self` arguments: `Pin::as_mut` and `Pin::as_ref`
12451248
/// will call `DerefMut::deref_mut` and `Deref::deref` *on the pointer type `Ptr`*
12461249
/// and expect these methods to uphold the pinning invariants.
@@ -1251,7 +1254,9 @@ impl<Ptr: Deref> Pin<Ptr> {
12511254
///
12521255
/// For example, calling `Pin::new_unchecked` on an `&'a mut T` is unsafe because
12531256
/// while you are able to pin it for the given lifetime `'a`, you have no control
1254-
/// over whether it is kept pinned once `'a` ends:
1257+
/// over whether it is kept pinned once `'a` ends, and therefore cannot uphold the
1258+
/// guarantee that a value, once pinned, remains pinned until it is dropped:
1259+
///
12551260
/// ```
12561261
/// use std::mem;
12571262
/// use std::pin::Pin;
@@ -1285,7 +1290,7 @@ impl<Ptr: Deref> Pin<Ptr> {
12851290
/// // ...
12861291
/// }
12871292
/// drop(pin);
1288-
1293+
///
12891294
/// let content = Rc::get_mut(&mut x).unwrap(); // Potential UB down the road ⚠️
12901295
/// // Now, if `x` was the only reference, we have a mutable reference to
12911296
/// // data that we pinned above, which we could use to move it as we have
@@ -1346,6 +1351,7 @@ impl<Ptr: Deref> Pin<Ptr> {
13461351
/// ```
13471352
///
13481353
/// [`mem::swap`]: crate::mem::swap
1354+
/// [`pin` module docs]: self
13491355
#[lang = "new_unchecked"]
13501356
#[inline(always)]
13511357
#[rustc_const_unstable(feature = "const_pin", issue = "76654")]

0 commit comments

Comments
 (0)
Failed to load comments.