|
196 | 196 | //! type should that reference have? Is it `Pin<&mut Field>` or `&mut Field`? |
197 | 197 | //! The same question arises with the fields of an enum, and also when considering |
198 | 198 | //! container/wrapper types such as [`Vec<T>`], [`Box<T>`], or [`RefCell<T>`]. |
199 | | -//! Also, this question arises for both mutable and shared references, we just |
200 | | -//! use the more common case of mutable references here for illustration. |
| 199 | +//! (This question applies to both mutable and shared references, we just |
| 200 | +//! use the more common case of mutable references here for illustration.) |
201 | 201 | //! |
202 | 202 | //! It turns out that it is actually up to the author of the data structure |
203 | 203 | //! to decide whether the pinned projection for a particular field turns |
|
214 | 214 | //! |
215 | 215 | //! ## Pinning *is not* structural for `field` |
216 | 216 | //! |
217 | | -//! It may seem counter-intuitive that the field of a pinned struct is not pinned, |
| 217 | +//! It may seem counter-intuitive that the field of a pinned struct might not be pinned, |
218 | 218 | //! but that is actually the easiest choice: if a `Pin<&mut Field>` is never created, |
219 | 219 | //! nothing can go wrong! So, if you decide that some field does not have structural pinning, |
220 | 220 | //! all you have to ensure is that you never create a pinned reference to that field. |
221 | 221 | //! |
222 | | -//! Then you may add a projection method that turns `Pin<&mut Struct>` into `Pin<&mut Field>`: |
| 222 | +//! Then you may add a projection method that turns `Pin<&mut Struct>` into `&mut Field`: |
223 | 223 | //! ```rust,ignore |
224 | 224 | //! impl Struct { |
225 | | -//! fn get_field<'a>(self: Pin<&'a mut Self>) -> &'a mut Field { |
| 225 | +//! fn pin_get_field<'a>(self: Pin<&'a mut Self>) -> &'a mut Field { |
226 | 226 | //! // This is okay because `field` is never considered pinned. |
227 | 227 | //! unsafe { &mut self.get_unchecked_mut().field } |
228 | 228 | //! } |
229 | 229 | //! } |
230 | 230 | //! ``` |
231 | 231 | //! |
232 | | -//! You may also make make `Struct: Unpin` *even if* the type of `field` |
| 232 | +//! You may also `impl Unpin for Struct` *even if* the type of `field` |
233 | 233 | //! is not `Unpin`. What that type thinks about pinning is just not relevant |
234 | 234 | //! when no `Pin<&mut Field>` is ever created. |
235 | 235 | //! |
|
242 | 242 | //! witnessing that the field is pinned: |
243 | 243 | //! ```rust,ignore |
244 | 244 | //! impl Struct { |
245 | | -//! fn get_field<'a>(self: Pin<&'a mut Self>) -> Pin<&'a mut Field> { |
| 245 | +//! fn pin_get_field<'a>(self: Pin<&'a mut Self>) -> Pin<&'a mut Field> { |
246 | 246 | //! // This is okay because `field` is pinned when `self` is. |
247 | 247 | //! unsafe { self.map_unchecked_mut(|s| &mut s.field) } |
248 | 248 | //! } |
|
252 | 252 | //! However, structural pinning comes with a few extra requirements: |
253 | 253 | //! |
254 | 254 | //! 1. The struct must only be [`Unpin`] if all the structural fields are |
255 | | -//! `Unpin`. This is the default, but `Unpin` is a safe trait, so it is your |
256 | | -//! responsibility as the author of the struct *not* to add something like |
| 255 | +//! `Unpin`. This is the default, but `Unpin` is a safe trait, so as the author of |
| 256 | +//! the struct it is your responsibility *not* to add something like |
257 | 257 | //! `impl<T> Unpin for Struct<T>`. (Notice that adding a projection operation |
258 | 258 | //! requires unsafe code, so the fact that `Unpin` is a safe trait does not break |
259 | 259 | //! the principle that you only have to worry about any of this if you use `unsafe`.) |
|
0 commit comments