2

I'm interested in doing some low level programming of Rust and React-Native, this tweet by Jared Sumner peaked my interested.

So basically he used the JSI to do a c++ implementation and apparently the results are much much faster than the regular libraries, so I got spelunking and followed some tutorials to get rust code working on a RN project.

Here is my basic rust code:

extern crate libc;

mod string;

use string::StringPtr;

// string ffi

#[no_mangle]
pub unsafe extern "C" fn rust_string_ptr(s: *mut String) -> *mut StringPtr {
    Box::into_raw(Box::new(StringPtr::from(&**s)))
}

#[no_mangle]
pub unsafe extern "C" fn rust_string_destroy(s: *mut String) {
    let _ = Box::from_raw(s);
}

#[no_mangle]
pub unsafe extern "C" fn rust_string_ptr_destroy(s: *mut StringPtr) {
    let _ = Box::from_raw(s);
}

#[no_mangle]
pub unsafe extern "C" fn hello_world(name: *mut StringPtr) -> *mut String {
    let name = (*name).as_str();
    let response = format!("Hello {}!", name);
    Box::into_raw(Box::new(response))
}

#[cfg(feature = "jni")]
#[allow(non_snake_case)]
pub mod android {
    extern crate jni;

    use self::jni::objects::{JClass, JString};
    use self::jni::sys::jstring;
    use self::jni::JNIEnv;

    #[no_mangle]
    pub unsafe extern "C" fn Java_com_mobile_1app_MobileAppBridge_helloWorld(
        env: JNIEnv,
        _: JClass,
        name: JString,
    ) -> jstring {
        let name: String = env.get_string(name).unwrap().into();
        let response = format!("Hello {}!", name);
        env.new_string(response).unwrap().into_inner()
    }
}

I have managed to compile rust code and pass a string to the RN code, the module is registered and I can call the library and get some strings, now however my question is, how do I import the photos module from iOS to get the camera roll photos?

Basically I would like to replace the camera-roll framework with a faster implementation, I would need to figure out how to get the photos.h library working with the rust code though.

Oscar Franco
  • 5,691
  • 5
  • 34
  • 56
  • Did you make any progress on this? With the upcoming release of the new react-native architecture it seems that Rust code could be a first class option for writing fast native modules but I lack the expertise to know how to integrate Rust with JSI to make that work seamlessly. – imagio Apr 12 '22 at 23:18
  • You can forget about this, when I asked this question I did not understand how the JSI was implemented. It is not a matter of just hooking up rust, JSI is written in C++ and takes C++ arguments such as lambdas and other primitive types, it won't work with Rust or other languages – Oscar Franco Apr 13 '22 at 13:04

1 Answers1

0

When I asked this question I did not understand how the JSI was implemented. It is not a matter of just hooking up rust, JSI is written in C++ and takes C++ arguments such as lambdas and other primitive types, it won't work with Rust or other languages.

Also, questions like how to call a iOS framework from Rust is also non-sensical, some frameworks might be written in C while others in Obj-c or Swift. So in some cases the answer is: you can't, or at least not-worth try to embed Rust in those scenarios.

Even if the JSI would support Rust, OS frameworks might never do, so it is still a pipedream to write your app in Rust.

Oscar Franco
  • 5,691
  • 5
  • 34
  • 56
  • It looks like it is indeed possible to do FFI in Rust with C++ lambdas but it doesn't seem simple https://users.rust-lang.org/t/rust-and-c-interoperability-c-lambdas/67136/5 . It seems a rust FFI binding to JSI is probably possible but I lack the experience with C++ to figure it out. Rust-bindgen is probably the place to start https://github.com/rust-lang/rust-bindgen – imagio Apr 14 '22 at 14:12
  • it's not only about the lambdas, you would also have to wrangle the primitive types from C++ to Rust and backwards. Even if technically possible, way too cumbersome for any real usage. – Oscar Franco Apr 14 '22 at 15:11