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

Lifetimes on an opaque C++ type #608

Closed
dtolnay opened this issue Dec 28, 2020 · 1 comment · Fixed by #652
Closed

Lifetimes on an opaque C++ type #608

dtolnay opened this issue Dec 28, 2020 · 1 comment · Fixed by #652

Comments

@dtolnay
Copy link
Owner

dtolnay commented Dec 28, 2020

For example suppose we have a C++ type which stores references:

class Resource;

class Holder {
  Holder(const Resource &resource) : resource(resource) {}
  const Resource &resource;
};

std::shared_ptr<Holder> makeHolder(const Resource &resource) {
  return std::make_shared<Holder>(resource);
}

It would be most appropriate to describe this in Rust with a lifetime as:

#[cxx::bridge]
mod ffi {
    unsafe extern "C++" {
        type Resource;
        type Holder<'a>;

        fn makeHolder<'a>(resource: &'a Resource) -> SharedPtr<Holder<'a>>;

        // or with lifetime elision
        fn makeHolder(resource: &Resource) -> SharedPtr<Holder>;
    }
}

Currently CXX doesn't handle the lifetimes on an extern type:

  error[cxxbridge]: expected `=` or `:`
    ┌─ src/main.rs:5:20

  5 │         type Holder<'a>;
    │                    ^ expected `=` or `:`
@dtolnay
Copy link
Owner Author

dtolnay commented Dec 29, 2020

Hit an ICE while working on this: rust-lang/rust#80468. I think it happens only in otherwise erroneous code so it shouldn't be a blocker, but it's possible it would come up often when using this feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant