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 |
| impl<'a, T> Default for &'a Option<T> { | ||
| /// Returns `&None` | ||
| #[inline] | ||
| fn default() -> &'a Option<T> { | ||
| &None | ||
| } | ||
| } |
There was a problem hiding this comment.
ymmv: I'm pretty sure you can remove the explicit lifetime here by writing it as
| impl<'a, T> Default for &'a Option<T> { | |
| /// Returns `&None` | |
| #[inline] | |
| fn default() -> &'a Option<T> { | |
| &None | |
| } | |
| } | |
| impl<T> Default for &Option<T> { | |
| /// Returns `&None` | |
| #[inline] | |
| fn default() -> Self { | |
| &None | |
| } | |
| } |
|
Regarding the concern about forward-compatibility with what we might want with const traits, a |
|
This could be experimented with when #134628 lands (it's already approved) |
I think that impl should not exist because of those constraints? This impl on |
|
I think the point is to add an additional (though in this specific case unnecessary) bound on the |
|
I feel like I'm missing something here. Isn't it not possible to add an impl for all |
|
No. No user-defined type currently implements |
|
I see. Thanks for spelling that out. 😅 Back to my previous comment then, if we add fn test() -> &'static Option<MyType> {
Default::default() // error: not MyType is not Freeze
}
#[derive_const(Default)]
enum MyType {
Foo(Cell<u32>),
#[default]
Bar
} |
|
@Amanieu proposal cancelled. |
|
Team member @Amanieu 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. |
|
Re-adding concern:
@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 implementDefaultfor&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 usingMyTypein 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