-
Notifications
You must be signed in to change notification settings - Fork 48
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
Trait upcasting coercion #98
Comments
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. |
@rustbot second |
This has been seconded and the https://rust-lang.github.io/dyn-upcasting-coercion-initiative/ repository created to track this work! |
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 Of course, both directions could be possible, but it seems reversed here. |
Is there a workaround until this becomes available? |
yes, i think casting through a struct should work? struct CastFoo<'a>(&'a dyn Bar);
impl<'a> Foo for CastFoo<'a> {
fn fo_every_foo_method(&self, a:Param) -> Output {
self.0.that_method(a)
}
}
let bar: &dyn Bar = &123;
let tmp = CastFoo(bar);
let foo: &dyn Foo = &tmp; |
I guess I am doing almost exactly that now. But this code is so ugly that by "workaround" I meant something else :-) Thanks anyway. |
Is there some way to help regarding stabilising this feature? |
Proposal
Summary and problem statement
trait_upcasting
feature to the language.Motivation, use-cases, and solution sketches
trait_upcasting
feature adds support for trait upcasting coercion. This allows atrait object of type
dyn Bar
to be cast to a trait object of typedyn Foo
so long as
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.
The text was updated successfully, but these errors were encountered: