Skip to content

Commit 63a97c0

Browse files
authored
Merge pull request #1571 from dtolnay/pr1233
Touch up PR 1233
2 parents b77f320 + 98ce4f8 commit 63a97c0

File tree

3 files changed

+22
-23
lines changed

3 files changed

+22
-23
lines changed

tests/cxx_gen.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,26 @@ fn test_impl_annotation() {
3434
const BRIDGE1: &str = r#"
3535
#[cxx::bridge]
3636
mod ffi {
37-
unsafe extern "C++" {
37+
extern "C++" {
3838
type CppType;
3939
}
4040
4141
extern "Rust" {
42-
fn rust_method_cpp_context(self: Pin<&mut CppType>);
42+
fn rust_method_cpp_receiver(self: Pin<&mut CppType>);
4343
}
4444
}
4545
"#;
4646

47-
// Ensure that implementing a Rust method on a C++ type only causes generation
48-
// of the implementation.
47+
// Ensure that implementing a Rust method on an opaque C++ type only causes
48+
// generation of the member function definition, not a member function
49+
// declaration in a class definition.
4950
//
50-
// The header should be implemented in the C++ class definition and the Rust
51-
// implementation in the usual way.
51+
// The member function declaration will come from whichever header provides the
52+
// C++ class definition.
5253
//
53-
// This allows for developers and crates that are generating both C++ and Rust
54-
// code to have a C++ method implemented in Rust without having to use a
55-
// free method and passing through the C++ "this" as an argument.
54+
// This allows for developers and crates that are producing both C++ and Rust
55+
// code to have a C++ method implemented in Rust without having to use a free
56+
// function and passing through the C++ "this" as an argument.
5657
#[test]
5758
fn test_extern_rust_method_on_c_type() {
5859
let opt = Opt::default();
@@ -61,17 +62,14 @@ fn test_extern_rust_method_on_c_type() {
6162
let header = str::from_utf8(&generated.header).unwrap();
6263
let implementation = str::from_utf8(&generated.implementation).unwrap();
6364

64-
// To avoid continual breakage we won't test every byte.
65-
// Let's look for the major features.
66-
67-
// Check that the header doesn't have the Rust method
68-
assert!(!header.contains("rust_method_cpp_context"));
65+
// Check that the header doesn't have the Rust method.
66+
assert!(!header.contains("rust_method_cpp_receiver"));
6967

70-
// Check that there is a cxxbridge to the Rust method
68+
// Check that there is a generated C signature bridging to the Rust method.
7169
assert!(implementation
72-
.contains("void cxxbridge1$CppType$rust_method_cpp_context(::CppType &self) noexcept;"));
70+
.contains("void cxxbridge1$CppType$rust_method_cpp_receiver(::CppType &self) noexcept;"));
7371

74-
// Check that there is a implementation on the C++ class calling the Rust method
75-
assert!(implementation.contains("void CppType::rust_method_cpp_context() noexcept {"));
76-
assert!(implementation.contains("cxxbridge1$CppType$rust_method_cpp_context(*this);"));
72+
// Check that there is an implementation on the C++ class calling the Rust method.
73+
assert!(implementation.contains("void CppType::rust_method_cpp_receiver() noexcept {"));
74+
assert!(implementation.contains("cxxbridge1$CppType$rust_method_cpp_receiver(*this);"));
7775
}

tests/ffi/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ pub mod ffi {
324324
fn set(self: &mut R, n: usize) -> usize;
325325
fn r_method_on_shared(self: &Shared) -> String;
326326
fn r_get_array_sum(self: &Array) -> i32;
327-
// Ensure that a Rust method can be implemented on a C++ type
327+
// Ensure that a Rust method can be implemented on an opaque C++ type.
328328
fn r_method_on_c_get_mut(self: Pin<&mut C>) -> &mut usize;
329329

330330
#[cxx_name = "rAliasedFunction"]
@@ -450,7 +450,7 @@ impl ffi::Array {
450450
}
451451
}
452452

453-
// A Rust method implemented on the C++ type
453+
// A Rust method implemented on an opaque C++ type.
454454
impl ffi::C {
455455
pub fn r_method_on_c_get_mut(self: core::pin::Pin<&mut Self>) -> &mut usize {
456456
self.getMut()

tests/ffi/tests.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ class C {
5454
rust::String cOverloadedMethod(int32_t x) const;
5555
rust::String cOverloadedMethod(rust::Str x) const;
5656
static size_t c_static_method();
57-
// Note that the implementation of this method is generated by CXX itself
58-
// which is then bridged to a Rust method but with the C++ type as self
57+
// Unlike the other contents of this class, the C++ definition of this member
58+
// function is generated by CXX and forwards to a Rust method implementation
59+
// in an `impl ffi::C` block.
5960
size_t &r_method_on_c_get_mut() noexcept;
6061

6162
private:

0 commit comments

Comments
 (0)