Skip to content

Commit

Permalink
Update tests & READMEs.
Browse files Browse the repository at this point in the history
  • Loading branch information
ralfbiedert committed Oct 5, 2024
1 parent 19ed3ff commit 64af68b
Show file tree
Hide file tree
Showing 11 changed files with 400 additions and 269 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ If you want to ...
- **understand what's possible**, see the [**reference project**](https://github.com/ralfbiedert/interoptopus/tree/master/reference_project/src),
- **support a new language**, [**copy the C backend**](https://github.com/ralfbiedert/interoptopus/tree/master/backends/c).

For a "production-ready" project you generally want to follow these 3 steps:

- Write a Rust crate, such as `my_library` and solve your problem in a Rust-idiomatic way.
- Add a crate `my_library_ffi`, which holds interop types and C-style definitions for your library similar to the examples above.
- For example, for a `my_library::Container` you probably want to a FFI sibling `my_library_ffi::Container`.
- While this might seem like extra work (it is) it gives you fine-grained access to what you expose. Once your project reaches a certain size, this will be invaluable.
- You'll probably want to add some conversion methods between your types, and FFI exposed methods to allow your interop users
to interact with these types in a controlled fashion.
- Add a crate `my_library_interop`. Inside there reference the backends for which you want to create interop definitions
(either in some unit tests, a `main.rs` or `build.rs`), and invoke the interop generation when needed; again, compare examples above.


### Supported Rust Constructs

Expand Down Expand Up @@ -125,6 +136,7 @@ Gated behind **feature flags**, these enable:

### Changelog

- **v0.15** - Updated to syn2, bug fixes.
- **v0.14** - Better inventory UX.
- **v0.13** - Python backend uses `ctypes` now.
- **v0.12** - Better compat using `#[ffi_service_method]`.
Expand Down
35 changes: 20 additions & 15 deletions backends/c/tests/output_docs_inline/my_header.h.expected
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ typedef struct my_library_generic3 my_library_generic3;

typedef struct my_library_generic4 my_library_generic4;

typedef struct my_library_opaque my_library_opaque;

/// Some struct we want to expose as a class.
typedef struct my_library_simple_service my_library_simple_service;

Expand Down Expand Up @@ -97,6 +95,11 @@ typedef struct my_library_inner
float x;
} my_library_inner;

typedef struct my_library_local
{
uint32_t x;
} my_library_local;

#pragma pack(push, 1)
typedef struct my_library_packed1
{
Expand All @@ -118,11 +121,6 @@ typedef struct my_library_phantomu8
uint32_t x;
} my_library_phantomu8;

typedef struct my_library_some_foreign_type
{
uint32_t x;
} my_library_some_foreign_type;

/// Documented struct.
typedef struct my_library_struct_documented
{
Expand Down Expand Up @@ -200,6 +198,11 @@ typedef struct my_library_array
uint8_t data[16];
} my_library_array;

typedef struct my_library_container
{
my_library_local foreign;
} my_library_container;

