Skip to content

Commit 4fd1f93

Browse files
authored
add note about PhantomData and dropck
1 parent 60dd86d commit 4fd1f93

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/libs/maintaining-std.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,23 @@ Changes to collection internals may affect the order their items are dropped in.
189189

190190
Generic types that manually implement `Drop` should consider whether a `#[may_dangle]` attribute is appropriate. The [Nomicon][dropck] has some details on what `#[may_dangle]` is all about.
191191

192+
If the type pretends to store a value that may dangle, but doesn't do so directly (perhaps through `MaybeUninit<T>` or a raw pointer) then make sure there's an appropriate use of `PhantomData` to support dropck. As a [real-world example][rust/issues/76367], adding a `#[may_dangle]` attribute to an `OptionCell<T>` that internally stores its value as a `MaybeUninit<T>` requires both a `PhantomData` marker and a `#[may_dangle]` attribute:
193+
194+
```diff
195+
struct OptionCell<T> {
196+
is_init: bool,
197+
value: MaybeUninit<T>,
198+
+ _marker: PhantomData<T>,
199+
}
200+
201+
- impl Drop<T> OptionCell<T> {
202+
+ impl Drop<#[may_dangle] T> OptionCell<T> {
203+
..
204+
}
205+
```
206+
207+
Types in the standard library that use the internal `Unique<T>` will don't need a `PhantomData` marker field. That's taken care of for them by `Unique<T>`.
208+
192209
### How could `mem` break assumptions?
193210

194211
#### `mem::replace` and `mem::swap`
@@ -262,5 +279,6 @@ Where `unsafe` and `const` is involved, e.g., for operations which are "unconst"
262279
[RFC 1105]: https://rust-lang.github.io/rfcs/1105-api-evolution.html
263280
[Everyone Poops]: http://cglab.ca/~abeinges/blah/everyone-poops
264281
[rust/pull/46799]: https://github.com/rust-lang/rust/pull/46799
282+
[rust/issues/76367]: https://github.com/rust-lang/rust/issues/76367
265283
[hashbrown/pull/119]: https://github.com/rust-lang/hashbrown/pull/119
266284
[rollup guidelines]: ../compiler/reviews.md#rollups

0 commit comments

Comments
 (0)