dtolnay/cxx

Support UniquePtr as self type of extern C++ member function

Offen

#690 geöffnet am 27.01.2021

 (0 Kommentare) (5 Reaktionen) (0 zugewiesene Personen)Rust (253 Forks)batch import
help wanted

Repository-Metriken

Stars
 (4.472 Sterne)
PR-Merge-Metriken
 (Durchschn. Merge 8m) (3 gemergte PRs in 30 T)

Beschreibung

I'd like to be able to write:

#[cxx::bridge]
mod ffi {
    unsafe extern "C++" {
        type Thing;

        fn method(self: &UniquePtr<Thing>, arg: i32);
    }
}

and have this translate a Rust uptr_thing.method(arg) (where the type of uptr_thing is UniquePtr<Thing>) to uptr_thing->method(arg) in C++, by way of std::unique_ptr's operator->.

This pattern turns out to be quite common for exposing threadsafe non-const member functions to Rust. A threadsafe non-const member function requires a this of type non-const Thing*, which cannot be obtained from simply a &Thing in Rust, but at the same time it does not require this to be exclusively held when called, so a &mut Thing argument in Rust is also not appropriate. Aside from #537 the easy and correct way to expose such threadsafe non-const member functions to Rust is by passing &UniquePtr<Thing> as seen above.

Contributor Guide