deltachat-core-rust icon indicating copy to clipboard operation
deltachat-core-rust copied to clipboard

Start playing with cbindgen

Open flub opened this issue 2 years ago • 2 comments

This is my attempt at getting a deltachat.h header automatically generated by cbindgen. I think the structs and functions are there already, but I'm a bit stuck at getting the constant values out of enums at the moment.

Some background to the choices so far, but nothing set in stone.

  • I decided to newtype every type in the deltachat-ffi crate. This gives us nicer typedef struct lines. Though the cost is that I've had to change how all those references are built from pointers.

    • The one exception is Provider, since this gives out 'static references, so I can't allocate the newtype anywhere.
    • Another reason for the newtypes is that I was trying to to let cbingen not look at crates other than deltachat-ffi. Though because of Provider that doesn't work anyway.
    • The newtype also avoids having to rename accounts::EventEmitter: cbindgen looks only at the names of the types and we have two EventEmitter names.
    • Overall I think newtyping is a good tradeoff here, despite the exception of Provider.
  • Somehow the function signatures are not using the typedefs. Users should still have the typedefs afailable though, and these should be the same fields.

  • I think generating the header in build.rs as well as committing it is the best tradeoff here. It makes sure that it is always up to date and also that we get diffs for the cffi API. (currently it is generated into bindings.h, that can be renamed once it is generated correctly)

  • I'm blindly making things pub for now just to get things working, the actual public API of deltachat's rust lib needs a lot of care anyway.

I'd be happy for any feedback, also if someone else is feels inspired and figures out how to add still missing things to the generated header please go ahead :)

flub avatar Apr 07 '22 19:04 flub

fyi I have recently started migrating my projects that use cbindgen to use https://docs.rs/safer-ffi/, it is considerably safer and has less surprising edges (and actually captures a lot of pieces cbindgen just "misses")

dignifiedquire avatar Apr 08 '22 14:04 dignifiedquire

  * The one exception is Provider, since this gives out `'static` references, so I can't allocate the newtype anywhere.

There's a dc_provider_unref (that's currently a no-op) so that's a bogus reason and this exception can go away.

flub avatar May 10 '22 10:05 flub

Given that talk is now rather about to gently introduce jsonrpc for interfacing with core with Android/iOS/Python bindings i think it's not worthwhile to revamp c-level bindings. Also @dignifiedquire mentioned https://docs.rs/safer-ffi/ as an alternative. Thus moved it to resurrection.

hpk42 avatar Dec 04 '22 19:12 hpk42

ftr, safer-ffi was discussed at https://github.com/deltachat/deltachat-core-rust/issues/3514

r10s avatar Dec 05 '22 16:12 r10s

Also ftr when it was first discussed safer-ffi did not have sufficient features for deltachat's FFI purposes.

On Mon 05 Dec 2022 at 08:30 -0800, bjoern wrote:

ftr, safer-ffi was discussed at https://github.com/deltachat/deltachat-core-rust/issues/3514

flub avatar Dec 08 '22 13:12 flub