rust-lang / rust-lang/rfcs

Method for `Option<&T>` that converts to `*const T`

Open
#1,652 10 comments 16 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 find myself often wanting to go from an Option<&T> to a *const T, which SHOULD be an easy and safe conversion, as every Option<&T> is guaranteed to correspond a *const T identical to it in representation. We already have a conversion, std::pointer::as_ref, the other way around, so I find it strange that the shortest way to do so (without defining a new function) is this:

.map_or(ptr::null(), |x| x)

It's just as easy to do the same for Option<&mut T> -> *mut T, of course, although I can't see the utility in it.

It's not as fundamental as Option<&T> -> *const T, but it would also be nice to have Option<&CStr> -> *const c_char.

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

Start with the issue's proposed conversions and compare them with the existing std::pointer::as_ref API. Follow the Rust RFC process and related standard-library pointer APIs to determine the desired scope, including Option<&T>, Option<&mut T>, and Option<&CStr>. Done means the API design is resolved and the relevant RFC or implementation work is accepted.

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
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.