rust-lang / rust-lang/rfcs

Feature: unchecked access to enum interior

Open
#1,863 14 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

T-libs
Dominant language
Markdown
Stars
6.6k
Forks
1.7k
Avg merge
16h 14m
Merged PRs (30d)
1

Description

I'll use Option as an example, but this applies to any enum:

Sometimes you may have a *mut Option<T>, and want to get a *mut T from that, without dereferencing the pointer, and without checking whether it is actually a Some(T). AFAICT, this is currently impossible to do in rust, unless you have some way to construct a valid T (which would allow calculating an offset before-hand and then using pointer arithmetic).

Using mem::uninitialized(), or mem::zeroed() to construct a T before-hand to calculate the pointer offset doesn't work, because of enum layout optimization, which means it could be UB.

This came up when writing some concurrency code: in this case de-referencing the pointer would be invalid because the memory may be being concurrently written to. The unsafe code is able to be correct because it checks at run-time that it has exclusive access, and that it was indeed a Some(T) before it ever tries to dereference the pointer.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No files or tests are named; start by reviewing the RFC process and Rust’s enum-layout and raw-pointer rules. A complete contribution would need a concrete, reviewed proposal for unchecked enum-interior access that addresses the stated concurrency and undefined-behavior constraints.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.