|
11 | 11 | //! Types which pin data to its location in memory
|
12 | 12 | //!
|
13 | 13 | //! 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. |
15 | 15 | //!
|
16 | 16 | //! A prime example of such a scenario would be building self-referencial structs,
|
17 | 17 | //! since moving an object with pointers to itself will invalidate them,
|
18 | 18 | //! which could cause undefined behavior.
|
19 | 19 | //!
|
20 | 20 | //! In order to prevent objects from moving, they must be *pinned*,
|
21 | 21 | //! 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, |
23 | 24 | //! 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. |
26 | 26 | //!
|
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. |
30 | 29 | //!
|
31 | 30 | //! [`PinMut`]: struct.PinMut.html
|
32 | 31 | //! [`PinBox`]: struct.PinBox.html
|
33 | 32 | //! [`Unpin`]: ../../core/marker/trait.Unpin.html
|
34 |
| -//! [`DerefMut`]: ../../core/ops/trait.DerefMut.html |
35 |
| -//! [`Deref`]: ../../core/ops/trait.Deref.html |
36 | 33 | //! [`swap`]: ../../core/mem/fn.swap.html
|
37 | 34 | //!
|
38 | 35 | //! # Examples
|
|
83 | 80 | //! let mut still_unmoved = unmoved;
|
84 | 81 | //! assert_eq!(still_unmoved.slice, NonNull::from(&still_unmoved.data));
|
85 | 82 | //!
|
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); |
90 | 86 | //! ```
|
91 | 87 |
|
92 | 88 | #![unstable(feature = "pin", issue = "49150")]
|
|
0 commit comments