Skip to content

Trait object coercion without as #10039

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

Closed
alexcrichton opened this issue Oct 23, 2013 · 4 comments
Closed

Trait object coercion without as #10039

alexcrichton opened this issue Oct 23, 2013 · 4 comments
Labels
A-grammar Area: The grammar of Rust A-type-system Area: Type system P-medium Medium priority

Comments

@alexcrichton
Copy link
Member

When creating a trait object, I shouldn't have to write &mut obj as &mut Trait, I should be able to just write &mut obj and the compiler will coerce it to &mut Trait.

I remember awhile ago that @pcwalton had a patch to do this, but then it fell by the wayside. I was under the impression that we want to get this done, so I'm nominating this for 1.0.

@nikomatsakis
Copy link
Contributor

I really want this. I tried to port the visitor code to use objects and it was horribly tedious to insert the manual coercions. I would be happy to work on it once I get some of my current branches cleared off, but also happy to review anyone else's efforts.

@catamorphism
Copy link
Contributor

High priority but not 1.0

@wycats
Copy link
Contributor

wycats commented Nov 15, 2013

👍 I found this pretty surprising.

Just improving the error message would go a long way here.

@tomdale
Copy link

tomdale commented Dec 21, 2013

I'd like to give my 👍 to this.

Running into this was confusing as a first-time Rust user, and the code we ended needing to write up became very obtuse. I'm pretty sure I couldn't have figured it out with nagging people in the IRC channel. It makes code that should be straightforward even more annoying if you want to re-use the coerced object in multiple places by pulling it into a variable:

// Where OutputWriter is a struct and OutputStream is a trait it implements
let mut writer = OutputWriter::new(counter.bytecount);
let &mut writer_stream = &mut writer as &mut OutputStream;

do_something_with_writer_stream(writer_stream);

// Calling writer.bytes() moves the original writer out, so
// the compiler complains because there is still a mutable loan outstanding.
writer.bytes();

@bors bors closed this as completed in 1a9c8cc Dec 27, 2013
flip1995 pushed a commit to flip1995/rust that referenced this issue Dec 17, 2022
…time, r=dswij

Use ubuntu-20.04 instead of ubuntu-latest

changelog: none

r? `@ghost`

I'll create an issue, to undo this change, once the CI passes on `ubuntu-latest` again
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-grammar Area: The grammar of Rust A-type-system Area: Type system P-medium Medium priority
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants