Rust library exports C API. How to -sys?

Hi,

I have a Rust crate https://github.com/hyperledger/indy-sdk/tree/master/libindy that builds a library and has a C API. The result is e.g. target/release/libindy.so
Also there are hand written (mostly) C header files in https://github.com/hyperledger/indy-sdk/tree/master/libindy/include

Now I have other crates that use this library and they use a local indy-sys crate for this.

My question is: The definitions of what is exported from the library are manually created in indy-sys
e.g.: https://github.com/hyperledger/indy-sdk/blob/master/wrappers/rust/indy-sys/src/pairwise.rs#L14


    #[no_mangle]
    pub fn indy_is_pairwise_exists(command_handle: Handle,
                                   wallet_handle: Handle,
                                   their_did: CString,
                                   cb: Option<ResponseBoolCB>) -> Error;

    #[no_mangle]
    pub fn indy_create_pairwise(command_handle: Handle,
                                wallet_handle: Handle,
                                their_did: CString,
                                my_did: CString,
                                metadata: CString,
                                cb: Option<ResponseEmptyCB>) -> Error;
extern {

    #[no_mangle]
    pub fn indy_is_pairwise_exists(command_handle: Handle,
                                   wallet_handle: Handle,
                                   their_did: CString,
                                   cb: Option<ResponseBoolCB>) -> Error;

    #[no_mangle]
    pub fn indy_create_pairwise(command_handle: Handle,
                                wallet_handle: Handle,
                                their_did: CString,
                                my_did: CString,
                                metadata: CString,
                                cb: Option<ResponseEmptyCB>) -> Error;

This seems somewhat cumbersome because the "real" definition is not far away:
https://github.com/hyperledger/indy-sdk/blob/master/libindy/src/api/pairwise.rs#L75

So this is the rare case of a -sys that is based not on C code but on Rust code.
How do I get from that libindy Rust code to something indy-sys can use?

Axel

creating the C-Binding using cbindgen and then using bindgen to get the Rust bindings seem awkward.
Also cbindgen creates C code that bindgen does not like e.g. using #include <cstdarg> and using wchar without including wchar.h static const wchar_t DELIMITER = :; seems to be missing ' around the colon, too.

1 Like