question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Trait upcasting coercion

See original GitHub issue

Proposal

Summary and problem statement

  • Add the trait_upcasting feature to the language.

Motivation, use-cases, and solution sketches

  • The trait_upcasting feature adds support for trait upcasting coercion. This allows a trait object of type dyn Bar to be cast to a trait object of type dyn Foo so long as Bar: Foo.
#![feature(trait_upcasting)]

trait Foo {}

trait Bar: Foo {}

impl Foo for i32 {}

impl<T: Foo + ?Sized> Bar for T {}

let foo: &dyn Foo = &123;
let bar: &dyn Bar = foo;

Prioritization

Links and related work

Initial people involved

Also cc @alexreg and @eddyb in case they’re interested.

What happens now?

This issue is part of the experimental MCP process described in RFC 2936. Once this issue is filed, a Zulip topic will be opened for discussion, and the lang-team will review open MCPs in its weekly triage meetings. You should receive feedback within a week or two.

This issue is not meant to be used for technical discussion. There is a Zulip stream for that. Use this issue to leave procedural comments, such as volunteering to review, indicating that you second the proposal (or third, etc), or raising a concern that you would like to be addressed.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:19
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
JeremyRubincommented, Jun 20, 2022

the example seems backwards? should it not be:

#![feature(trait_upcasting)]

trait Foo {}

trait Bar: Foo {}

impl Foo for i32 {}

impl<T: Foo + ?Sized> Bar for T {}

let foo: &dyn Bar = &123;
let bar: &dyn Foo = foo;

where Bar is able to cast up to Foo, as described “trait object of type dyn Bar to be cast to a trait object of type dyn Foo so long as Bar: Foo.”.

Of course, both directions could be possible, but it seems reversed here.

0reactions
edevilcommented, Jul 22, 2022

Is there some way to help regarding stabilising this feature?

Read more comments on GitHub >

github_iconTop Results From Across the Web

trait_upcasting - The Rust Unstable Book
The trait_upcasting feature adds support for trait upcasting coercion. This allows a trait object of type dyn Bar to be cast to a...
Read more >
Why doesn't Rust support trait object upcasting?
Still, as I said, Rust supports a lot of OO-style abstractions, and traits are allowed to inherit, forming something akin to a type...
Read more >
Traits, dynamic dispatch and upcasting
Values of sized types implicitly coerce to trait objects for any object-safe trait they implement. The second value is a pointer to the...
Read more >
Definition - Dyn upcast initiative
The condition comes from the existing infrastructure within the compiler. Currently the unsizing coercion on a trait object type allows it to: ......
Read more >
[Beginner] How to overcome the lack of "upcasting" for Traits ...
My use case for this is to share some common behavior between two (or more ) traits, I though : put the common...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found