Skip to content
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

Merged
merged 1 commit into from
Apr 25, 2015
Merged

TRPL: deref coercions #24722

merged 1 commit into from
Apr 25, 2015

Conversation

steveklabnik
Copy link
Member

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:
Copy link
Member

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

@alexcrichton
Copy link
Member

Nice! Could this also mention somewhere that Vec<T> will coerce to [T]? I think that's the only other major coercion that may need to be mentioned.

@alexcrichton
Copy link
Member

r=me with those nits otherwise though!

@Manishearth
Copy link
Member

Can we also mention (or link to) autoderef here somewhere? I'd like all of the deref magic to be mentioned in one place.

@steveklabnik
Copy link
Member Author

issues fixed, and @Manishearth 's section added 😄

```rust,ignore
f.foo();
(*f).foo();
(******f).foo();
Copy link
Member

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 *?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. eff.

Copy link
Member Author

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?

Copy link
Member

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).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexcrichton
Copy link
Member

@bors: r+ d70a994 rollup

@steveklabnik
Copy link
Member Author

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:
Copy link
Member

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?

steveklabnik added a commit to steveklabnik/rust that referenced this pull request Apr 25, 2015
bors added a commit that referenced this pull request Apr 25, 2015
@bors bors merged commit d70a994 into rust-lang:master Apr 25, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants