@@ -1226,21 +1226,24 @@ impl<Ptr: Deref<Target: Unpin>> Pin<Ptr> {
1226
1226
1227
1227
impl < Ptr : Deref > Pin < Ptr > {
1228
1228
/// 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`] .
1230
1230
///
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
1232
1232
/// instead.
1233
1233
///
1234
1234
/// # Safety
1235
1235
///
1236
1236
/// 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].
1241
1240
///
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
1244
1247
/// must not move out of their `self` arguments: `Pin::as_mut` and `Pin::as_ref`
1245
1248
/// will call `DerefMut::deref_mut` and `Deref::deref` *on the pointer type `Ptr`*
1246
1249
/// and expect these methods to uphold the pinning invariants.
@@ -1251,7 +1254,9 @@ impl<Ptr: Deref> Pin<Ptr> {
1251
1254
///
1252
1255
/// For example, calling `Pin::new_unchecked` on an `&'a mut T` is unsafe because
1253
1256
/// 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
+ ///
1255
1260
/// ```
1256
1261
/// use std::mem;
1257
1262
/// use std::pin::Pin;
@@ -1285,7 +1290,7 @@ impl<Ptr: Deref> Pin<Ptr> {
1285
1290
/// // ...
1286
1291
/// }
1287
1292
/// drop(pin);
1288
-
1293
+ ///
1289
1294
/// let content = Rc::get_mut(&mut x).unwrap(); // Potential UB down the road ⚠️
1290
1295
/// // Now, if `x` was the only reference, we have a mutable reference to
1291
1296
/// // data that we pinned above, which we could use to move it as we have
@@ -1346,6 +1351,7 @@ impl<Ptr: Deref> Pin<Ptr> {
1346
1351
/// ```
1347
1352
///
1348
1353
/// [`mem::swap`]: crate::mem::swap
1354
+ /// [`pin` module docs]: self
1349
1355
#[ lang = "new_unchecked" ]
1350
1356
#[ inline( always) ]
1351
1357
#[ rustc_const_unstable( feature = "const_pin" , issue = "76654" ) ]
0 commit comments