Skip to content

Commit 0fac321

Browse files
committed
Adding tests for method calls in foreign namespaces
1 parent ddc146e commit 0fac321

File tree

8 files changed

+71
-3
lines changed

8 files changed

+71
-3
lines changed

tests/BUCK

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ rust_library(
1515
"ffi/extra.rs",
1616
"ffi/lib.rs",
1717
"ffi/module.rs",
18+
"ffi/class_in_ns.rs",
1819
],
1920
crate = "cxx_test_suite",
2021
deps = [
@@ -30,6 +31,7 @@ cxx_library(
3031
":bridge/source",
3132
":extra/source",
3233
":module/source",
34+
":class_in_ns/source",
3335
],
3436
headers = {
3537
"ffi/lib.rs.h": ":bridge/header",
@@ -52,3 +54,8 @@ rust_cxx_bridge(
5254
name = "module",
5355
src = "ffi/module.rs",
5456
)
57+
58+
rust_cxx_bridge(
59+
name = "class_in_ns",
60+
src = "ffi/class_in_ns.rs",
61+
)

tests/BUILD

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ rust_library(
1818
"ffi/extra.rs",
1919
"ffi/lib.rs",
2020
"ffi/module.rs",
21+
"ffi/class_in_ns.rs",
2122
],
2223
deps = [
2324
":impl",
@@ -32,6 +33,7 @@ cc_library(
3233
":bridge/source",
3334
":extra/source",
3435
":module/source",
36+
":class_in_ns/source",
3537
],
3638
hdrs = ["ffi/tests.h"],
3739
deps = [
@@ -57,3 +59,9 @@ rust_cxx_bridge(
5759
src = "ffi/module.rs",
5860
deps = [":impl"],
5961
)
62+
63+
rust_cxx_bridge(
64+
name = "class_in_ns",
65+
src = "ffi/class_in_ns.rs",
66+
deps = [":impl"],
67+
)

tests/ffi/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ fn main() {
66
}
77

88
CFG.include_prefix = "tests/ffi";
9-
let sources = vec!["lib.rs", "extra.rs", "module.rs"];
9+
let sources = vec!["lib.rs", "extra.rs", "module.rs", "class_in_ns.rs"];
1010
cxx_build::bridges(sources)
1111
.file("tests.cc")
1212
.flag_if_supported(cxxbridge_flags::STD)

tests/ffi/class_in_ns.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// To test receivers on a type in a namespace outide
2+
// the default. cxx::bridge blocks can only have a single
3+
// receiver type, and there can only be one such block per,
4+
// which is why this is outside.
5+
6+
#[rustfmt::skip]
7+
#[cxx::bridge(namespace = tests)]
8+
pub mod ffi3 {
9+
10+
extern "C" {
11+
include!("tests/ffi/tests.h");
12+
13+
#[namespace (namespace = I)]
14+
type I;
15+
16+
fn get(self: &I) -> u32;
17+
18+
#[namespace (namespace = I)]
19+
fn ns_c_return_unique_ptr_ns() -> UniquePtr<I>;
20+
}
21+
}

tests/ffi/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
clippy::trivially_copy_pass_by_ref
55
)]
66

7+
pub mod class_in_ns;
78
pub mod extra;
89
pub mod module;
910

tests/ffi/tests.cc

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,4 +647,14 @@ namespace other {
647647
cxx_test_suite_set_correct();
648648
}
649649
}
650-
}
650+
} // namespace other
651+
652+
namespace I {
653+
uint32_t I::get() const {
654+
return a;
655+
}
656+
657+
std::unique_ptr<I> ns_c_return_unique_ptr_ns() {
658+
return std::unique_ptr<I>(new I());
659+
}
660+
} // namespace I

tests/ffi/tests.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,16 @@ namespace other {
182182
void ns_c_take_trivial(::tests::D d);
183183
::tests::D ns_c_return_trivial();
184184
void ns_c_take_ns_shared(::A::AShared shared);
185-
} // namespace other
185+
} // namespace other
186+
187+
namespace I {
188+
class I {
189+
private:
190+
uint32_t a;
191+
public:
192+
I() : a(1000) {}
193+
uint32_t get() const;
194+
};
195+
196+
std::unique_ptr<I> ns_c_return_unique_ptr_ns();
197+
} // namespace I

tests/test.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use cxx_test_suite::class_in_ns::ffi3;
12
use cxx_test_suite::extra::ffi2;
23
use cxx_test_suite::ffi;
34
use std::cell::Cell;
@@ -193,6 +194,14 @@ fn test_c_method_calls() {
193194
assert!(unique_ptr.get_fail().is_err());
194195
}
195196

197+
#[test]
198+
fn test_c_ns_method_calls() {
199+
let unique_ptr = ffi3::ns_c_return_unique_ptr_ns();
200+
201+
let old_value = unique_ptr.get();
202+
assert_eq!(1000, old_value);
203+
}
204+
196205
#[test]
197206
fn test_enum_representations() {
198207
assert_eq!(0, ffi::Enum::AVal.repr);

0 commit comments

Comments
 (0)