Skip to content

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

Open
@cmazakas

Description

@cmazakas

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-discussionCategory: Discussion or questions that doesn't represent real issues.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions