Support calling C++ methods from Rust
These methods can be declared in the bridge by naming the first
argument self and making it a reference to the containing class, e.g.,
fn get(self: &C) -> usize;
fn set(self: &mut C, n: usize);
This syntax requires Rust 1.43.
Note that the implementation also changes the internal naming of shim
functions so that they also contain the name of the owning class, if
any. This allows defining multiple methods with the same name on
different objects.
diff --git a/tests/test.rs b/tests/test.rs
index 511c52b..36de8f9 100644
--- a/tests/test.rs
+++ b/tests/test.rs
@@ -107,6 +107,18 @@
check!(cxx_run_test());
}
+#[test]
+fn test_c_method_calls() {
+ let mut unique_ptr = ffi::c_return_unique_ptr();
+
+ let old_value = unique_ptr.as_ref().unwrap().get();
+ assert_eq!(2020, old_value);
+ assert_eq!(2021, unique_ptr.as_mut().unwrap().set(2021));
+ assert_eq!(2021, unique_ptr.as_ref().unwrap().get());
+ assert_eq!(old_value, unique_ptr.as_mut().unwrap().set(old_value));
+ assert_eq!(old_value, unique_ptr.as_ref().unwrap().get())
+}
+
#[no_mangle]
extern "C" fn cxx_test_suite_get_box() -> *mut cxx_test_suite::R {
Box::into_raw(Box::new(2020usize))