|
166 | 166 | //! implementation as well: if an element of your type could have been pinned,
|
167 | 167 | //! you must treat Drop as implicitly taking `Pin<&mut Self>`.
|
168 | 168 | //!
|
169 |
| -//! In particular, if your type is `#[repr(packed)]`, the compiler will automatically |
| 169 | +//! For example, you could implement `Drop` as follows: |
| 170 | +//! ```rust,ignore |
| 171 | +//! impl Drop for Type { |
| 172 | +//! fn drop(&mut self) { |
| 173 | +//! // `new_unchecked` is okay because we know this value is never used |
| 174 | +//! // again after being dropped. |
| 175 | +//! inner_drop(unsafe { Pin::new_unchecked(self)}); |
| 176 | +//! fn inner_drop(this: Pin<&mut Type>) { |
| 177 | +//! // Actual drop code goes here. |
| 178 | +//! } |
| 179 | +//! } |
| 180 | +//! } |
| 181 | +//! ``` |
| 182 | +//! `inner_drop` has the type that `drop` *should* have, so this makes sure that |
| 183 | +//! you do not accidentally use `self`/`this` in a way that is in conflict with pinning. |
| 184 | +//! |
| 185 | +//! Moreover, if your type is `#[repr(packed)]`, the compiler will automatically |
170 | 186 | //! move fields around to be able to drop them. As a consequence, you cannot use
|
171 | 187 | //! pinning with a `#[repr(packed)]` type.
|
172 | 188 | //!
|
173 | 189 | //! # Projections and Structural Pinning
|
174 | 190 | //!
|
175 |
| -//! One interesting question arises when considering the interaction of pinning |
176 |
| -//! and the fields of a struct. When can a struct have a "pinning projection", |
177 |
| -//! i.e., an operation with type `fn(Pin<&Struct>) -> Pin<&Field>`? In a |
178 |
| -//! similar vein, when can a generic wrapper type (such as `Vec<T>`, `Box<T>`, |
179 |
| -//! or `RefCell<T>`) have an operation with type `fn(Pin<&Wrapper<T>>) -> |
180 |
| -//! Pin<&T>`? |
| 191 | +//! When working with pinned structs, the question arises how one can access the |
| 192 | +//! fields of that struct in a method that takes just `Pin<&mut Struct>`. |
| 193 | +//! The usual approach is to write helper methods (so called *projections*) |
| 194 | +//! that turn `Pin<&mut Struct>` into a reference to the field, but what |
| 195 | +//! type should that reference have? Is it `Pin<&mut Field>` or `&mut Field`? |
| 196 | +//! The same question arises with the fields of an enum, and also when considering |
| 197 | +//! container/wrapper types such as `Vec<T>`, `Box<T>`, or `RefCell<T>`. |
| 198 | +//! Also, this question arises for both mutable and shared references, we just |
| 199 | +//! use the more common case of mutable references here for illustration. |
| 200 | +//! |
| 201 | +//! It turns out that it is actually up to the author of the data structure |
| 202 | +//! to decide whether the pinned projection for a particular field turns |
| 203 | +//! `Pin<&mut Struct>` into `Pin<&mut Field>` or `&mut Field`. There are some |
| 204 | +//! constraints though, and the most important constraint is *consistency*: |
| 205 | +//! every field can be *either* projected to a pinned reference, *or* have |
| 206 | +//! pinning removed as part of the projection. If both are done for the same field, |
| 207 | +//! that will likely be unsound! |
| 208 | +//! |
| 209 | +//! Basically, as the author of a data structure you get to decide for each field whether pinning |
| 210 | +//! "propagates" to this field or not. Pinning that propagates is also called "structural", |
| 211 | +//! because it follows the structure of the type. |
| 212 | +//! In the following, we describe the considerations that have to be made for either choice. |
| 213 | +//! |
| 214 | +//! ## Pinning *is not* structural for `field` |
| 215 | +//! |
| 216 | +//! It may seem counter-intuitive that the field of a pinned struct is not pinned, |
| 217 | +//! but that is actually the easiest choice: if a `Pin<&mut Field>` is never created, |
| 218 | +//! nothing can go wrong! So, if you decide that some field does not have structural pinning, |
| 219 | +//! all you have to ensure is that you never create a pinned reference to that field. |
| 220 | +//! |
| 221 | +//! Then you may add a projection method that turns `Pin<&mut Struct>` into `Pin<&mut Field>`: |
| 222 | +//! ```rust,ignore |
| 223 | +//! impl Struct { |
| 224 | +//! fn get_field<'a>(self: Pin<&'a mut Self>) -> &'a mut Field { |
| 225 | +//! // This is okay because `field` is never considered pinned. |
| 226 | +//! unsafe { &mut self.get_unchecked_mut().field } |
| 227 | +//! } |
| 228 | +//! } |
| 229 | +//! ``` |
181 | 230 | //!
|
182 |
| -//! Note: For the entirety of this discussion, the same applies for mutable references as it |
183 |
| -//! does for shared references. |
| 231 | +//! You may also make make `Struct: Unpin` *even if* the type of `field` |
| 232 | +//! is not `Unpin`. What that type thinks about pinning is just not relevant |
| 233 | +//! when no `Pin<&mut Field>` is ever created. |
184 | 234 | //!
|
185 |
| -//! Having a pinning projection for some field means that pinning is "structural": |
186 |
| -//! when the wrapper is pinned, the field must be considered pinned, too. |
187 |
| -//! After all, the pinning projection lets us get a `Pin<&Field>`. |
| 235 | +//! ## Pinning *is* structural for `field` |
188 | 236 | //!
|
189 |
| -//! However, structural pinning comes with a few extra requirements, so not all |
190 |
| -//! wrappers can be structural and hence not all wrappers can offer pinning projections: |
| 237 | +//! The other option is to decide that pinning is "structural" for `field`, |
| 238 | +//! meaning that if the struct is pinned then so is the field. |
191 | 239 | //!
|
192 |
| -//! 1. The wrapper must only be [`Unpin`] if all the structural fields are |
193 |
| -//! `Unpin`. This is the default, but `Unpin` is a safe trait, so as the author of |
194 |
| -//! the wrapper it is your responsibility *not* to add something like |
195 |
| -//! `impl<T> Unpin for Wrapper<T>`. (Notice that adding a projection operation |
| 240 | +//! This allows writing a projection that creates a `Pin<&mut Field>`, thus |
| 241 | +//! witnessing that the field is pinned: |
| 242 | +//! ```rust,ignore |
| 243 | +//! impl Struct { |
| 244 | +//! fn get_field<'a>(self: Pin<&'a mut Self>) -> Pin<&'a mut Field> { |
| 245 | +//! // This is okay because `field` is pinned when `self` is. |
| 246 | +//! unsafe { self.map_unchecked_mut(|s| &mut s.field) } |
| 247 | +//! } |
| 248 | +//! } |
| 249 | +//! ``` |
| 250 | +//! |
| 251 | +//! However, structural pinning comes with a few extra requirements: |
| 252 | +//! |
| 253 | +//! 1. The struct must only be [`Unpin`] if all the structural fields are |
| 254 | +//! `Unpin`. This is the default, but `Unpin` is a safe trait, so it is your |
| 255 | +//! responsibility as the author of the struct *not* to add something like |
| 256 | +//! `impl<T> Unpin for Struct<T>`. (Notice that adding a projection operation |
196 | 257 | //! requires unsafe code, so the fact that `Unpin` is a safe trait does not break
|
197 | 258 | //! the principle that you only have to worry about any of this if you use `unsafe`.)
|
198 |
| -//! 2. The destructor of the wrapper must not move structural fields out of its argument. This |
| 259 | +//! 2. The destructor of the struct must not move structural fields out of its argument. This |
199 | 260 | //! is the exact point that was raised in the [previous section][drop-impl]: `drop` takes
|
200 |
| -//! `&mut self`, but the wrapper (and hence its fields) might have been pinned before. |
| 261 | +//! `&mut self`, but the struct (and hence its fields) might have been pinned before. |
201 | 262 | //! You have to guarantee that you do not move a field inside your `Drop` implementation.
|
202 |
| -//! In particular, as explained previously, this means that your wrapper type must *not* |
| 263 | +//! In particular, as explained previously, this means that your struct must *not* |
203 | 264 | //! be `#[repr(packed)]`.
|
| 265 | +//! See that section for how to write `drop` in a way that the compiler can help you |
| 266 | +//! not accidentally break pinning. |
204 | 267 | //! 3. You must make sure that you uphold the [`Drop` guarantee][drop-guarantee]:
|
205 |
| -//! once your wrapper is pinned, the memory that contains the |
| 268 | +//! once your struct is pinned, the memory that contains the |
206 | 269 | //! content is not overwritten or deallocated without calling the content's destructors.
|
207 | 270 | //! This can be tricky, as witnessed by `VecDeque<T>`: the destructor of `VecDeque<T>` can fail
|
208 | 271 | //! to call `drop` on all elements if one of the destructors panics. This violates the
|
209 | 272 | //! `Drop` guarantee, because it can lead to elements being deallocated without
|
210 | 273 | //! their destructor being called. (`VecDeque` has no pinning projections, so this
|
211 | 274 | //! does not cause unsoundness.)
|
212 | 275 | //! 4. You must not offer any other operations that could lead to data being moved out of
|
213 |
| -//! the fields when your type is pinned. For example, if the wrapper contains an |
| 276 | +//! the structural fields when your type is pinned. For example, if the struct contains an |
214 | 277 | //! `Option<T>` and there is a `take`-like operation with type
|
215 |
| -//! `fn(Pin<&mut Wrapper<T>>) -> Option<T>`, |
216 |
| -//! that operation can be used to move a `T` out of a pinned `Wrapper<T>` -- which means |
217 |
| -//! pinning cannot be structural. |
| 278 | +//! `fn(Pin<&mut Struct<T>>) -> Option<T>`, |
| 279 | +//! that operation can be used to move a `T` out of a pinned `Struct<T>` -- which means |
| 280 | +//! pinning cannot be structural for the field holding this data. |
218 | 281 | //!
|
219 | 282 | //! For a more complex example of moving data out of a pinned type, imagine if `RefCell<T>`
|
220 | 283 | //! had a method `fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut T>`.
|
|
231 | 294 | //! (using `RefCell::get_pin_mut`) and then move that content using the mutable
|
232 | 295 | //! reference we got later.
|
233 | 296 | //!
|
234 |
| -//! For a type like `Vec<T>`, both possibilites (structural pinning or not) make sense, |
235 |
| -//! and the choice is up to the author. A `Vec<T>` with structural pinning could |
236 |
| -//! have `get_pin`/`get_pin_mut` projections. However, it could *not* allow calling |
| 297 | +//! ## Examples |
| 298 | +//! |
| 299 | +//! For a type like `Vec<T>`, both possibilites (structural pinning or not) make sense. |
| 300 | +//! A `Vec<T>` with structural pinning could have `get_pin`/`get_pin_mut` methods to get |
| 301 | +//! pinned references to elements. However, it could *not* allow calling |
237 | 302 | //! `pop` on a pinned `Vec<T>` because that would move the (structurally pinned) contents!
|
238 | 303 | //! Nor could it allow `push`, which might reallocate and thus also move the contents.
|
239 | 304 | //! A `Vec<T>` without structural pinning could `impl<T> Unpin for Vec<T>`, because the contents
|
240 | 305 | //! are never pinned and the `Vec<T>` itself is fine with being moved as well.
|
| 306 | +//! At that point pinning just has no effect on the vector at all. |
241 | 307 | //!
|
242 | 308 | //! In the standard library, pointer types generally do not have structural pinning,
|
243 | 309 | //! and thus they do not offer pinning projections. This is why `Box<T>: Unpin` holds for all `T`.
|
|
249 | 315 | //! whether the content is pinned is entirely independent of whether the pointer is
|
250 | 316 | //! pinned, meaning pinning is *not* structural.
|
251 | 317 | //!
|
| 318 | +//! When implementing a `Future` combinator, you will usually need structural pinning |
| 319 | +//! for the nested futures, as you need to get pinned references to them to call `poll`. |
| 320 | +//! But if your combinator contains any other data that does not need to be pinned, |
| 321 | +//! you can make those fields not structural and hence freely access them with a |
| 322 | +//! mutable reference even when you just have `Pin<&mut Self>` (such as in your own |
| 323 | +//! `poll` implementation). |
| 324 | +//! |
252 | 325 | //! [`Pin<P>`]: struct.Pin.html
|
253 | 326 | //! [`Unpin`]: ../../std/marker/trait.Unpin.html
|
254 | 327 | //! [`Deref`]: ../../std/ops/trait.Deref.html
|
|
0 commit comments