Skip to content

Commit 585bb0b

Browse files
committed
Tests for namespaced extern trivial types
1 parent 5e79c64 commit 585bb0b

File tree

5 files changed

+69
-1
lines changed

5 files changed

+69
-1
lines changed

tests/ffi/extra.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub mod ffi2 {
1313
impl UniquePtr<D> {}
1414
impl UniquePtr<E> {}
1515
impl UniquePtr<F> {}
16+
impl UniquePtr<G> {}
1617

1718
extern "C" {
1819
include!("tests/ffi/tests.h");
@@ -21,16 +22,23 @@ pub mod ffi2 {
2122
type E = crate::other::E;
2223
#[namespace (namespace = F)]
2324
type F = crate::other::f::F;
25+
#[namespace (namespace = G)]
26+
type G = crate::other::G;
2427

2528
fn c_take_trivial_ptr(d: UniquePtr<D>);
2629
fn c_take_trivial_ref(d: &D);
2730
fn c_take_trivial(d: D);
31+
fn c_take_trivial_ns_ptr(g: UniquePtr<G>);
32+
fn c_take_trivial_ns_ref(g: &G);
33+
fn c_take_trivial_ns(g: G);
2834
fn c_take_opaque_ptr(e: UniquePtr<E>);
2935
fn c_take_opaque_ref(e: &E);
3036
fn c_take_opaque_ns_ptr(e: UniquePtr<F>);
3137
fn c_take_opaque_ns_ref(e: &F);
3238
fn c_return_trivial_ptr() -> UniquePtr<D>;
3339
fn c_return_trivial() -> D;
40+
fn c_return_trivial_ns_ptr() -> UniquePtr<G>;
41+
fn c_return_trivial_ns() -> G;
3442
fn c_return_opaque_ptr() -> UniquePtr<E>;
3543
fn c_return_ns_opaque_ptr() -> UniquePtr<F>;
3644
}

tests/ffi/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ mod other {
4141
}
4242
}
4343

44+
#[repr(C)]
45+
pub struct G {
46+
pub g: u64,
47+
}
48+
49+
unsafe impl ExternType for G {
50+
type Id = type_id!("G::G");
51+
type Kind = Trivial;
52+
}
53+
4454
unsafe impl ExternType for D {
4555
type Id = type_id!("tests::D");
4656
type Kind = Trivial;

tests/ffi/tests.cc

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,12 +466,32 @@ void c_take_trivial_ref(const D& d) {
466466
cxx_test_suite_set_correct();
467467
}
468468
}
469+
469470
void c_take_trivial(D d) {
470471
if (d.d == 30) {
471472
cxx_test_suite_set_correct();
472473
}
473474
}
474475

476+
477+
void c_take_trivial_ns_ptr(std::unique_ptr<::G::G> g) {
478+
if (g->g == 30) {
479+
cxx_test_suite_set_correct();
480+
}
481+
}
482+
483+
void c_take_trivial_ns_ref(const ::G::G& g) {
484+
if (g.g == 30) {
485+
cxx_test_suite_set_correct();
486+
}
487+
}
488+
489+
void c_take_trivial_ns(::G::G g) {
490+
if (g.g == 30) {
491+
cxx_test_suite_set_correct();
492+
}
493+
}
494+
475495
void c_take_opaque_ptr(std::unique_ptr<E> e) {
476496
if (e->e == 40) {
477497
cxx_test_suite_set_correct();
@@ -496,7 +516,6 @@ void c_take_opaque_ns_ref(const ::F::F& f) {
496516
}
497517
}
498518

499-
500519
std::unique_ptr<D> c_return_trivial_ptr() {
501520
auto d = std::unique_ptr<D>(new D());
502521
d->d = 30;
@@ -509,6 +528,18 @@ D c_return_trivial() {
509528
return d;
510529
}
511530

531+
std::unique_ptr<::G::G> c_return_trivial_ns_ptr() {
532+
auto g = std::unique_ptr<::G::G>(new ::G::G());
533+
g->g = 30;
534+
return g;
535+
}
536+
537+
::G::G c_return_trivial_ns() {
538+
::G::G g;
539+
g.g = 30;
540+
return g;
541+
}
542+
512543
std::unique_ptr<E> c_return_opaque_ptr() {
513544
auto e = std::unique_ptr<E>(new E());
514545
e->e = 40;

tests/ffi/tests.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ namespace F {
1919
};
2020
}
2121

22+
namespace G {
23+
struct G {
24+
uint64_t g;
25+
};
26+
}
27+
2228
namespace tests {
2329

2430
struct R;
@@ -143,12 +149,18 @@ const rust::Vec<uint8_t> &c_try_return_ref_rust_vec(const C &c);
143149
void c_take_trivial_ptr(std::unique_ptr<D> d);
144150
void c_take_trivial_ref(const D& d);
145151
void c_take_trivial(D d);
152+
153+
void c_take_trivial_ns_ptr(std::unique_ptr<::G::G> g);
154+
void c_take_trivial_ns_ref(const ::G::G& g);
155+
void c_take_trivial_ns(::G::G g);
146156
void c_take_opaque_ptr(std::unique_ptr<E> e);
147157
void c_take_opaque_ns_ptr(std::unique_ptr<::F::F> f);
148158
void c_take_opaque_ref(const E& e);
149159
void c_take_opaque_ns_ref(const ::F::F& f);
150160
std::unique_ptr<D> c_return_trivial_ptr();
151161
D c_return_trivial();
162+
std::unique_ptr<::G::G> c_return_trivial_ns_ptr();
163+
::G::G c_return_trivial_ns();
152164
std::unique_ptr<E> c_return_opaque_ptr();
153165
std::unique_ptr<::F::F> c_return_ns_opaque_ptr();
154166

tests/test.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,13 @@ fn test_extern_trivial() {
223223
let d = ffi2::c_return_trivial_ptr();
224224
check!(ffi2::c_take_trivial_ptr(d));
225225
cxx::UniquePtr::new(ffi2::D { d: 42 });
226+
227+
let g = ffi2::c_return_trivial_ns();
228+
check!(ffi2::c_take_trivial_ns_ref(&g));
229+
check!(ffi2::c_take_trivial_ns(g));
230+
let g = ffi2::c_return_trivial_ns_ptr();
231+
check!(ffi2::c_take_trivial_ns_ptr(g));
232+
cxx::UniquePtr::new(ffi2::G { g: 42 });
226233
}
227234

228235
#[test]

0 commit comments

Comments
 (0)