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

Upcasting trait object to supertrait #2368

Closed
comex opened this issue Mar 23, 2018 · 3 comments
Closed

Upcasting trait object to supertrait #2368

comex opened this issue Mar 23, 2018 · 3 comments
Labels
T-lang Relevant to the language team, which will review and decide on the RFC.

Comments

@comex
Copy link

comex commented Mar 23, 2018

If there's a trait Sub: Super, and I have a &Sub, I should be able to cast it to &Super.

This is essentially a special case of #1277, which proposes allowing arbitrary multi-trait trait objects, e.g. &(A + B + C + D), which can be upcasted to any subset of the list of traits, e.g. &(A + C). The problem is that achieving that seems to require either (a) generating an exponential number of vtables, one for each subset, (b) putting several different vtables in a single trait object reference ("obese pointers"), which has a lot of downsides, or (c) using some indirect approach that would likely compromise the efficiency of method lookup. None of these alternatives look good. That doesn't mean we should give up, since both multi-trait trait objects and arbitrary upcasting are highly desirable in order to improve consistency with the generics system, and generally to make things "just work". But it seems like there needs to be some significant experimentation and exploration before the feature can even be spec'ed out at the RFC level.

On the other hand, simply allowing upcasting to supertraits doesn't face the same obstacles. Even if rustc always generated a separate vtable for every trait in the hierarchy, the bloat would be linear in the number of traits, not exponential - much less of an issue. And it would be a straightforward optimization to have a given trait's vtable layout be based on one of its supertraits' (perhaps the first mentioned), with additional methods appended at the end, so that it can be upcasted to that supertrait just by bitcasting the vtable pointer. Thus, trait hierarchies which are long but linear wouldn't result in any additional overhead.

@Centril Centril added the T-lang Relevant to the language team, which will review and decide on the RFC. label Mar 23, 2018
@Centril
Copy link
Contributor

Centril commented Mar 23, 2018

Small note: I think this will be read by a wider audience and discussed better at the internals forum; The issue tracker here is mostly not read by the lang (or other) team.

@Ixrec
Copy link
Contributor

Ixrec commented Mar 24, 2018

afaik it already has an internals thread: https://internals.rust-lang.org/t/wheres-the-catch-with-box-read-write/6617

@Centril
Copy link
Contributor

Centril commented Mar 24, 2018

Yes, closing in favor of that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-lang Relevant to the language team, which will review and decide on the RFC.
Projects
None yet
Development

No branches or pull requests

3 participants