UniFFI - automatically generate foreign-language bindings targeting Rust libraries (mozilla.github.io)
from lysdexic@programming.dev to rust@programming.dev on 29 Aug 2023 07:36
https://programming.dev/post/2288300

#rust

threaded - newest

0xDEADBEEFCAFE@programming.dev on 29 Aug 2023 08:27 next collapse

How is the quality of the generated bindings?

I have previously used PyO3 for Python bindings and it doesn’t seem to correctly generate type hints.

Walnut356@programming.dev on 30 Aug 2023 00:41 collapse

Pyo3 doesnt generate type hints at all iirc. There’s some more info here

The gist, as i recall, is that you’re supposed to maintain a .pyi file in the rust project folder that gets automatically added in by maturin when building. I’m no regex wizard, but i was able to whip together a small script that generates simple function and class stubs that also have the rust docstring. Iirc they’re planning on adding some official support for generating type hints, but i have no idea how far along it is or where it is on the priority list

0xDEADBEEFCAFE@programming.dev on 30 Aug 2023 09:53 collapse

That’s a bummer. I guess what I though was PyO3 type hints was actually just PyCharm guessing at the types.

I tried creating a .pyi file and it seems to be a pretty big improvement over no type hints, but I don’t know how to add type hints for modules added with PyModule::add_module and adding the .pyi file seems to make the type checker not know about my other modules.

Would you mind sharing the script for auto generating type hints? Keeping them in sync manually would be pretty annoying.

Walnut356@programming.dev on 01 Sep 2023 04:50 collapse

Here’s the script. It’s nothing fancy, and iirc it only works for top level functions/classes. That means you still have to take care of attributes and methods which is a little annoying, but for simple stuff it should save a bit of time.

Vorpal@programming.dev on 30 Aug 2023 19:31 collapse

Seems rather limited: only targeting some high level languages. Now, if this could also generate C++ bindings i would be very interested.

Knusper@feddit.de on 31 Aug 2023 08:12 collapse

Hmm, but Rust has native generation of C bindings. Is there a big difference with C+±specific bindings?

snaggen@programming.dev on 31 Aug 2023 08:23 collapse

If you refer to bindgen it also supports C++, and since Mozilla who was the main driver of rust uses C++ I assume the bindgen for that is pretty good.

Knusper@feddit.de on 31 Aug 2023 10:14 collapse

Ah, thanks. I did not know that, but I figured, if C+±specific bindings make sense, then Mozilla would have built something for that, before working on UniFFI.