-
Notifications
You must be signed in to change notification settings - Fork 17
Open
Labels
use caseA Rust/C++ interop usage exampleA Rust/C++ interop usage example
Description
An interop API has the following properties:
- a C++ method is safe to call in Rust on some pointer/reference types, but unsafe on others
- the Rust methods should have the same name, because the underlying C++ method is the same
- in Rust, the unsafe method will be called in an unsafe block, so using a suffixed name is redundant:
unsafe { MyCppType::do_the_thing_unsafe(…) }Example Code
This can be supported using inherent trait impls and trait-based Rust method name overloading. This allows writing the same method name with two different receiver types, one safe and one unsafe:
pub struct MyCppType;
impl MyCppType {
fn do_the_thing(&mut self) { ... }
}
trait MyCppTypeUnsafe {
unsafe fn do_the_thing(self: CMut<'_, Self>);
}
#[inherent]
impl MyCppTypeUnsafe for MyCppType { ... }Calls look like:
unsafe { MyCppTypeUnsafe::do_the_thing(...) }
MyCppType::do_the_thing(...)
<MyCppType as Self>::do_the_thing(...)Rust Compiler Features
Sources
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
use caseA Rust/C++ interop usage exampleA Rust/C++ interop usage example