diff options
| author | bors <bors@rust-lang.org> | 2025-10-04 13:37:12 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2025-10-04 13:37:12 +0000 |
| commit | 1bd98acf0e54f1ea678c4fabb8e1b10851eb8465 (patch) | |
| tree | e1e0adfca6dd8050387b45ffb09c0985cf2a32a8 /library/core/src/mem/mod.rs | |
| parent | 99b9a8850349e56247acb6ce19910c7f96db8439 (diff) | |
| parent | 1ebbb3c2fd78de38d0730cf9d874295f0d5dc2f1 (diff) | |
| download | rust-auto.tar.gz rust-auto.zip | |
Auto merge of #147330 - matthiaskrgr:rollup-h4jyzmv, r=matthiaskrgr auto
Rollup of 11 pull requests Successful merges: - rust-lang/rust#142670 (Document fully-qualified syntax in `as`' keyword doc) - rust-lang/rust#145685 (add CloneFromCell and Cell::get_cloned) - rust-lang/rust#146330 (Bump unicode_data and printables to version 17.0.0) - rust-lang/rust#146451 (Fix atan2 inaccuracy in documentation) - rust-lang/rust#146479 (add mem::conjure_zst) - rust-lang/rust#147117 (interpret `#[used]` as `#[used(compiler)]` on illumos) - rust-lang/rust#147190 (std: `sys::net` cleanups) - rust-lang/rust#147251 (Do not assert that a change in global cache only happens when concurrent) - rust-lang/rust#147280 (Return to needs-llvm-components being info-only) - rust-lang/rust#147288 (compiletest: Make `DirectiveLine` responsible for name/value splitting) - rust-lang/rust#147315 (bless autodiff batching test) r? `@ghost` `@rustbot` modify labels: rollup
Diffstat (limited to 'library/core/src/mem/mod.rs')
| -rw-r--r-- | library/core/src/mem/mod.rs | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/library/core/src/mem/mod.rs b/library/core/src/mem/mod.rs index db4c8e9e551..c484551187c 100644 --- a/library/core/src/mem/mod.rs +++ b/library/core/src/mem/mod.rs @@ -7,6 +7,7 @@ use crate::alloc::Layout; use crate::marker::DiscriminantKind; +use crate::panic::const_assert; use crate::{clone, cmp, fmt, hash, intrinsics, ptr}; mod manually_drop; @@ -1407,3 +1408,60 @@ pub macro offset_of($Container:ty, $($fields:expr)+ $(,)?) { // The `{}` is for better error messages {builtin # offset_of($Container, $($fields)+)} } + +/// Create a fresh instance of the inhabited ZST type `T`. +/// +/// Prefer this to [`zeroed`] or [`uninitialized`] or [`transmute_copy`] +/// in places where you know that `T` is zero-sized, but don't have a bound +/// (such as [`Default`]) that would allow you to instantiate it using safe code. +/// +/// If you're not sure whether `T` is an inhabited ZST, then you should be +/// using [`MaybeUninit`], not this function. +/// +/// # Panics +/// +/// If `size_of::<T>() != 0`. +/// +/// # Safety +/// +/// - `T` must be *[inhabited]*, i.e. possible to construct. This means that types +/// like zero-variant enums and [`!`] are unsound to conjure. +/// - You must use the value only in ways which do not violate any *safety* +/// invariants of the type. +/// +/// While it's easy to create a *valid* instance of an inhabited ZST, since having +/// no bits in its representation means there's only one possible value, that +/// doesn't mean that it's always *sound* to do so. +/// +/// For example, a library could design zero-sized tokens that are `!Default + !Clone`, limiting +/// their creation to functions that initialize some state or establish a scope. Conjuring such a +/// token could break invariants and lead to unsoundness. +/// +/// # Examples +/// +/// ``` +/// #![feature(mem_conjure_zst)] +/// use std::mem::conjure_zst; +/// +/// assert_eq!(unsafe { conjure_zst::<()>() }, ()); +/// assert_eq!(unsafe { conjure_zst::<[i32; 0]>() }, []); +/// ``` +/// +/// [inhabited]: https://doc.rust-lang.org/reference/glossary.html#inhabited +#[unstable(feature = "mem_conjure_zst", issue = "95383")] +pub const unsafe fn conjure_zst<T>() -> T { + const_assert!( + size_of::<T>() == 0, + "mem::conjure_zst invoked on a nonzero-sized type", + "mem::conjure_zst invoked on type {t}, which is not zero-sized", + t: &str = stringify!(T) + ); + + // SAFETY: because the caller must guarantee that it's inhabited and zero-sized, + // there's nothing in the representation that needs to be set. + // `assume_init` calls `assert_inhabited`, so we don't need to here. + unsafe { + #[allow(clippy::uninit_assumed_init)] + MaybeUninit::uninit().assume_init() + } +} |
