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

Support associated methods #51

Closed
dtolnay opened this issue Mar 2, 2020 · 3 comments
Closed

Support associated methods #51

dtolnay opened this issue Mar 2, 2020 · 3 comments

Comments

@dtolnay
Copy link
Owner

dtolnay commented Mar 2, 2020

Currently only freestanding functions are supported by this crate, not methods.

I like the approach of using self in the foreign function parameter list to signify a method receiver:

#[cxx::bridge]
mod ffi {
    extern "C" {
        type C;
        fn f(self: &C);
    }

    extern "Rust" {
        type R;
        fn g(self: &R);
    }
}

struct R {...}

impl R {
    fn g(&self) {...}
}

This would expose the methods r.g() to C++ and c.f() to Rust.

The above syntax would require rustc 1.43 or newer for rust-lang/rust#68728.

@myronahn
Copy link
Contributor

Would it make sense to use the &self shorthand instead of self: &C?

@dtolnay
Copy link
Owner Author

dtolnay commented Mar 25, 2020

That would be fine if there is only one extern type in the block.

@dtolnay
Copy link
Owner Author

dtolnay commented Apr 17, 2020

Implemented in #121.

I filed #124 to follow up on &self and &mut self shorthand inside of extern blocks that contain exactly one extern type.

@dtolnay dtolnay closed this as completed Apr 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants