rust-lang / rust-lang/rust

Should `Vec::as_ptr()`/`Vec::as_mut_ptr()` return nullptr instead of the dangling value?

Open
#140,410 4 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-discussion T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

In Rust's stdlib, the only way to know if the return from as_mut_ptr() is to check the .capacity() of the Vec.

While this is fine on its own, it does create an interesting interaction with system's APIs which expect either NULL or a valid pointer.

I think updating .as_ptr() and .as_mut_ptr() to return nullptr in the case of capacity() == 0 could help marginally reduce errors when using external APIs written in C or C++ where this idiom is common. There's a lot of debug checking that will specifically check for NULL but this isn't possible when the pointer's value is 0x1, as an example.

Contributor guide

Open the contributing guide

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 by reviewing the Rust stdlib implementations and documentation for Vec::as_ptr(), Vec::as_mut_ptr(), and capacity()==0 behavior. Determine whether changing the returned pointer semantics is acceptable for existing users and C/C++ API interoperability; done requires a decided design plus corresponding implementation and tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
backend
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.