Skip to content

Tracking issue for release notes of #161710: Stabilize mem::conjure_zst #163351

Description

@rustbot

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:

  • 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?
# Stabilized APIs
- [`std::mem::conjure_zst`](https://doc.rust-lang.org/std/mem/fn.conjure_zst.html)

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.

# 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:

```rust
use std::cell::Cell;
use std::sync::atomic::{AtomicUsize, Ordering};

// look, I said the example was contrived, okay?
static HANDLE: Cell<*const AtomicUsize> = std::ptr::null();
static COUNTER: AtomicUsize = AtomicUsize::new(0);

// outside this module, you can't make your own `Handle`
pub struct Handle(());
impl Handle {
    pub fn setup() -> Handle {
        HANDLE.set(&raw const COUNTER);
        Handle(())
    }

    pub fn count(&self) -> usize {
        // SAFETY: We initialized `HANDLE` in the constructor, so, this is a valid pointer.
        let atomic = 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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-libsRelevant to the library team, which will review and decide on the PR/issue.release-blog-postMarks issues tracking what text to put in the release blog post.relnotesMarks issues that should be documented in the release notes of the next release.relnotes-tracking-issueMarks issues tracking what text to put in release notes.

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions