#abi #auto-generate #safe #part #abis

foreignc

Auto generate ffi abis directly from rust code

3 releases

0.1.2 Dec 17, 2019
0.1.1 Dec 17, 2019
0.1.0 Dec 17, 2019

#273 in FFI

MIT/Apache

20KB
430 lines

Foreignc

Foreignc is a framework for auto generating a safe ffi abi for rust methods. The main advantage if foreignc is that allows for easy deplayment and maintenance of safe ffi abi. The crate is made up of two parts.

  • Macros for auto generating ffi abis and cleanup methods
  • Functions for auto generate the recieving end of the ffi abi
    • Currently only python is supported

Templating

Using the feature 'template' it is possible to auto generate the recieving side of the ffi. This will also add two new functions get_package_dir and get_parsed_dir. Both functions return a representation of the current ffi api

Default types

Primititve types

The following primitive types are supported:

  • bool
  • ()
  • i8, i16, i32, i64
  • u8, u16, u32, u64
  • f32, f64
  • &str (will be converted to a CString)

Other types

The following other types are soppurted:

  • Result (will be converted to a CResult)
  • Option (will be converted to a COption)
  • String (will be converted to a CString)

Custom Structs

Custom types can be implemented either by using the IntoFFi, FromFFi trait or the Boxed, Json macro.

Safety

As a rule of thumb, all allocated memory needs too be unallocated by the creator. This is also the basis for generate_free_methods that creates frunctions for freeing memory allocated by structures made by foreignc The following functions are made

  • free_string(ptr: *mut CString)
  • free_coption(ptr: *mut COption)
  • free_cresult(ptr: *mut CResult)

Boxed structs will auto generate a free method using the following convention free_{to_snake_case(struct name)}

For more information see the ![git repository]

Dependencies

~1.3–8MB
~80K SLoC