-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add "as raw pointer" methods to Box
#355
Comments
It is worth noting that this is entirely independent of rust-lang/unsafe-code-guidelines#326. It's about avoiding intermediate references on the way from |
I think we should do this regardless of it can can be done with casts, like how we have https://doc.rust-lang.org/std/ptr/fn.from_ref.html. |
Also note that the current situation is a hazard if we ever want to make |
@rust-lang/libs-api is there any way to have this nominated for an upcoming meeting, or so? :) |
This comment was marked as resolved.
This comment was marked as resolved.
We briefly discussed this in the libs-api meeting. Looks good to us. Feel free to open a tracking issue and open a PR to rust-lang/rust to add it as an unstable feature. |
add Box::as_ptr and Box::as_mut_ptr methods Unstably implements rust-lang/libs-team#355. Tracking issue: rust-lang#129090. r? libs-api
add Box::as_ptr and Box::as_mut_ptr methods Unstably implements rust-lang/libs-team#355. Tracking issue: rust-lang#129090. r? libs-api
Rollup merge of rust-lang#129091 - RalfJung:box_as_ptr, r=Amanieu add Box::as_ptr and Box::as_mut_ptr methods Unstably implements rust-lang/libs-team#355. Tracking issue: rust-lang#129090. r? libs-api
Proposal
Problem statement
When working with raw pointers, it can be necessary to obtain raw pointers to the contents of a container in a way that multiple such pointers can alias with each other arbitrarily. For
Vec
, we haveVec::as_mut_ptr
for that purpose, and that aliasing model interaction is even documented nowadays. However, forBox
, we have no such method. One has to instead writeaddr_of_mut!(*bx)
, which works because Box is a primitive and this does not go throughDerefMut
. (DerefMut
would be creating a reference and thus defeat the aliasing point of this.)Motivating examples or use cases
Here's an example of code that's wrong:
This came about in rustc itself due to a refactor that replaced
Vec
byBox
.Solution sketch
I'm not sure how to avoid the refactoring issue, since
Box
can't really haveself
methods, soas_mut_ptr
on aBox<[T]>
will always call the slice method and thus create an intermediate reference. But we could at least provide methods that do the right thing and are more discoverable than theaddr_of_mut!
trick:This would also complement the existing
into_raw
.Alternatives
We could do nothing and instead just document the
addr_of_mut!
pattern somewhere in theBox
type docs.Links and related work
Vec
hasas_ptr
/as_mut_ptr
with aliasing model guarantees.What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
Second, if there's a concrete solution:
The text was updated successfully, but these errors were encountered: