-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Implement Default for &Option #140808
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
base: master
Are you sure you want to change the base?
Implement Default for &Option #140808
Conversation
r? libs-api |
Looking at this more closely, this is not as obvious as I thought. There doesn't seem to be any precedent in std for implementing Default for any reference types excluding unsized types like &str. Is this a slippery slope of adding impl Default to references to all std types? Or is there an obvious solution I'm missing? Also I realized that |
I'm honestly surprised that this works at all. Assuming it does work, it seems reasonable to add. |
I wonder, would it be possible to make the derive smarter to handle all reference types? Rather than needing to add this on a case-by-case basis. |
@rfcbot merge |
Team member @joshtriplett has proposed to merge this. The next step is review by the rest of the tagged team members: Concerns:
Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
I'm concerned about the forward-compatibility of this with a more general @rfcbot concern const-trait |
Pretty straightforward, but here is some justification:
I wasn't able to derive Default for my struct containing a
&Option<MyType>
because I can't implementDefault
for&Option<MyType>
because of the orphan rule. I suppose this is an unusual case becauseOption<&T>
is generally preferred over&Option<T>
. But I think adding this is justified for a couple reasons. 1) It removes an unnecessary speed bump. 2) I think there are times when&Option<T>
is preferable. In my project I am creating and passing around&Option<MyType>
in a lot of places, but only actually usingMyType
in one place. So it's not worth the ergonomic cost of having to writebuild_mytype(...).as_ref()
instead of&build_mytype(...)
in many places.r? libs