Skip to content

Commit

Permalink
Merge pull request #111 from getditto/support-mut-params-in-ffi-export
Browse files Browse the repository at this point in the history
Support `mut` parameters on `#[ffi_export]`ed functions
  • Loading branch information
danielhenrymantilla authored May 20, 2022
2 parents 83dc777 + 3389783 commit d0ba971
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 45 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/gh-pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- macos-latest
- windows-latest
rust:
- 1.43.0
- 1.56.0
- stable
- nightly
steps:
Expand Down Expand Up @@ -61,7 +61,7 @@ jobs:
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: 1.43.0
toolchain: 1.56.0
override: true

- name: Clone repo
Expand Down
10 changes: 8 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ path = "src/_lib.rs"
[package]
name = "safer-ffi"
# Keep in sync with `[dependencies.safer_ffi-proc_macro]` and `src/proc_macro/Cargo.toml`
version = "0.0.9"
version = "0.0.10"
authors = ["Daniel Henry-Mantilla <[email protected]>"]
edition = "2018"

Expand Down Expand Up @@ -48,6 +48,8 @@ headers = [

[dev-dependencies]
macro_rules_attribute = "0.0.1"
safer-ffi.path = "."
safer-ffi.features = ["proc_macros", "headers"]

[target.'cfg(not(target = "wasm32-unknown-unknown"))'.dependencies]
libc = { version = "0.2.66", default-features = false }
Expand All @@ -61,7 +63,7 @@ proc-macro-hack = { version = "0.5.15", optional = true }

[dependencies.safer_ffi-proc_macro]
path = "src/proc_macro"
version = "0.0.9"
version = "0.0.10"

[dependencies.uninit]
optional = true
Expand All @@ -76,3 +78,7 @@ members = ["src/proc_macro"]
[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docs"]

[[test]]
name = "ffi-tests"
path = "ffi_tests/src/lib.rs"
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ https://github.com/getditto/safer_ffi)

## Prerequisites

Minimum Supported Rust Version: `1.43.0`
Minimum Supported Rust Version: `1.56.0`

## Quickstart

Expand Down Expand Up @@ -151,7 +151,7 @@ Point_t mid_point (
Point_t const * a,
Point_t const * b);

typedef enum Figure
typedef enum Figure
{
FIGURE_CIRCLE,
FIGURE_SQUARE
Expand Down
46 changes: 23 additions & 23 deletions ffi_tests/generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,27 @@ int32_t read_foo (
void free_foo (
foo_t * foo);

/** \remark Has the same ABI as `uint8_t` **/
#ifdef DOXYGEN
typedef enum Bar
#else
typedef uint8_t Bar_t; enum
#endif
{
/** . */
BAR_A,
}
#ifdef DOXYGEN
Bar_t
#endif
;

void check_bar (
Bar_t _bar);

/** \brief
* Concatenate the two input strings into a new one.
*
*
* The returned string must be freed using `free_char_p`.
*/
char * concat (
Expand Down Expand Up @@ -62,19 +80,19 @@ void with_concat (

/** \brief
* `&'lt [T]` but with a guaranteed `#[repr(C)]` layout.
*
*
* # C layout (for some given type T)
*
*
* ```c
* typedef struct {
* // Cannot be NULL
* T * ptr;
* size_t len;
* } slice_T;
* ```
*
*
* # Nullable pointer?
*
*
* If you want to support the above typedef, but where the `ptr` field is
* allowed to be `NULL` (with the contents of `len` then being undefined)
* use the `Option< slice_ptr<_> >` type.
Expand Down Expand Up @@ -136,24 +154,6 @@ typedef struct AnUnusedStruct {

} AnUnusedStruct_t;

/** \remark Has the same ABI as `uint8_t` **/
#ifdef DOXYGEN
typedef enum Bar
#else
typedef uint8_t Bar_t; enum
#endif
{
/** . */
BAR_A,
}
#ifdef DOXYGEN
Bar_t
#endif
;

void check_bar (
Bar_t _bar);


#ifdef __cplusplus
} /* extern "C" */
Expand Down
3 changes: 1 addition & 2 deletions ffi_tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ fn free_char_p (_string: Option<char_p::Box>)
fn with_concat (
fst: char_p::Ref<'_>,
snd: char_p::Ref<'_>,
/*mut*/ cb: RefDynFnMut1<'_, (), char_p::Raw>,
mut cb: RefDynFnMut1<'_, (), char_p::Raw>,
)
{
let mut cb = cb;
let concat = concat(fst, snd);
cb.call(concat.as_ref().into());
}
Expand Down
2 changes: 1 addition & 1 deletion guide/src/introduction/_.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Link to [<i class="fa fa-cubes" aria-hidden="true"></i> the `rustdoc`-generated

## Prerequisites

- Minimum Supported Rust Version: `1.43.0`
- Minimum Supported Rust Version: `1.56.0`

## Getting started

Expand Down
2 changes: 1 addition & 1 deletion guide/src/prerequisites.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Prerequisites

- Minimum Supported Rust Version: `1.43.0`
- Minimum Supported Rust Version: `1.56.0`
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.43.0
1.56.0
42 changes: 32 additions & 10 deletions src/ffi_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ macro_rules! __ffi_export__ {
$(extern $("C")?)?
fn $fname:ident $(<$($lt:lifetime $(: $sup_lt:lifetime)?),* $(,)?>)? (
$(
$arg_name:ident : $arg_ty:ty
$($arg_name:ident)+ : $arg_ty:ty
),* $(,)?
) $(-> $Ret:ty)?
$( where {
Expand All @@ -59,7 +59,7 @@ macro_rules! __ffi_export__ {
extern "C"
fn $fname $(<$($lt $(: $sup_lt)?),*>)? (
$(
$arg_name : $arg_ty,
$($arg_name)+ : $arg_ty,
)*
) $(-> $Ret)?
$(
Expand All @@ -77,7 +77,7 @@ macro_rules! __ffi_export__ {
extern "C"
fn $fname $(<$($lt $(: $sup_lt)?),*>)? (
$(
$arg_name : <$arg_ty as $crate::layout::ReprC>::CLayout,
$($arg_name)+ : <$arg_ty as $crate::layout::ReprC>::CLayout,
)*
) $(-> $Ret)?
where
Expand All @@ -100,7 +100,7 @@ macro_rules! __ffi_export__ {
let _: <$Ret as $crate::layout::ReprC>::CLayout;
)?
$(
{
$crate::__last__! { $(({
mod __parameter__ {
pub(in super)
fn $arg_name<T> (_: T)
Expand All @@ -114,11 +114,11 @@ macro_rules! __ffi_export__ {
{}
}
let _ = __parameter__::$arg_name::<$arg_ty>;
}
}))+ }
#[allow(unused_unsafe)]
let $arg_name: $arg_ty = unsafe {
let $($arg_name)+: $arg_ty = unsafe {
$crate::layout::from_raw_unchecked::<$arg_ty>(
$arg_name,
$crate::__last__! { $(( $arg_name ))+ },
)
};
)*
Expand Down Expand Up @@ -223,9 +223,10 @@ macro_rules! __ffi_export__ {
<$arg_ty as $crate::layout::ReprC>::CLayout
as
$crate::layout::CType
>::c_var({
let it = stringify!($arg_name);
if it == "_" { "" } else { it }
>::c_var($crate::__last__! {
$((
stringify!($arg_name)
))+
})
,
)?;
Expand Down Expand Up @@ -270,3 +271,24 @@ macro_rules! __ffi_export__ {
// ints.as_slice().iter().max()
// }
// }

#[doc(hidden)] #[macro_export]
macro_rules! __last__ {
(
$not_last:tt
$($rest:tt)+
) => (
$crate::__last__! {
$($rest)*
}
);

(
// last
(
$($contents:tt)*
)
) => (
$($contents)*
);
}
2 changes: 1 addition & 1 deletion src/proc_macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ proc-macro = true
[package]
name = "safer_ffi-proc_macro"
# Keep in sync with `/Cargo.toml`
version = "0.0.9"
version = "0.0.10"
authors = ["Daniel Henry-Mantilla <[email protected]>"]
edition = "2018"

Expand Down

0 comments on commit d0ba971

Please sign in to comment.