-
Notifications
You must be signed in to change notification settings - Fork 20
implement Default
for raw pointers
#571
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
Comments
Defaulting raw pointers to null does seem reasonable, as especially That said, I suppose one could argue that the same effect can be obtained with |
I guess that probably the biggest downside to I kind of feel like |
previous discussion rust-lang/rfcs#2464 |
We discussed this in the @rust-lang/libs-api meeting and are happy to accept this. I've started an FCP on the PR (rust-lang/rust#139535). |
Implement `Default` for raw pointers ACP: rust-lang/libs-team#571 This is instantly stable so we will need an FCP here. Closes rust-lang/rfcs#2464
Implement `Default` for raw pointers ACP: rust-lang/libs-team#571 This is instantly stable so we will need an FCP here. Closes rust-lang/rfcs#2464
Rollup merge of rust-lang#139535 - ChrisDenton:default-ptr, r=tgross35 Implement `Default` for raw pointers ACP: rust-lang/libs-team#571 This is instantly stable so we will need an FCP here. Closes rust-lang/rfcs#2464
Implement `Default` for raw pointers ACP: rust-lang/libs-team#571 This is instantly stable so we will need an FCP here. Closes rust-lang/rfcs#2464
Proposal
Problem statement
Pointers do not implement
Default
despite there being a natural default value (as shown byis_null
).Default
is however implemented forAtomicPtr
.Motivating examples or use cases
This works nicely, with all the primitives being zeroed by default:
But if you add a pointer:
Then you get this error:
To workaround this, each struct containing a pointer can manually implement
Default
instead of deriving it. The safe way to do so is quite verbose, especially when structs get larger:So people will often reach for
unsafe
to make life easier:This is more succinct (if still more verbose than a derive), however now we've introduced a completely unnecessary use of
unsafe
. This matters because now the guard rails are off. It won't protect you if there are values that cannot be zeroed (there are some lints that can help but they necessarily can't catch everything).Solution sketch
Note: These will be instantly stable.
Alternatives
The status quo.
Links and related work
Default
could (safely!) be used in similar situations to C's= {};
initialiser without needing more than a derive.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: