-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Introduce Option::<&T>::borrowed #1792
Conversation
I'm not sure about |
Yeah, I know I've at least burned One thing I worry about is the overlap between |
I believe that the only difference is that |
I would like to focus on AsRef, and not spread Borrow too far away from its use case (which was originally HashMap::get, BTreeMap::get). |
That seems reasonable, though naming will be a bit less obvious (please no |
There's probably some connection with #1403, as people have mentioned |
Isn't that already a lost battle, with |
The book says:
So I would rather prefer we use |
Borrow is constrained by its hash/ordering guarantees, and those have no special connection to this use case. AsRef is more general and versatile, supporting |
I also believe that this would be |
I agree with @bluss that we likely want to leverage Also the RFC motivation may want to be updated slightly, the stated problem is:
Yet the RFC does not solve this problem as it defines a method on impl<T> Option<T> {
fn name<U: Bound<T>>(&self) -> Option<&U> {
self.as_ref().map(Bound::method)
}
} |
I just meant that with this method, we have the solution |
AFAIK, |
It should also be noted that there is no implementation of |
I'm not bothered by If
|
That would mean calling .as_ref().as_refed() which is even uglier. I don't understand why AsRef is promoted as the way to go even for generic functions when that trait doesn't even have an impl for T. |
On the other hand, the lack of a general identity impl lets For example, you can convert from Coherence stops us from adding a corresponding feature to |
How often do you hold onto |
I really hope this ends up going through the coercion mechanism and not more ad-hoc hierarchies. |
This please! Allow any |
I would prefer it opt-in (like |
Using As for implicit coercions, that's a rabbit hole I'm very wary of. |
It's not like we must limit ourselves to only one choice... Would people find |
Any news on this? |
Happy new year. Was a decision reached for that? |
[motivation]: #motivation | ||
|
||
How to convert from `Option<String>` to `Option<&str>` is sometimes asked on | ||
Stack Overflow [1] [2]. This use case is also common in Servo, along other |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: "along with other" rather than "along other."
What? Why? |
https://doc.rust-lang.org/std/option/enum.Option.html#method.cloned takes |
I commented above that I commented farther above with rationale for |
er didn't mean to close |
This RFC doesn't really make the common use case of "borrowing" To the best of my knowledge, the shortest way to do it currently is: opt_string.as_ref().map(|s| s.as_str()) which this RFC improves only marginally to: opt_string.as_ref().borrowed() What I'd like to do is get rid of impl<T> Option<T> {
fn borrowed<U>(&self) -> Option<&U>
where T: Borrow<U>
{
self.as_ref().map(|x| x.borrow())
}
} which may or may not be rephrasable to an equally powerful |
If we do that, what happens for the type |
@nox If I understand correctly, this is a reason to do both
|
@daboross |
@nox Sorry, I know I should probably be getting this, but I don't seem to be. Here's an reference implementation of
http://play.integer32.com/?gist=6d1861f23259da72e8791a232e36ccc9 If there was a case I missed, could you elaborate on it specifically? I really do want to find a good solution for this, I just can't find a place where |
It doesn't work for arbitrary types.
…--
Anthony Ramine
Le 22 mai 2017 à 09:10, David Ross ***@***.***> a écrit :
If there was a case I missed, could you elaborate on it specifically?
|
OK - I think I understand now. The identity not being there with If it isn't about the I can get behind a Thank you for clarifying that, and sorry I didn't completely get that before. |
I'm sorry for the lack of follow-up here; it's been difficult to prioritize this particular API, given some of the issues we've covered on this thread. At this point, I'm inclined to postpone consideration; there's enough else going on that I'd like to let the dust settle a bit and see whether language-based solutions are shaping up. @rfcbot fcp postpone |
Team member @aturon has proposed to postpone this. The next step is review by the rest of the tagged teams: No concerns currently listed. Once these reviewers reach consensus, 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. |
@alexcrichton Sorry but I don't see a rationale in any of these two comments. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
3 similar comments
🔔 This is now entering its final comment period, as per the review above. 🔔 |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
whoa there rfcbot... |
I'm going to close this RFC as postponed. This is still a significant paper cut that would be great to fix, but work during the impl period on the trait system may provide an alternative, more general way of doing so. Thanks, @nox! |
No description provided.