From 360a768c6d4fc5910e9c849d793c82f9f3bf3936 Mon Sep 17 00:00:00 2001 From: Daniel Henry-Mantilla Date: Sun, 10 Dec 2023 04:18:32 +0100 Subject: [PATCH] Improve the `PhantomData` table (#417) --- src/phantom-data.md | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/phantom-data.md b/src/phantom-data.md index 449d9e7..cd2428d 100644 --- a/src/phantom-data.md +++ b/src/phantom-data.md @@ -106,7 +106,14 @@ that that `Vec` _owns_ values of type `T` (more precisely: may use values of in its `Drop` implementation), and Rust will thus not allow them to _dangle_ should a `Vec` be dropped. -**Adding an extra `_owns_T: PhantomData` field is thus _superfluous_ and accomplishes nothing**. +When a type already has a `Drop impl`, **adding an extra `_owns_T: PhantomData` field +is thus _superfluous_ and accomplishes nothing**, dropck-wise (it still affects variance +and auto-traits). + + - (advanced edge case: if the type containing the `PhantomData` has no `Drop` impl at all, + but still has drop glue (by having _another_ field with drop glue), then the + dropck/`#[may_dangle]` considerations mentioned herein do apply as well: a `PhantomData` + field will then require `T` to be droppable whenever the containing type goes out of scope). ___ @@ -234,14 +241,18 @@ standard library made a utility for itself called `Unique` which: Here’s a table of all the wonderful ways `PhantomData` could be used: -| Phantom type | `'a` | `T` | `Send` | `Sync` | -|-----------------------------|-----------|-----------------------------|-----------|-----------| -| `PhantomData` | - | covariant (with drop check) | `T: Send` | `T: Sync` | -| `PhantomData<&'a T>` | covariant | covariant | `T: Sync` | `T: Sync` | -| `PhantomData<&'a mut T>` | covariant | invariant | `T: Send` | `T: Sync` | -| `PhantomData<*const T>` | - | covariant | - | - | -| `PhantomData<*mut T>` | - | invariant | - | - | -| `PhantomData` | - | contravariant | `Send` | `Sync` | -| `PhantomData T>` | - | covariant | `Send` | `Sync` | -| `PhantomData T>` | - | invariant | `Send` | `Sync` | -| `PhantomData>` | invariant | - | `Send` | - | +| Phantom type | variance of `'a` | variance of `T` | `Send`/`Sync`
(or lack thereof) | dangling `'a` or `T` in drop glue
(_e.g._, `#[may_dangle] Drop`) | +|-----------------------------|:----------------:|:-----------------:|:-----------------------------------------:|:------------------------------------------------:| +| `PhantomData` | - | **cov**ariant | inherited | disallowed ("owns `T`") | +| `PhantomData<&'a T>` | **cov**ariant | **cov**ariant | `Send + Sync`
requires
`T : Sync` | allowed | +| `PhantomData<&'a mut T>` | **cov**ariant | **inv**ariant | inherited | allowed | +| `PhantomData<*const T>` | - | **cov**ariant | `!Send + !Sync` | allowed | +| `PhantomData<*mut T>` | - | **inv**ariant | `!Send + !Sync` | allowed | +| `PhantomData` | - | **contra**variant | `Send + Sync` | allowed | +| `PhantomData T>` | - | **cov**ariant | `Send + Sync` | allowed | +| `PhantomData T>` | - | **inv**ariant | `Send + Sync` | allowed | +| `PhantomData>` | **inv**ariant | - | `Send + !Sync` | allowed | + + - Note: opting out of the `Unpin` auto-trait requires the dedicated [`PhantomPinned`] type instead. + +[`PhantomPinned`]: ../core/marker/struct.PhantomPinned.html