Skip to content

Commit 8e9aad2

Browse files
committed
deemphasize immutability and improve swap explanation in pin module
1 parent 2f501a1 commit 8e9aad2

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

src/liballoc/pin.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,25 @@
1111
//! Types which pin data to its location in memory
1212
//!
1313
//! It is sometimes useful to have objects that are guaranteed to not move,
14-
//! in the sense that their placement in memory in consistent, and can thus be relied upon.
14+
//! in the sense that their placement in memory does not change, and can thus be relied upon.
1515
//!
1616
//! A prime example of such a scenario would be building self-referencial structs,
1717
//! since moving an object with pointers to itself will invalidate them,
1818
//! which could cause undefined behavior.
1919
//!
2020
//! In order to prevent objects from moving, they must be *pinned*,
2121
//! by wrapping the data in special pointer types, such as [`PinMut`] and [`PinBox`].
22-
//! These restrict access to the underlying data to only be immutable by implementing [`Deref`],
22+
//! On top of ensuring the data cannot be taked by value by being pointers,
23+
//! these types restrict access to the underlying data such that it cannot be moved out of them,
2324
//! unless the type implements the [`Unpin`] trait,
24-
//! which indicates that it doesn't need these restrictions and can be safely mutated,
25-
//! by implementing [`DerefMut`].
25+
//! which indicates that it can be used safely without these restrictions.
2626
//!
27-
//! This is done because, while modifying an object can be done in-place,
28-
//! it might also relocate a buffer when its at full capacity,
29-
//! or it might replace one object with another without logically "moving" them with [`swap`].
27+
//! A type may be moved out of a reference to it using a function like [`swap`],
28+
//! which replaces the contents of the references, and thus changes their place in memory.
3029
//!
3130
//! [`PinMut`]: struct.PinMut.html
3231
//! [`PinBox`]: struct.PinBox.html
3332
//! [`Unpin`]: ../../core/marker/trait.Unpin.html
34-
//! [`DerefMut`]: ../../core/ops/trait.DerefMut.html
35-
//! [`Deref`]: ../../core/ops/trait.Deref.html
3633
//! [`swap`]: ../../core/mem/fn.swap.html
3734
//!
3835
//! # Examples
@@ -83,10 +80,9 @@
8380
//! let mut still_unmoved = unmoved;
8481
//! assert_eq!(still_unmoved.slice, NonNull::from(&still_unmoved.data));
8582
//!
86-
//! // Now the only way to access to data (safely) is immutably,
87-
//! // so this will fail to compile:
88-
//! // still_unmoved.data.push_str(" world");
89-
//!
83+
//! // Since our type doesn't implement Unpin, this will fail to compile:
84+
//! // let new_unmoved = Unmovable::new("world".to_string());
85+
//! // std::mem::swap(&mut *still_unmoved, &mut *new_unmoved);
9086
//! ```
9187
9288
#![unstable(feature = "pin", issue = "49150")]

0 commit comments

Comments
 (0)