| 1 | ## Define your own PhantomData |
| 2 | |
| 3 | [<img alt="github" src="https://img.shields.io/badge/github-dtolnay/ghost-8da0cb?style=for-the-badge&labelColor=555555&logo=github" height="20">](https://github.com/dtolnay/ghost) |
| 4 | [<img alt="crates.io" src="https://img.shields.io/crates/v/ghost.svg?style=for-the-badge&color=fc8d62&logo=rust" height="20">](https://crates.io/crates/ghost) |
| 5 | [<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-ghost-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs" height="20">](https://docs.rs/ghost) |
| 6 | [<img alt="build status" src="https://img.shields.io/github/workflow/status/dtolnay/ghost/CI/master?style=for-the-badge" height="20">](https://github.com/dtolnay/ghost/actions?query=branch%3Amaster) |
| 7 | |
| 8 | This crate makes it possible to define your own PhantomData and similarly |
| 9 | behaved unit types with generic parameters, which is not permitted in ordinary |
| 10 | Rust. |
| 11 | |
| 12 | ```toml |
| 13 | [dependencies] |
| 14 | ghost = "0.1" |
| 15 | ``` |
| 16 | |
| 17 | *Supports rustc 1.31+* |
| 18 | |
| 19 | ### Background |
| 20 | |
| 21 | [`PhantomData`] as defined by the Rust standard library is magical in that the |
| 22 | same type is impossible to define in ordinary Rust code. It is defined in the |
| 23 | standard library like this: |
| 24 | |
| 25 | [`PhantomData`]: https://doc.rust-lang.org/std/marker/struct.PhantomData.html |
| 26 | |
| 27 | ```rust |
| 28 | #[lang = "phantom_data"] |
| 29 | pub struct PhantomData<T: ?Sized>; |
| 30 | ``` |
| 31 | |
| 32 | The `#[lang = "..."]` attribute indicates that this is a [lang item], a special |
| 33 | case known to the compiler. It is the only type permitted to carry an unused |
| 34 | type parameter. |
| 35 | |
| 36 | [lang item]: https://manishearth.github.io/blog/2017/01/11/rust-tidbits-what-is-a-lang-item/ |
| 37 | |
| 38 | If we try to define an equivalent unit struct with type parameter, the compiler |
| 39 | rejects that. |
| 40 | |
| 41 | ```rust |
| 42 | struct MyPhantom<T: ?Sized>; |
| 43 | ``` |
| 44 | |
| 45 | ```console |
| 46 | error[E0392]: parameter `T` is never used |
| 47 | --> src/main.rs:1:18 |
| 48 | | |
| 49 | 1 | struct MyPhantom<T: ?Sized>; |
| 50 | | ^ unused type parameter |
| 51 | | |
| 52 | = help: consider removing `T` or using a marker such as `std::marker::PhantomData` |
| 53 | ``` |
| 54 | |
| 55 | This crate provides a `#[phantom]` attribute that makes it possible to define |
| 56 | unit structs with generic parameters. |
| 57 | |
| 58 | ### Examples |
| 59 | |
| 60 | ```rust |
| 61 | use ghost::phantom; |
| 62 | |
| 63 | #[phantom] |
| 64 | struct MyPhantom<T: ?Sized>; |
| 65 | |
| 66 | fn main() { |
| 67 | // Proof that MyPhantom behaves like PhantomData. |
| 68 | let _: MyPhantom<u8> = MyPhantom::<u8>; |
| 69 | assert_eq!(0, std::mem::size_of::<MyPhantom<u8>>()); |
| 70 | } |
| 71 | |
| 72 | // Proof that MyPhantom is not just a re-export of PhantomData. |
| 73 | // If it were a re-export, these would be conflicting impls. |
| 74 | trait Trait {} |
| 75 | impl<T> Trait for std::marker::PhantomData<T> {} |
| 76 | impl<T> Trait for MyPhantom<T> {} |
| 77 | |
| 78 | // Proof that MyPhantom is local to the current crate. |
| 79 | impl<T> MyPhantom<T> { |
| 80 | } |
| 81 | ``` |
| 82 | |
| 83 | The implementation accepts where-clauses, lifetimes, multiple generic |
| 84 | parameters, and derives. Here is a contrived invocation that demonstrates |
| 85 | everything at once: |
| 86 | |
| 87 | ```rust |
| 88 | use ghost::phantom; |
| 89 | |
| 90 | #[phantom] |
| 91 | #[derive(Copy, Clone, Default, Hash, PartialOrd, Ord, PartialEq, Eq, Debug)] |
| 92 | struct Crazy<'a, V: 'a, T> where &'a V: IntoIterator<Item = T>; |
| 93 | |
| 94 | fn main() { |
| 95 | let _ = Crazy::<'static, Vec<String>, &'static String>; |
| 96 | |
| 97 | // Lifetime elision. |
| 98 | let crazy = Crazy::<Vec<String>, &String>; |
| 99 | println!("{:?}", crazy); |
| 100 | } |
| 101 | ``` |
| 102 | |
| 103 | ### Variance |
| 104 | |
| 105 | The `#[phantom]` attribute accepts attributes on individual generic parameters |
| 106 | (both lifetime and type parameters) to make them contravariant or invariant. The |
| 107 | default is covariance. |
| 108 | |
| 109 | - `#[contra]` — contravariant generic parameter |
| 110 | - `#[invariant]` — invariant generic parameter |
| 111 | |
| 112 | The implications of variance are explained in more detail by the [Subtyping |
| 113 | chapter] of the Rustonomicon. |
| 114 | |
| 115 | [Subtyping chapter]: https://doc.rust-lang.org/nomicon/subtyping.html |
| 116 | |
| 117 | ```rust |
| 118 | use ghost::phantom; |
| 119 | |
| 120 | #[phantom] |
| 121 | struct ContravariantLifetime<#[contra] 'a>; |
| 122 | |
| 123 | fn f<'a>(arg: ContravariantLifetime<'a>) -> ContravariantLifetime<'static> { |
| 124 | // This coercion is only legal because the lifetime parameter is |
| 125 | // contravariant. If it were covariant (the default) or invariant, |
| 126 | // this would not compile. |
| 127 | arg |
| 128 | } |
| 129 | |
| 130 | #[phantom] |
| 131 | struct Demo<A, #[contra] B, #[invariant] C>; |
| 132 | ``` |
| 133 | |
| 134 | ### Use cases |
| 135 | |
| 136 | Entirely up to your imagination. Just to name one, how about a typed registry |
| 137 | library that admits the following syntax for iterating over values registered of |
| 138 | a particular type: |
| 139 | |
| 140 | ```rust |
| 141 | for flag in Registry::<Flag> { |
| 142 | /* ... */ |
| 143 | } |
| 144 | ``` |
| 145 | |
| 146 | <br> |
| 147 | |
| 148 | #### License |
| 149 | |
| 150 | <sup> |
| 151 | Licensed under either of <a href="LICENSE-APACHE">Apache License, Version |
| 152 | 2.0</a> or <a href="LICENSE-MIT">MIT license</a> at your option. |
| 153 | </sup> |
| 154 | |
| 155 | <br> |
| 156 | |
| 157 | <sub> |
| 158 | Unless you explicitly state otherwise, any contribution intentionally submitted |
| 159 | for inclusion in this crate by you, as defined in the Apache-2.0 license, shall |
| 160 | be dual licensed as above, without any additional terms or conditions. |
| 161 | </sub> |