You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Tracking issue for release notes of #161710: Stabilize mem::conjure_zst #163351
See the forge.rust-lang.org chapter about release notes for an overview of how the release team makes use of these tracking issues.
Release notes text
This section should be edited to specify the correct category(s) for the change, with succinct description(s) of what changed. Some things worth considering:
Does this need an additional compat notes section?
Was this a libs stabilization that should have additional headers to list new APIs under Stabilized APIs and Const Stabilized APIs?
Use the previous releases for inspiration on how to write the release notes text and which categories to pick.
Release blog section
If this change is notable enough for inclusion in the blog post then this section should be edited to contain a draft for the blog post. Otherwise leave it empty.
# Conjuring zero-sized types
As Rust's [unsafe code guidelines](https://github.com/rust-lang/unsafe-code-guidelines) are still being decided, it became clear that it would be very useful to decide what exactly it means to create zero-sized types in an unsafe way, and what we should recommend for people to do this.
This question is answered in Rust 1.100 with a single function: [`std::mem::conjure_zst`](https://doc.rust-lang.org/stable/std/mem/fn.conjure_zst.html).
Instead of transmuting `()`, `MaybeUninit::uninit().assume_init()`, or various other incantations, this is the dedicated way to create a value of a zero-sized type when you know that doing so is safe to do. If the type has no size, we know exactly what value it must be: nothing.
Of course, APIs often rely on the inability to construct such types for safety, and that's why this function is unsafe. For example, take this contrived example:
```rustusestd::cell::Cell;
usestd::sync::atomic::{AtomicUsize, Ordering};
// look, I said the example was contrived, okay?staticHANDLE:Cell<*constAtomicUsize> =std::ptr::null();
staticCOUNTER:AtomicUsize=AtomicUsize::new(0);
// outside this module, you can't make your own `Handle`pubstructHandle(());
implHandle {
pubfnsetup() ->Handle {
HANDLE.set(&rawconstCOUNTER);
Handle(())
}
pubfncount(&self) ->usize {
// SAFETY: We initialized `HANDLE` in the constructor, so, this is a valid pointer.letatomic=unsafe { &*HANDLE.get() };
atomic.fetch_add(1, Ordering::Relaxed)
}
}
```
Here, the module exploits the fact that `Handle`*cannot be used* without first setting up `HANDLE`, to make the `count` function safe. If we could just `conjure_zst::<Handle>()` in safe code, this is no longer sound, which is why it's unsafe to call `conjure_zst`.
Note
If a blog post section is required the release-blog-post label should be added (@rustbot label +release-blog-post) to this issue as otherwise it may be missed by the release team.
This issue tracks the release notes text for #161710.
cc @theemathas, @clarfonthey -- original issue/PR authors and assignees for drafting text
See the forge.rust-lang.org chapter about release notes for an overview of how the release team makes use of these tracking issues.
Release notes text
This section should be edited to specify the correct category(s) for the change, with succinct description(s) of what changed. Some things worth considering:
Stabilized APIsandConst Stabilized APIs?Tip
Use the previous releases for inspiration on how to write the release notes text and which categories to pick.
Release blog section
If this change is notable enough for inclusion in the blog post then this section should be edited to contain a draft for the blog post. Otherwise leave it empty.
Note
If a blog post section is required the
release-blog-postlabel should be added (@rustbot label +release-blog-post) to this issue as otherwise it may be missed by the release team.