Trait upcasting coercion
See original GitHub issueProposal
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 typedyn Bar
to be cast to a trait object of typedyn Foo
so long asBar: 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
- This belongs to the “Trait and type system extensions” part of the lang team priorities.
Links and related work
- Previously established tracking issue: https://github.com/rust-lang/rust/issues/65991
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:
- Created 2 years ago
- Reactions:19
- Comments:8 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
the example seems backwards? should it not be:
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.
Is there some way to help regarding stabilising this feature?