Skip to content

Commit 5e79c64

Browse files
committed
Tests for namespaced opaque extern types.
1 parent 9a158e4 commit 5e79c64

File tree

5 files changed

+56
-0
lines changed

5 files changed

+56
-0
lines changed

tests/ffi/extra.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,26 @@
1212
pub mod ffi2 {
1313
impl UniquePtr<D> {}
1414
impl UniquePtr<E> {}
15+
impl UniquePtr<F> {}
1516

1617
extern "C" {
1718
include!("tests/ffi/tests.h");
1819

1920
type D = crate::other::D;
2021
type E = crate::other::E;
22+
#[namespace (namespace = F)]
23+
type F = crate::other::f::F;
2124

2225
fn c_take_trivial_ptr(d: UniquePtr<D>);
2326
fn c_take_trivial_ref(d: &D);
2427
fn c_take_trivial(d: D);
2528
fn c_take_opaque_ptr(e: UniquePtr<E>);
2629
fn c_take_opaque_ref(e: &E);
30+
fn c_take_opaque_ns_ptr(e: UniquePtr<F>);
31+
fn c_take_opaque_ns_ref(e: &F);
2732
fn c_return_trivial_ptr() -> UniquePtr<D>;
2833
fn c_return_trivial() -> D;
2934
fn c_return_opaque_ptr() -> UniquePtr<E>;
35+
fn c_return_ns_opaque_ptr() -> UniquePtr<F>;
3036
}
3137
}

tests/ffi/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,22 @@ mod other {
2525
e_str: CxxString,
2626
}
2727

28+
pub mod f {
29+
use cxx::kind::Opaque;
30+
use cxx::{type_id, CxxString, ExternType};
31+
32+
#[repr(C)]
33+
pub struct F {
34+
e: u64,
35+
e_str: CxxString,
36+
}
37+
38+
unsafe impl ExternType for F {
39+
type Id = type_id!("F::F");
40+
type Kind = Opaque;
41+
}
42+
}
43+
2844
unsafe impl ExternType for D {
2945
type Id = type_id!("tests::D");
3046
type Kind = Trivial;

tests/ffi/tests.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,12 +478,25 @@ void c_take_opaque_ptr(std::unique_ptr<E> e) {
478478
}
479479
}
480480

481+
void c_take_opaque_ns_ptr(std::unique_ptr<::F::F> f) {
482+
if (f->f == 40) {
483+
cxx_test_suite_set_correct();
484+
}
485+
}
486+
481487
void c_take_opaque_ref(const E& e) {
482488
if (e.e == 40 && e.e_str == "hello") {
483489
cxx_test_suite_set_correct();
484490
}
485491
}
486492

493+
void c_take_opaque_ns_ref(const ::F::F& f) {
494+
if (f.f == 40 && f.f_str == "hello") {
495+
cxx_test_suite_set_correct();
496+
}
497+
}
498+
499+
487500
std::unique_ptr<D> c_return_trivial_ptr() {
488501
auto d = std::unique_ptr<D>(new D());
489502
d->d = 30;
@@ -503,6 +516,13 @@ std::unique_ptr<E> c_return_opaque_ptr() {
503516
return e;
504517
}
505518

519+
std::unique_ptr<::F::F> c_return_ns_opaque_ptr() {
520+
auto f = std::unique_ptr<::F::F>(new ::F::F());
521+
f->f = 40;
522+
f->f_str = std::string("hello");
523+
return f;
524+
}
525+
506526
extern "C" const char *cxx_run_test() noexcept {
507527
#define STRINGIFY(x) #x
508528
#define TOSTRING(x) STRINGIFY(x)

tests/ffi/tests.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ namespace A {
1212
} // namespace B
1313
} // namespace A
1414

15+
namespace F {
16+
struct F {
17+
uint64_t f;
18+
std::string f_str;
19+
};
20+
}
21+
1522
namespace tests {
1623

1724
struct R;
@@ -137,10 +144,13 @@ void c_take_trivial_ptr(std::unique_ptr<D> d);
137144
void c_take_trivial_ref(const D& d);
138145
void c_take_trivial(D d);
139146
void c_take_opaque_ptr(std::unique_ptr<E> e);
147+
void c_take_opaque_ns_ptr(std::unique_ptr<::F::F> f);
140148
void c_take_opaque_ref(const E& e);
149+
void c_take_opaque_ns_ref(const ::F::F& f);
141150
std::unique_ptr<D> c_return_trivial_ptr();
142151
D c_return_trivial();
143152
std::unique_ptr<E> c_return_opaque_ptr();
153+
std::unique_ptr<::F::F> c_return_ns_opaque_ptr();
144154

145155
rust::String cOverloadedFunction(int32_t x);
146156
rust::String cOverloadedFunction(rust::Str x);

tests/test.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,4 +230,8 @@ fn test_extern_opaque() {
230230
let e = ffi2::c_return_opaque_ptr();
231231
check!(ffi2::c_take_opaque_ref(e.as_ref().unwrap()));
232232
check!(ffi2::c_take_opaque_ptr(e));
233+
234+
let f = ffi2::c_return_ns_opaque_ptr();
235+
check!(ffi2::c_take_opaque_ns_ref(f.as_ref().unwrap()));
236+
check!(ffi2::c_take_opaque_ns_ptr(f));
233237
}

0 commit comments

Comments
 (0)