From 63c1043dfb92fcac0bf3073c2d71e9e4ac69c943 Mon Sep 17 00:00:00 2001 From: Kendall Koning Date: Sat, 19 Jun 2021 12:11:33 -0400 Subject: [PATCH] Simplify Pin to T cbindgen already simplifies `MaybeUninit` and `ManuallyDrop` to `T`. This adds `Pin` as well. --- docs.md | 2 ++ src/bindgen/ir/ty.rs | 2 +- tests/expectations/pin.both.c | 27 +++++++++++++++++++++ tests/expectations/pin.both.compat.c | 35 ++++++++++++++++++++++++++++ tests/expectations/pin.c | 27 +++++++++++++++++++++ tests/expectations/pin.compat.c | 35 ++++++++++++++++++++++++++++ tests/expectations/pin.cpp | 32 +++++++++++++++++++++++++ tests/expectations/pin.pyx | 29 +++++++++++++++++++++++ tests/expectations/pin.tag.c | 27 +++++++++++++++++++++ tests/expectations/pin.tag.compat.c | 35 ++++++++++++++++++++++++++++ tests/expectations/pin.tag.pyx | 29 +++++++++++++++++++++++ tests/rust/pin.rs | 8 +++++++ tests/rust/pin.toml | 21 +++++++++++++++++ 13 files changed, 308 insertions(+), 1 deletion(-) create mode 100644 tests/expectations/pin.both.c create mode 100644 tests/expectations/pin.both.compat.c create mode 100644 tests/expectations/pin.c create mode 100644 tests/expectations/pin.compat.c create mode 100644 tests/expectations/pin.cpp create mode 100644 tests/expectations/pin.pyx create mode 100644 tests/expectations/pin.tag.c create mode 100644 tests/expectations/pin.tag.compat.c create mode 100644 tests/expectations/pin.tag.pyx create mode 100644 tests/rust/pin.rs create mode 100644 tests/rust/pin.toml diff --git a/docs.md b/docs.md index 5bece0cb1..3dd983e36 100644 --- a/docs.md +++ b/docs.md @@ -161,6 +161,8 @@ cbindgen contains the following hardcoded mappings (again completely ignoring na * PhantomData => *evaporates*, can only appear as the field of a type * PhantomPinned => *evaporates*, can only appear as the field of a type * () => *evaporates*, can only appear as the field of a type +* MaybeUninit, ManuallyDrop, and Pin => T + diff --git a/src/bindgen/ir/ty.rs b/src/bindgen/ir/ty.rs index 0bae11c85..1dda7af1e 100644 --- a/src/bindgen/ir/ty.rs +++ b/src/bindgen/ir/ty.rs @@ -630,7 +630,7 @@ impl Type { is_ref: false, }), "Cell" => Some(generic.into_owned()), - "ManuallyDrop" | "MaybeUninit" if config.language != Language::Cxx => { + "ManuallyDrop" | "MaybeUninit" | "Pin" if config.language != Language::Cxx => { Some(generic.into_owned()) } _ => None, diff --git a/tests/expectations/pin.both.c b/tests/expectations/pin.both.c new file mode 100644 index 000000000..c43b4c345 --- /dev/null +++ b/tests/expectations/pin.both.c @@ -0,0 +1,27 @@ +#if 0 +''' ' +#endif + +#ifdef __cplusplus +template +using Pin = T; +template +using Box = T*; +#endif + +#if 0 +' ''' +#endif + + +#include +#include +#include +#include + +typedef struct PinTest { + int32_t *pinned_box; + int32_t *pinned_ref; +} PinTest; + +void root(int32_t *s, struct PinTest p); diff --git a/tests/expectations/pin.both.compat.c b/tests/expectations/pin.both.compat.c new file mode 100644 index 000000000..126cb6663 --- /dev/null +++ b/tests/expectations/pin.both.compat.c @@ -0,0 +1,35 @@ +#if 0 +''' ' +#endif + +#ifdef __cplusplus +template +using Pin = T; +template +using Box = T*; +#endif + +#if 0 +' ''' +#endif + + +#include +#include +#include +#include + +typedef struct PinTest { + int32_t *pinned_box; + int32_t *pinned_ref; +} PinTest; + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +void root(int32_t *s, struct PinTest p); + +#ifdef __cplusplus +} // extern "C" +#endif // __cplusplus diff --git a/tests/expectations/pin.c b/tests/expectations/pin.c new file mode 100644 index 000000000..df4cd3200 --- /dev/null +++ b/tests/expectations/pin.c @@ -0,0 +1,27 @@ +#if 0 +''' ' +#endif + +#ifdef __cplusplus +template +using Pin = T; +template +using Box = T*; +#endif + +#if 0 +' ''' +#endif + + +#include +#include +#include +#include + +typedef struct { + int32_t *pinned_box; + int32_t *pinned_ref; +} PinTest; + +void root(int32_t *s, PinTest p); diff --git a/tests/expectations/pin.compat.c b/tests/expectations/pin.compat.c new file mode 100644 index 000000000..2cccd3e34 --- /dev/null +++ b/tests/expectations/pin.compat.c @@ -0,0 +1,35 @@ +#if 0 +''' ' +#endif + +#ifdef __cplusplus +template +using Pin = T; +template +using Box = T*; +#endif + +#if 0 +' ''' +#endif + + +#include +#include +#include +#include + +typedef struct { + int32_t *pinned_box; + int32_t *pinned_ref; +} PinTest; + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +void root(int32_t *s, PinTest p); + +#ifdef __cplusplus +} // extern "C" +#endif // __cplusplus diff --git a/tests/expectations/pin.cpp b/tests/expectations/pin.cpp new file mode 100644 index 000000000..8a5bc2c4b --- /dev/null +++ b/tests/expectations/pin.cpp @@ -0,0 +1,32 @@ +#if 0 +''' ' +#endif + +#ifdef __cplusplus +template +using Pin = T; +template +using Box = T*; +#endif + +#if 0 +' ''' +#endif + + +#include +#include +#include +#include +#include + +struct PinTest { + Pin> pinned_box; + Pin pinned_ref; +}; + +extern "C" { + +void root(Pin s, PinTest p); + +} // extern "C" diff --git a/tests/expectations/pin.pyx b/tests/expectations/pin.pyx new file mode 100644 index 000000000..f5336e606 --- /dev/null +++ b/tests/expectations/pin.pyx @@ -0,0 +1,29 @@ +#if 0 +''' ' +#endif + +#ifdef __cplusplus +template +using Pin = T; +template +using Box = T*; +#endif + +#if 0 +' ''' +#endif + + +from libc.stdint cimport int8_t, int16_t, int32_t, int64_t, intptr_t +from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t, uintptr_t +cdef extern from *: + ctypedef bint bool + ctypedef struct va_list + +cdef extern from *: + + ctypedef struct PinTest: + int32_t *pinned_box; + int32_t *pinned_ref; + + void root(int32_t *s, PinTest p); diff --git a/tests/expectations/pin.tag.c b/tests/expectations/pin.tag.c new file mode 100644 index 000000000..49fb9d849 --- /dev/null +++ b/tests/expectations/pin.tag.c @@ -0,0 +1,27 @@ +#if 0 +''' ' +#endif + +#ifdef __cplusplus +template +using Pin = T; +template +using Box = T*; +#endif + +#if 0 +' ''' +#endif + + +#include +#include +#include +#include + +struct PinTest { + int32_t *pinned_box; + int32_t *pinned_ref; +}; + +void root(int32_t *s, struct PinTest p); diff --git a/tests/expectations/pin.tag.compat.c b/tests/expectations/pin.tag.compat.c new file mode 100644 index 000000000..ecaae829d --- /dev/null +++ b/tests/expectations/pin.tag.compat.c @@ -0,0 +1,35 @@ +#if 0 +''' ' +#endif + +#ifdef __cplusplus +template +using Pin = T; +template +using Box = T*; +#endif + +#if 0 +' ''' +#endif + + +#include +#include +#include +#include + +struct PinTest { + int32_t *pinned_box; + int32_t *pinned_ref; +}; + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +void root(int32_t *s, struct PinTest p); + +#ifdef __cplusplus +} // extern "C" +#endif // __cplusplus diff --git a/tests/expectations/pin.tag.pyx b/tests/expectations/pin.tag.pyx new file mode 100644 index 000000000..04a60d73f --- /dev/null +++ b/tests/expectations/pin.tag.pyx @@ -0,0 +1,29 @@ +#if 0 +''' ' +#endif + +#ifdef __cplusplus +template +using Pin = T; +template +using Box = T*; +#endif + +#if 0 +' ''' +#endif + + +from libc.stdint cimport int8_t, int16_t, int32_t, int64_t, intptr_t +from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t, uintptr_t +cdef extern from *: + ctypedef bint bool + ctypedef struct va_list + +cdef extern from *: + + cdef struct PinTest: + int32_t *pinned_box; + int32_t *pinned_ref; + + void root(int32_t *s, PinTest p); diff --git a/tests/rust/pin.rs b/tests/rust/pin.rs new file mode 100644 index 000000000..c8781ae60 --- /dev/null +++ b/tests/rust/pin.rs @@ -0,0 +1,8 @@ +#[repr(C)] +struct PinTest { + pinned_box: Pin>, + pinned_ref: Pin<&mut i32> +} + +#[no_mangle] +pub extern "C" fn root(s: Pin<&mut i32>, p: PinTest) {} diff --git a/tests/rust/pin.toml b/tests/rust/pin.toml new file mode 100644 index 000000000..5aeccf9df --- /dev/null +++ b/tests/rust/pin.toml @@ -0,0 +1,21 @@ +header = """ +#if 0 +''' ' +#endif + +#ifdef __cplusplus +template +using Pin = T; +template +using Box = T*; +#endif + +#if 0 +' ''' +#endif +""" +[export] +exclude = [ + "Pin", + "Box" +]