-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
TRPL: deref coercions #24722
TRPL: deref coercions #24722
Conversation
This is useful for writing custom pointer types. However, there’s a language | ||
feature related to `Deref`: ‘deref coercions’. Here’s the rule: If you have a | ||
type `U`, and it implements `Deref<Target=T>`, values of `&U` will | ||
automatically convert to a `&T`. Here’s an example: |
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.
s/convert/coerce/
I think the distinction may matter in some places? I'm not super sure though
Nice! Could this also mention somewhere that |
r=me with those nits otherwise though! |
Can we also mention (or link to) autoderef here somewhere? I'd like all of the deref magic to be mentioned in one place. |
issues fixed, and @Manishearth 's section added 😄 |
```rust,ignore | ||
f.foo(); | ||
(*f).foo(); | ||
(******f).foo(); |
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.
Did this mean to use &
instead of *
?
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.
Yes. eff.
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.
well, i guess it actually meant to use &
and *
alternating, since that's the behavior, right?
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.
I think this means to say that a value of type &&&&&&&&&&&&&&&&Foo
can still have methods defined on Foo
called as the compiler will insert as many *
operations as necessary (basically).
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.
adding to #24786 |
## Deref and method calls | ||
|
||
`Deref` will also kick in when calling a method. In other words, these are | ||
the same two things in Rust: |
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.
Which 'two things' is this referring to?
r? @alexcrichton