tribufu-java/Source/lib.rs
GuilhermeWerner ee9258811a Update lib.rs
2021-10-03 13:53:48 -03:00

39 lines
1.2 KiB
Rust

// Copyright (c) TribuFu. All Rights Reserved.
//! JNI bindings to TribuFu SDK.
#![allow(non_snake_case)]
#[cfg(not(any(
target_os = "windows",
target_os = "macos",
target_os = "linux",
target_os = "android",
)))]
compile_error!("INVALID_TARGET_PLATFORM");
use jni::objects::{JClass, JString};
use jni::sys::jstring;
use jni::JNIEnv;
use std::ffi::{CStr, CString};
#[no_mangle]
// This is the class that owns our static method. It's not going to be used,
// but still must be present to match the expected signature of a static
// native method.
pub extern "system" fn Java_com_tribufu_sdk_TribuFu_getVersion(
env: JNIEnv,
_class: JClass,
input: JString,
) -> jstring {
// First, we have to get the string out of Java. Check out the `strings`
// module for more info on how this works.
let input = CString::from(unsafe { CStr::from_ptr(env.get_string(input).unwrap().as_ptr()) });
// Then we have to create a new Java string to return. Again, more info
// in the `strings` module.
let output = env.new_string(input.to_str().unwrap()).unwrap();
// Finally, extract the raw pointer to return.
return output.into_inner();
}