@@ -1227,21 +1227,24 @@ impl<Ptr: Deref<Target: Unpin>> Pin<Ptr> {
1227
1227
1228
1228
impl < Ptr : Deref > Pin < Ptr > {
1229
1229
/// Construct a new `Pin<Ptr>` around a reference to some data of a type that
1230
- /// may or may not implement `Unpin`.
1230
+ /// may or may not implement [ `Unpin`] .
1231
1231
///
1232
- /// If `pointer` dereferences to an `Unpin` type, `Pin::new` should be used
1232
+ /// If `pointer` dereferences to an [ `Unpin`] type, [ `Pin::new`] should be used
1233
1233
/// instead.
1234
1234
///
1235
1235
/// # Safety
1236
1236
///
1237
1237
/// This constructor is unsafe because we cannot guarantee that the data
1238
- /// pointed to by `pointer` is pinned, meaning that the data will not be moved or
1239
- /// its storage invalidated until it gets dropped. If the constructed `Pin<Ptr>` does
1240
- /// not guarantee that the data `Ptr` points to is pinned, that is a violation of
1241
- /// the API contract and may lead to undefined behavior in later (safe) operations.
1238
+ /// pointed to by `pointer` is pinned. At its core, pinning a value means making the
1239
+ /// guarantee that the value's data will not be moved nor have its storage invalidated until
1240
+ /// it gets dropped. For a more thorough explanation of pinning, see the [`pin` module docs].
1242
1241
///
1243
- /// By using this method, you are making a promise about the `Ptr::Deref` and
1244
- /// `Ptr::DerefMut` implementations, if they exist. Most importantly, they
1242
+ /// If the caller that is constructing this `Pin<Ptr>` does not ensure that the data `Ptr`
1243
+ /// points to is pinned, that is a violation of the API contract and may lead to undefined
1244
+ /// behavior in later (even safe) operations.
1245
+ ///
1246
+ /// By using this method, you are also making a promise about the [`Deref`] and
1247
+ /// [`DerefMut`] implementations of `Ptr`, if they exist. Most importantly, they
1245
1248
/// must not move out of their `self` arguments: `Pin::as_mut` and `Pin::as_ref`
1246
1249
/// will call `DerefMut::deref_mut` and `Deref::deref` *on the pointer type `Ptr`*
1247
1250
/// and expect these methods to uphold the pinning invariants.
@@ -1252,7 +1255,9 @@ impl<Ptr: Deref> Pin<Ptr> {
1252
1255
///
1253
1256
/// For example, calling `Pin::new_unchecked` on an `&'a mut T` is unsafe because
1254
1257
/// while you are able to pin it for the given lifetime `'a`, you have no control
1255
- /// over whether it is kept pinned once `'a` ends:
1258
+ /// over whether it is kept pinned once `'a` ends, and therefore cannot uphold the
1259
+ /// guarantee that a value, once pinned, remains pinned until it is dropped:
1260
+ ///
1256
1261
/// ```
1257
1262
/// use std::mem;
1258
1263
/// use std::pin::Pin;
@@ -1286,7 +1291,7 @@ impl<Ptr: Deref> Pin<Ptr> {
1286
1291
/// // ...
1287
1292
/// }
1288
1293
/// drop(pin);
1289
-
1294
+ ///
1290
1295
/// let content = Rc::get_mut(&mut x).unwrap(); // Potential UB down the road ⚠️
1291
1296
/// // Now, if `x` was the only reference, we have a mutable reference to
1292
1297
/// // data that we pinned above, which we could use to move it as we have
@@ -1347,6 +1352,7 @@ impl<Ptr: Deref> Pin<Ptr> {
1347
1352
/// ```
1348
1353
///
1349
1354
/// [`mem::swap`]: crate::mem::swap
1355
+ /// [`pin` module docs]: self
1350
1356
#[ lang = "new_unchecked" ]
1351
1357
#[ inline( always) ]
1352
1358
#[ rustc_const_unstable( feature = "const_pin" , issue = "76654" ) ]
0 commit comments