-
Notifications
You must be signed in to change notification settings - Fork 20
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 Option::as_deref_inner: Option<&T> -> Option<&T::Target> where T: Deref
#407
Comments
Original tracking issue and stabilization for Any chance we could update the existing |
I don't think we could because impl<T> Option<T> {
fn as_deref(&self) -> Option<&T::Target> where T: Deref;
}
// replace T = &U
impl<U> Option<&U> {
fn as_deref(&self) -> Option<&U>;
} |
can we add a |
Option<&T> to Option<&T::Target> where T: Deref
Option::as_deref_inner: Option<&T> -> Option<&T::Target> where T: Deref
We discussed this in the libs-api meeting. We don't think this method outweighs the cost in readability: it's relatively uncommon and it's not immediately clear what it does when encountering this in code, and it doesn't seem to provide enough value over the alternative over |
Ty for letting me know that. Is improving compile errors when the build fail with |
Improving compile errors would be welcome. Current error, for the motivating use case above: error[E0308]: mismatched types
--> src/lib.rs:5:30
|
5 | index.get(&id).unwrap_or(&[])
| --------- ^^^ expected `&Vec<i32>`, found `&[_; 0]`
| |
| arguments to this method are incorrect
|
= note: expected reference `&Vec<i32>`
found reference `&[_; 0]`
help: the return type of this call is `&[_; 0]` due to the type of the argument passed
--> src/lib.rs:5:5
|
5 | index.get(&id).unwrap_or(&[])
| ^^^^^^^^^^^^^^^^^^^^^^^^^---^
| |
| this argument influences the return type of `unwrap_or` It should be possible for rustc to suggest replacing this specific instantiation of |
I could have also used this several times recently, the name |
Proposal
Problem statement
Currently, we have
Option::as_deref
to transform&Option<T>
toOption<&T::Target>
. But because there isimpl Deref for &T with Target = T
.as_deref(opt: &Option<&T>) where T: Deref
always returnsOption<&T>
, not what we look for,Option<&T::Target>
.Motivating examples or use cases
Given this code:
User could try to rewrite it by using combinators:
That was frustrating because
as_deref
promises that&Option<T> -> Option<&T::Target>
. But they don't see that whenT=&U
, it will become&Option<&U> -> Option<&U>
, which is not always what they want.Solution sketch
Add this implementation
Or add a lint to war about this pitfall if the build passes. If the build fails, guide people to use
map(|v| &**v)
instead.Alternatives
Links and related work
Zulip: https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/Make.20.60Option.3C.26T.3E.3A.3Aas_deref.28.29.20returns.20Option.3C.26T.3A.3ATarget.3E.60
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: