Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the ManuallyDrop ABI guaranteed to be repr(transparent)?

Tags:

rust

The nomicon says:

repr(transparent)

[...] This repr is only considered part of the public ABI of a type if either the single field is pub, or if its layout is documented in prose. Otherwise, the layout should not be relied upon by other crates.

ManuallyDrop<T> is repr(transparent), but its single field is not pub. Its docs say:

[...] This wrapper is 0-cost.

ManuallyDrop<T> is subject to the same layout optimizations as T. [...]

Does this count as documenting its layout in the prose? Is it safe to assume that I can transmute or otherwise convert (e.g. pointer cast) from T to ManuallyDrop<T>?

like image 704
zstewart Avatar asked Oct 16 '25 00:10

zstewart


1 Answers

This was actually just clarified with today's release of Rust 1.61.0. The documentation of ManuallyDrop now specifies (added text highlighted in bold):

ManuallyDrop<T> is guaranteed to have the same layout as T, and is subject to the same layout optimizations as T.

like image 149
Kevin Reid Avatar answered Oct 18 '25 15:10

Kevin Reid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!