#closures #cc #functional-programming #structures #cross #compatible #boundary

c-closures-build

Generates C/C++ FFI compatible *Closure structures from rust closures, useful for cross language functional programming

10 releases (4 major breaking)

4.0.0 Nov 2, 2020
3.0.1 Jul 1, 2020
2.0.1 Jul 1, 2020
2.0.0 May 29, 2020
0.2.0 May 4, 2020

#798 in Procedural macros

MIT/Apache

17KB
258 lines

Purpose

This crate is for producing Rust closures that can cross an FFI boundary. It provides support for any function signature, assuming all of the types in it have valid representations in C/C++ and Rust.

Here's an example.

Safety concerns

Creating a *Closure by itself can not cause undefined behavior, however when the resulting structure is used in C/C++ it can still trigger undefined behavior. *Closure should never be an argument to a safe function, nor should it be a public member of any structures passed into a safe function. Please write your own safe wrappers that incorporate the *Closure types internally.

Usage in C/C++

To use this with a C/C++ library you'll need to include the header provided in the repo, rust_closures.h. Then you can accept the relevant *Closure type anywhere that you need to accept arbitrary Rust code.

Limitations

This cannot be used to transfer ownership of allocated memory across FFI boundaries, as this crate cannot reasonably guarantee both sides are using the same memory allocator, or dispose of the types in the same way. If such transfer is required, you should copy the data into a new allocation, on the side of the FFI boundary it needs to live on. The major exception to this is types with the Copy marker trait, which are trivially cloned and require no disposal instructions.

Dependencies

~1.5MB
~37K SLoC