typedef struct my_library_genericu32
{
const uint32_t* x;
Expand Down Expand Up @@ -388,8 +391,6 @@ my_library_tupled tupled(my_library_tupled x);

my_library_ffi_error complex_args_1(my_library_vec3f32 a, const my_library_tupled* b);

const my_library_opaque* complex_args_2(my_library_some_foreign_type cmplx);

uint8_t callback(my_library_fptr_fn_u8_rval_u8 callback, uint8_t value);

uint32_t generic_1a(my_library_genericu32 x, my_library_phantomu8 y);
Expand Down Expand Up @@ -483,6 +484,8 @@ void pattern_callback_3(my_library_delegate_callback_my_callback_contextual call

uint32_t pattern_callback_4(my_library_my_callback_namespaced callback, uint32_t x);

void pattern_surrogates_1(my_library_local s, my_library_container* c);

/// Destroys the given instance.
///
/// # Safety
Expand Down Expand Up @@ -511,6 +514,8 @@ uint32_t simple_service_method_value(const my_library_simple_service* context, u
/// Multiple lines.
void simple_service_method_void(const my_library_simple_service* context);

void simple_service_method_void2(const my_library_simple_service* context);

uint8_t simple_service_method_mut_self(my_library_simple_service* context, my_library_sliceu8 slice);

/// Single line.
Expand Down Expand Up @@ -547,17 +552,17 @@ my_library_ffi_error simple_service_method_callback(my_library_simple_service* c
///
/// The passed parameter MUST have been created with the corresponding init function;
/// passing any other value results in undefined behavior.
my_library_ffi_error simple_service_lt_destroy(my_library_simple_service_lifetime** context);
my_library_ffi_error simple_service_lifetime_destroy(my_library_simple_service_lifetime** context);

my_library_ffi_error simple_service_lt_new_with(my_library_simple_service_lifetime** context, const uint32_t* some_value);
my_library_ffi_error simple_service_lifetime_new_with(my_library_simple_service_lifetime** context, const uint32_t* some_value);

void simple_service_lt_method_lt(my_library_simple_service_lifetime* context, my_library_slice_bool slice);
void simple_service_lifetime_method_lt(my_library_simple_service_lifetime* context, my_library_slice_bool slice);

void simple_service_lt_method_lt2(my_library_simple_service_lifetime* context, my_library_slice_bool slice);
void simple_service_lifetime_method_lt2(my_library_simple_service_lifetime* context, my_library_slice_bool slice);

const char* simple_service_lt_return_string_accept_slice(my_library_simple_service_lifetime* anon0, my_library_sliceu8 anon1);
const char* simple_service_lifetime_return_string_accept_slice(my_library_simple_service_lifetime* anon0, my_library_sliceu8 anon1);

my_library_ffi_error simple_service_lt_method_void_ffi_error(my_library_simple_service_lifetime* context);
my_library_ffi_error simple_service_lifetime_method_void_ffi_error(my_library_simple_service_lifetime* context);


#ifdef __cplusplus
Expand Down
31 changes: 18 additions & 13 deletions backends/c/tests/output_nodocs/my_header.h.expected
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ typedef enum my_library_enumrenamed
typedef struct my_library_generic2u8 my_library_generic2u8;
typedef struct my_library_generic3 my_library_generic3;
typedef struct my_library_generic4 my_library_generic4;
typedef struct my_library_opaque my_library_opaque;
typedef struct my_library_simpleservice my_library_simpleservice;
typedef struct my_library_simpleservicelifetime my_library_simpleservicelifetime;
typedef enum my_library_ffierror
Expand Down Expand Up @@ -86,6 +85,11 @@ typedef struct my_library_inner
float x;
} my_library_inner;

typedef struct my_library_local
{
uint32_t x;
} my_library_local;

#pragma pack(push, 1)
typedef struct my_library_packed1
{
Expand All @@ -107,11 +111,6 @@ typedef struct my_library_phantomu8
uint32_t x;
} my_library_phantomu8;

typedef struct my_library_someforeigntype
{
uint32_t x;
} my_library_someforeigntype;

typedef struct my_library_structdocumented
{
float x;
Expand Down Expand Up @@ -187,6 +186,11 @@ typedef struct my_library_array
uint8_t data[16];
} my_library_array;

typedef struct my_library_container
{
my_library_local foreign;
} my_library_container;

typedef struct my_library_genericu32
{
const uint32_t* x;
Expand Down Expand Up @@ -313,7 +317,6 @@ bool ref_option(const int64_t* x);
bool ref_mut_option(int64_t* x);
my_library_tupled tupled(my_library_tupled x);
my_library_ffierror complex_args_1(my_library_vec3f32 a, const my_library_tupled* b);
const my_library_opaque* complex_args_2(my_library_someforeigntype cmplx);
uint8_t callback(my_library_fptr_fn_u8_rval_u8 callback, uint8_t value);
uint32_t generic_1a(my_library_genericu32 x, my_library_phantomu8 y);
uint8_t generic_1b(my_library_genericu8 x, my_library_phantomu8 y);
Expand Down Expand Up @@ -360,6 +363,7 @@ uint32_t pattern_callback_1(my_library_mycallback callback, uint32_t x);
my_library_mycallbackvoid pattern_callback_2(my_library_mycallbackvoid callback);
void pattern_callback_3(my_library_delegatecallbackmycallbackcontextual callback, uint32_t x);
uint32_t pattern_callback_4(my_library_mycallbacknamespaced callback, uint32_t x);
void pattern_surrogates_1(my_library_local s, my_library_container* c);
my_library_ffierror simple_service_destroy(my_library_simpleservice** context);
my_library_ffierror simple_service_new_with(my_library_simpleservice** context, uint32_t some_value);
my_library_ffierror simple_service_new_without(my_library_simpleservice** context);
Expand All @@ -368,6 +372,7 @@ my_library_ffierror simple_service_new_failing(my_library_simpleservice** contex
my_library_ffierror simple_service_method_result(const my_library_simpleservice* context, uint32_t anon1);
uint32_t simple_service_method_value(const my_library_simpleservice* context, uint32_t x);
void simple_service_method_void(const my_library_simpleservice* context);
void simple_service_method_void2(const my_library_simpleservice* context);
uint8_t simple_service_method_mut_self(my_library_simpleservice* context, my_library_sliceu8 slice);
void simple_service_method_mut_self_void(my_library_simpleservice* context, my_library_slicebool slice);
uint8_t simple_service_method_mut_self_ref(my_library_simpleservice* context, const uint8_t* x, uint8_t* y);
Expand All @@ -380,12 +385,12 @@ my_library_slicemutu32 simple_service_return_slice_mut(my_library_simpleservice*
const char* simple_service_return_string(my_library_simpleservice* context);
my_library_ffierror simple_service_method_void_ffi_error(my_library_simpleservice* context);
my_library_ffierror simple_service_method_callback(my_library_simpleservice* context, my_library_mycallback callback);
my_library_ffierror simple_service_lt_destroy(my_library_simpleservicelifetime** context);
my_library_ffierror simple_service_lt_new_with(my_library_simpleservicelifetime** context, const uint32_t* some_value);
void simple_service_lt_method_lt(my_library_simpleservicelifetime* context, my_library_slicebool slice);
void simple_service_lt_method_lt2(my_library_simpleservicelifetime* context, my_library_slicebool slice);
const char* simple_service_lt_return_string_accept_slice(my_library_simpleservicelifetime* anon0, my_library_sliceu8 anon1);
my_library_ffierror simple_service_lt_method_void_ffi_error(my_library_simpleservicelifetime* context);
my_library_ffierror simple_service_lifetime_destroy(my_library_simpleservicelifetime** context);
my_library_ffierror simple_service_lifetime_new_with(my_library_simpleservicelifetime** context, const uint32_t* some_value);
void simple_service_lifetime_method_lt(my_library_simpleservicelifetime* context, my_library_slicebool slice);
void simple_service_lifetime_method_lt2(my_library_simpleservicelifetime* context, my_library_slicebool slice);
const char* simple_service_lifetime_return_string_accept_slice(my_library_simpleservicelifetime* anon0, my_library_sliceu8 anon1);
my_library_ffierror simple_service_lifetime_method_void_ffi_error(my_library_simpleservicelifetime* context);

#ifdef __cplusplus
}
Expand Down
35 changes: 20 additions & 15 deletions backends/c/tests/output_typedefs/my_header.h.expected
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ typedef struct my_library_generic3 my_library_generic3;

typedef struct my_library_generic4 my_library_generic4;

typedef struct my_library_opaque my_library_opaque;

/// Some struct we want to expose as a class.
typedef struct my_library_simple_service my_library_simple_service;

Expand Down Expand Up @@ -97,6 +95,11 @@ typedef struct my_library_inner
float x;
} my_library_inner;

typedef struct my_library_local
{
uint32_t x;
} my_library_local;

#pragma pack(push, 1)
typedef struct my_library_packed1
{
Expand All @@ -118,11 +121,6 @@ typedef struct my_library_phantomu8
uint32_t x;
} my_library_phantomu8;

typedef struct my_library_some_foreign_type
{
uint32_t x;
} my_library_some_foreign_type;

/// Documented struct.
typedef struct my_library_struct_documented
{
Expand Down Expand Up @@ -200,6 +198,11 @@ typedef struct my_library_array
uint8_t data[16];
} my_library_array;

typedef struct my_library_container
{
my_library_local foreign;
} my_library_container;

typedef struct my_library_genericu32
{
const uint32_t* x;
Expand Down Expand Up @@ -388,8 +391,6 @@ typedef my_library_tupled (*tupled)(my_library_tupled);

typedef my_library_ffi_error (*complex_args_1)(my_library_vec3f32, const my_library_tupled*);

typedef const my_library_opaque* (*complex_args_2)(my_library_some_foreign_type);

typedef uint8_t (*callback)(my_library_fptr_fn_u8_rval_u8, uint8_t);

typedef uint32_t (*generic_1a)(my_library_genericu32, my_library_phantomu8);
Expand Down Expand Up @@ -483,6 +484,8 @@ typedef void (*pattern_callback_3)(my_library_delegate_callback_my_callback_cont

typedef uint32_t (*pattern_callback_4)(my_library_my_callback_namespaced, uint32_t);

typedef void (*pattern_surrogates_1)(my_library_local, my_library_container*);

/// Destroys the given instance.
///
/// # Safety
Expand Down Expand Up @@ -511,6 +514,8 @@ typedef uint32_t (*simple_service_method_value)(const my_library_simple_service*
/// Multiple lines.
typedef void (*simple_service_method_void)(const my_library_simple_service*);

typedef void (*simple_service_method_void2)(const my_library_simple_service*);

typedef uint8_t (*simple_service_method_mut_self)(my_library_simple_service*, my_library_sliceu8);

/// Single line.
Expand Down Expand Up @@ -547,17 +552,17 @@ typedef my_library_ffi_error (*simple_service_method_callback)(my_library_simple
///
/// The passed parameter MUST have been created with the corresponding init function;
/// passing any other value results in undefined behavior.
typedef my_library_ffi_error (*simple_service_lt_destroy)(my_library_simple_service_lifetime**);
typedef my_library_ffi_error (*simple_service_lifetime_destroy)(my_library_simple_service_lifetime**);

typedef my_library_ffi_error (*simple_service_lt_new_with)(my_library_simple_service_lifetime**, const uint32_t*);
typedef my_library_ffi_error (*simple_service_lifetime_new_with)(my_library_simple_service_lifetime**, const uint32_t*);

typedef void (*simple_service_lt_method_lt)(my_library_simple_service_lifetime*, my_library_slice_bool);
typedef void (*simple_service_lifetime_method_lt)(my_library_simple_service_lifetime*, my_library_slice_bool);

typedef void (*simple_service_lt_method_lt2)(my_library_simple_service_lifetime*, my_library_slice_bool);
typedef void (*simple_service_lifetime_method_lt2)(my_library_simple_service_lifetime*, my_library_slice_bool);

typedef const char* (*simple_service_lt_return_string_accept_slice)(my_library_simple_service_lifetime*, my_library_sliceu8);
typedef const char* (*simple_service_lifetime_return_string_accept_slice)(my_library_simple_service_lifetime*, my_library_sliceu8);

typedef my_library_ffi_error (*simple_service_lt_method_void_ffi_error)(my_library_simple_service_lifetime*);
typedef my_library_ffi_error (*simple_service_lifetime_method_void_ffi_error)(my_library_simple_service_lifetime*);


#ifdef __cplusplus
Expand Down
Loading

0 comments on commit 64af68b

Please sign in to comment.