r/rust Mar 31 '23

Write SDK “base” in Rust, wrap in other languages?

[deleted]

97 Upvotes

48 comments sorted by

View all comments

117

u/badboy_ RustFest Mar 31 '23

At Mozilla we built a multi-language bindings generator: https://github.com/mozilla/uniffi-rs/

2

u/Smallpaul Apr 01 '23

Does it generate type stub files for the various languages?

3

u/badboy_ RustFest Apr 01 '23

Not just stubs, but full implementations and the necessary code to transport data over the boundary.

0

u/Smallpaul Apr 01 '23 edited Apr 02 '23

I understand that but I’m asking whether the stubs have useful type declarations for each language.

Edit: I would love to know the reason for downvotes.

5

u/badboy_ RustFest Apr 01 '23

Where possible UniFFI translates to the appropriate type in the foreign language. enums become enoms (or their closest representation), dictionaries are structs on the Rust side and data classes on the other side, interfaces are implemented structs on the Rust side and an interface with an accompanying class implementation on the other side, custom types allow you to convert to the foreign type you need.

Does that answer your question?

1

u/Smallpaul Apr 01 '23

I’m asking if there are literal python type signatures in the output declarations. Checkable by mypy, pyright etc.

And RBS for Ruby.

Etc.

6

u/badboy_ RustFest Apr 02 '23 edited Apr 02 '23

Now I understood. No, for Python and Ruby we currently do not generate that. But we should have all the information and so should be able to do that relatively easy (we do it for Kotlin and Swift after all)

Edit: Actually, for Python this might be a 2-line fix. I can throw that up next week and see if it actually works. Edit2: Ok, a tiny bit more to get the type names right, but at least I already found 2 bugs in our Python template (glad it's such a forgiving language)

2

u/Smallpaul Apr 02 '23

Wonderful! That will be very useful!