Can JNI crate accept generics #437
-
Can I do something like #[allow(non_snake_case)]
#[allow(no_mangle_generic_items)]
#[no_mangle]
pub extern "system" fn Java_RustLibrary_rustyGenericClass<'local, T>(
env: JNIEnv,
_class: JClass<'local>,
input: T,
) where
T: Into<JObject<'local>>,
{
} Whenever I add a generic type to an extern function it does not get compiled into the shared library like other extern functions. I have even checked the shared library with nm -D target/x86_64-unknown-linux-gnu/release/lib_name_of_lib.so It does not get compiled. Does JNI crate support generic extern functions ? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I don't think that's possible, no. The Rust compiler generates (“instantiates”) a separate version of each generic function for each combination of type parameters it's called with. That happens at compile time. What you're writing there is a function that's dynamically looked up and called by the JVM after your Rust library is compiled, so there's no opportunity for that instantiation to happen. I'm surprised that using |
Beta Was this translation helpful? Give feedback.
I don't think that's possible, no.
The Rust compiler generates (“instantiates”) a separate version of each generic function for each combination of type parameters it's called with. That happens at compile time. What you're writing there is a function that's dynamically looked up and called by the JVM after your Rust library is compiled, so there's no opportunity for that instantiation to happen.
I'm surprised that using
#[no_mangle]
on a function with type parameters isn't a compile error. That's arguably a bug in Rust, since such a function is impossible to call without name mangling.