Skip to content

Commit b8a0f66

Browse files
author
Danilo Krummrich
committed
library: core: document layout guarantee of TypeId
Document the layout guarantee of `TypeId` not to exceed 16 bytes. Additionally, use `repr(C)` to avoid additional padding if layout randomization improved. This is useful for FFI use-cases where a `TypeId` may be stored within a foreign data structure or buffer. An example for such a use-case can be found in [1]. Link: https://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core.git/commit/?id=6f61a2637abe4f89877da3280775565baedb60e0 [1] Suggested-by: Alice Ryhl <[email protected]> Signed-off-by: Danilo Krummrich <[email protected]>
1 parent f2bae99 commit b8a0f66

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

library/core/src/any.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -613,12 +613,14 @@ impl dyn Any + Send + Sync {
613613
///
614614
/// # Layout
615615
///
616-
/// Like other [`Rust`-representation][repr-rust] types, `TypeId`'s size and layout are unstable.
617-
/// In particular, this means that you cannot rely on the size and layout of `TypeId` remaining the
618-
/// same between Rust releases; they are subject to change without prior notice between Rust
619-
/// releases.
616+
/// The size of `TypeId` is guaranteed not to exceed 16 bytes.
620617
///
621-
/// [repr-rust]: https://doc.rust-lang.org/reference/type-layout.html#r-layout.repr.rust.unspecified
618+
/// This is the only guarantee given; any other layout or implementation details
619+
/// are unstable and prone to change between Rust releases without prior notice.
620+
///
621+
/// ```
622+
/// assert!(size_of::<std::any::TypeId>() <= 16);
623+
/// ```
622624
///
623625
/// # Danger of Improper Variance
624626
///
@@ -714,6 +716,7 @@ impl dyn Any + Send + Sync {
714716
/// std::mem::forget(fake_one_ring);
715717
/// }
716718
/// ```
719+
#[repr(C)]
717720
#[derive(Copy, PartialOrd, Ord)]
718721
#[derive_const(Clone, Eq)]
719722
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)