Should Vec::as_ptr()
/Vec::as_mut_ptr()
return nullptr instead of the dangling value?
#140410
Labels
C-discussion
Category: Discussion or questions that doesn't represent real issues.
T-libs-api
Relevant to the library API team, which will review and decide on the PR/issue.
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 ofcapacity() == 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 is0x1
, as an example.The text was updated successfully, but these errors were encountered: