Skip to content

Make it possible to create boxed iface instances without re-boxing #2299

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
marijnh opened this issue Apr 25, 2012 · 3 comments
Closed

Make it possible to create boxed iface instances without re-boxing #2299

marijnh opened this issue Apr 25, 2012 · 3 comments
Labels
A-type-system Area: Type system C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@marijnh
Copy link
Contributor

marijnh commented Apr 25, 2012

A boxed iface is a pair of a regular box holding the self value and a vtable pointer. When creating such a thing from a box, one currently has to do (*my_box) as my_iface. It would be great if we could somehow just bump the refcount of the box and slap a vtable on it.

Proposed solution: make cast-to-iface, when no impl is found, autoderef the left hand side. If it finds a match for the box' contents, it'll resolve to that impl, and the trans code will notice this and skip the boxing. So my_box as my_iface would be a very cheap operation.

@ghost ghost assigned marijnh Apr 25, 2012
@catamorphism
Copy link
Contributor

You don't have to do (*my_box) as my_iface anymore -- that is, this works:

struct Bird {
    x: int
}

trait Chirpy {
    fn tweet();
}

impl Bird: Chirpy {
    fn tweet() {}
}

fn main() {
    let x = @Bird{x: 5};
    let y = x as @Chirpy;
}

But looking at the generated code, it looks like the cast still involves copying x, so this is still an issue.

@nikomatsakis
Copy link
Contributor

It should not be copying the contents of x but just bumping the ref count.

@pcwalton
Copy link
Contributor

Fixed in 0.6.

bors added a commit to rust-lang-ci/rust that referenced this issue Sep 22, 2022
celinval pushed a commit to celinval/rust-dev that referenced this issue Jun 4, 2024
…tions (rust-lang#2967)

This PR is the next step to rework/introduce the
`should_panic`/`fail_uncoverable` options as global conditions.

Until now, we haven't had a concrete proposal to do so other than the
implementation in rust-lang#2532. This PR presents one for each option in their
respective RFCs. I'd like to agree on this design before starting the
code review for rust-lang#2532.

Related to rust-lang#1905 rust-lang#2272 rust-lang#2299 rust-lang#2516
jieyouxu added a commit to jieyouxu/rust that referenced this issue Apr 2, 2025
Slightly reorganize ecosystem tests, stub out codegen backend test pages
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-type-system Area: Type system C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

4 participants