casbin-rs
casbin-rs copied to clipboard
add c ffi so people can call casbin-rs from c code
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.
A simple link demo
I think we should continue from this PR and make it possible to call rust from c/c++, it's a great idea!
I have a question. If this idea (PR) works, do we still need to develop: https://github.com/casbin/casbin-cpp ?
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 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.
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.
Any update?
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.