nitro icon indicating copy to clipboard operation
nitro copied to clipboard

[Feature] Support for Rust language in nitrogen

Open pzoltowski opened this issue 1 year ago • 12 comments

FWIW, came across this repo: https://github.com/getditto/rn-jsi-rust-bridging and accompanying article: https://ditto.live/blog/bridging-react-native-and-rust-via-jsi and some other of their related repos: https://github.com/getditto/react-ditto https://github.com/getditto/react-native-sample-app

where they are bridging react native to rust (for both old and new architecture). I know almost nothing about rust (so cannot help) but curious if nitro could simplify the process for such react native - rust bindings?

It would probably open a new world of possibilities since there is plenty of truely multiplatoform (mobile include) libraries in rust land and rust cargo is a godsend comparing to cmake - there is a lot of c++ libraries that officially don't support mobile but mostly because of build issues.

In rust there is plenty of nice gems that have functionality missing in react native (or are not as good):

opencv-rust - [autogenerated opencv + opencv_contrib bindings] https://github.com/twistedfall/opencv-rust Cv-convert - [conversion between cv and different image libs ndarray] https://crates.io/crates/cv-convert cpal - [realtime audio i/o processing] https://github.com/RustAudio/cpal midir - [midi i/o] https://github.com/Boddlnagg/midir wmidi or rimd - [midi encoding / decoding] Peertalk-rst [ios - usb communication] https://github.com/AstroHQ/peertalk-rs rust-sdl2 - [gamecontrollers, IMU sensors, audio, etc] https://github.com/Rust-SDL2/rust-sdl2 rodio - [audio player] https://github.com/RustAudio/rodio

Maybe worth to contact those guys from ditto.live to invite for collaboration? (they might not know about Nitro), they seem have big clients in Aviation industry so they probably know what they are doing.

pzoltowski avatar Oct 25 '24 18:10 pzoltowski

Ha, it's funny how much of my code is in that repo/article. From the JSI integration, to even the error messages.

Anyways - yea this article isn't very relevant as of right now because it mostly showcases the JSI integration part, and then exposing a simple Rust function as an external C function. We use C++, and classes and inheritance, this would not be supported by Rust.

So if you know how to use Rust, it'd be quite easy to just expose your Rust functions to C++, and then call them in Nitro.

This also goes for all of the libraries you shared - you can expose them as C decls and call them from C++.

mrousavy avatar Oct 29 '24 08:10 mrousavy

@pzoltowski this is not exactly what you're asking, but you might find this interesting: https://hacks.mozilla.org/2024/12/introducing-uniffi-for-react-native-rust-powered-turbo-modules/

kraenhansen avatar Dec 06 '24 19:12 kraenhansen

Xactly. bump for this - wanna hear about uniffi x nitro - a dream, or nightmare ?

elix1er avatar Mar 03 '25 05:03 elix1er

https://github.com/niteshbalusu11/react-native-nitro-tor

https://github.com/niteshbalusu11/tor-rust-sdk

Here's my nitro modules library where most of the code that runs the Tor daemon is written in Rust, and the methods are exposed and called from C++ in nitro modules.

niteshbalusu11 avatar Mar 10 '25 04:03 niteshbalusu11

Cool stuff @niteshbalusu11 !

mrousavy avatar Mar 10 '25 10:03 mrousavy

Any progress on this?

dougg0k avatar Jul 20 '25 23:07 dougg0k

Any progress on this?

Just write all core logic in rust and expose to C++ via FFI. It's really easy.

niteshbalusu11 avatar Jul 21 '25 01:07 niteshbalusu11

But is anybody working on something to automate the process?

Also, a guide added to the docs would be helpful.


By automated I mean not needing to write anything FFI related.

As to keep the code clean and more towards the core code of the lib.

dougg0k avatar Jul 21 '25 01:07 dougg0k

Just write all core logic in rust and expose to C++ via FFI. It's really easy.

Agreed - this could be super easy.

My stance on this is; we/I can add Rust support to Nitro if one of the following applies;

  • A company sponsors us/pays us to implement and maintain it (❌)
  • We use it for one of our own projects (❌)
  • A popular and actively maintained open-source project uses Nitro + Rust (❌)

Until none of those apply, there is zero reason for us to implement Rust support for Nitro, since I wouldn't have feedback, no testers, no one interested in backporting fixes or helping maintain it, and no one that will be worth the countless of hours of effort this would be (as a plugin for the nitrogen). Only issues and feature requests would and will not help here.

As to keep the code clean and more towards the core code of the lib.

The bigger the codebase of Nitro gets, the harder it is to maintain. Right now it's super simple and minimal, anything we add now cannot simply be removed (it'd be breaking) and harder to maintain.

Keep in mind this is a huge effort - bridging every type over to Rust is pretty hard and requires a ton of testing and feedback.

mrousavy avatar Jul 21 '25 09:07 mrousavy

So, this is happening https://www.callstack.com/blog/announcing-node-api-support-for-react-native

Apparently will be possible to use https://napi.rs


https://www.callstack.com/blog/build-and-run-node-api-modules-in-react-native


Waiting for confirmation, but It seems to be already possible with their ferric-cli https://github.com/callstackincubator/react-native-node-api/discussions/198

dougg0k avatar Jul 23 '25 17:07 dougg0k

There are plenty offtopic urls (non-nitro) above.


I edited the hidden post with a possible way that they already have in their repo, as a Rust option. If works, it's as clean or even more than nitro with c++.

dougg0k avatar Jul 23 '25 20:07 dougg0k

Looks like a new thing is out where we can use Rust.

https://github.com/leegeunhyeok/craby

niteshbalusu11 avatar Nov 09 '25 13:11 niteshbalusu11