casbin-rs icon indicating copy to clipboard operation
casbin-rs copied to clipboard

add c ffi so people can call casbin-rs from c code

Open xcaptain opened this issue 4 years ago • 8 comments

Writing a pure C version casbin is quite difficult, but using rust ffi can be very easy. This commit adds C-bindings, so C/C++ programmer can easily integrate casbin into their projects.

xcaptain avatar Mar 20 '20 09:03 xcaptain

A simple link demo

图片

xcaptain avatar Mar 20 '20 10:03 xcaptain

I think we should continue from this PR and make it possible to call rust from c/c++, it's a great idea!

0x8f701 avatar Mar 20 '20 13:03 0x8f701

I have a question. If this idea (PR) works, do we still need to develop: https://github.com/casbin/casbin-cpp ?

hsluoyz avatar Mar 20 '20 16:03 hsluoyz

I bet it could be done, this was a really good idea @xcaptain!! Here's a few links I found while looking around cbindgen more helpful info

All of the C binding stuff could be in a new crate that imports the nessesary stuff from casbin-rs and with a build.rs file generates cpp files. According to the second link it is possible using the cpp crate to take the cdylib and generate cpp bindings.

DevinR528 avatar Mar 20 '20 19:03 DevinR528

@DevinR528 I did some experiments with cbindgen but sadly it doesn't work, because we have used

Box<dyn Adapter>
// or
*mut dyn Adapter

According to cbindgen's doc

cbindgen also cannot support wide pointers like &dyn Trait or &[T], as their layout and ABI is not guaranteed. In the case of slices you can at least decompose them into a pointer and length, and reconstruct them with slice::from_raw_parts.

but a hand written

pub extern "C" fn new_adapter(dsn: *const c_char) -> Box<dyn Adapter>

can be converted to

extern struct Adapter *new_adapter(char *dsn);

It's a little tricky but we were lucky it worked.

I also tried FFI diesel-adapter and again we were lucky, it worked.

Later I will do some experiments on multi-thread scenarios to see if rust's RWLock works in C code, if every thing goes well then I will start porting more APIs to ffi/c.rs.

As for the cpp crate, it used to embed C++ code into rust project, seems no relation with this pull request. I don't think a C++ binding is needed, because a C binding is enough, also write a well structured C++ binding may be hard.

xcaptain avatar Mar 28 '20 18:03 xcaptain

Hello everyone, this will be a fruitful feature for rust users where they do not require a authorization engine as its C/C++ client project's main requirement but as a RFC(Remote function call) interface like requirement where they do not need to use the whole heavy-weight set-up for C++. Obviously this engine won't be as fast as the main C/C++ library but can be easy to plugin in projects.

divy9881 avatar Jul 02 '20 07:07 divy9881

Any update?

hsluoyz avatar Oct 31 '20 01:10 hsluoyz

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

CLAassistant avatar Dec 21 '21 16:12 CLAassistant