mirror of
https://github.com/hexedtech/jni-toolbox.git
synced 2024-11-25 00:44:53 +01:00
chore: retab readme
This commit is contained in:
parent
561279d1dd
commit
62c4081694
1 changed files with 72 additions and 72 deletions
144
README.md
144
README.md
|
@ -9,7 +9,7 @@ just specify package and class on your function, and done!
|
||||||
```rust
|
```rust
|
||||||
#[jni_toolbox::jni(package = "your.package.path", class = "ContainerClass")]
|
#[jni_toolbox::jni(package = "your.package.path", class = "ContainerClass")]
|
||||||
fn your_function_name(arg: String) -> Result<Vec<String>, String> {
|
fn your_function_name(arg: String) -> Result<Vec<String>, String> {
|
||||||
Ok(arg.split('/').map(|x| x.to_string()).collect())
|
Ok(arg.split('/').map(|x| x.to_string()).collect())
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -32,7 +32,7 @@ the following function:
|
||||||
```rust
|
```rust
|
||||||
#[jni(package = "mp.code", class = "Client", ptr)]
|
#[jni(package = "mp.code", class = "Client", ptr)]
|
||||||
fn connect(config: Config) -> Result<Client, ConnectionError> {
|
fn connect(config: Config) -> Result<Client, ConnectionError> {
|
||||||
tokio().block_on(Client::connect(config))
|
tokio().block_on(Client::connect(config))
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -42,82 +42,82 @@ gets turned into these two functions:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
fn connect(config: Config) -> Result<Client, ConnectionError> {
|
fn connect(config: Config) -> Result<Client, ConnectionError> {
|
||||||
tokio().block_on(Client::connect(config))
|
tokio().block_on(Client::connect(config))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
#[allow(unused_mut)]
|
#[allow(unused_mut)]
|
||||||
pub extern "system" fn Java_mp_code_Client_connect<'local>(
|
pub extern "system" fn Java_mp_code_Client_connect<'local>(
|
||||||
mut env: jni::JNIEnv<'local>,
|
mut env: jni::JNIEnv<'local>,
|
||||||
_class: jni::objects::JClass<'local>,
|
_class: jni::objects::JClass<'local>,
|
||||||
mut config: <Config as jni_toolbox::FromJava<'local>>::T,
|
mut config: <Config as jni_toolbox::FromJava<'local>>::T,
|
||||||
) -> <Client as jni_toolbox::IntoJava<'local>>::T {
|
) -> <Client as jni_toolbox::IntoJava<'local>>::T {
|
||||||
use jni_toolbox::{FromJava, IntoJava, JniToolboxError};
|
use jni_toolbox::{FromJava, IntoJava, JniToolboxError};
|
||||||
let mut env_copy = unsafe { env.unsafe_clone() };
|
let mut env_copy = unsafe { env.unsafe_clone() };
|
||||||
let config_new = match jni_toolbox::from_java_static::<Config>(&mut env, config) {
|
let config_new = match jni_toolbox::from_java_static::<Config>(&mut env, config) {
|
||||||
Ok(x) => x,
|
Ok(x) => x,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
let _ = env.throw_new(
|
let _ = env.throw_new(
|
||||||
"java/lang/RuntimeException",
|
"java/lang/RuntimeException",
|
||||||
$crate::__export::must_use({
|
$crate::__export::must_use({
|
||||||
let res = $crate::fmt::format($crate::__export::format_args!("{e:?}"));
|
let res = $crate::fmt::format($crate::__export::format_args!("{e:?}"));
|
||||||
res
|
res
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
return std::ptr::null_mut();
|
return std::ptr::null_mut();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
match connect(config_new) {
|
match connect(config_new) {
|
||||||
Err(e) => match env_copy.find_class(e.jclass()) {
|
Err(e) => match env_copy.find_class(e.jclass()) {
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
$crate::panicking::panic_fmt($crate::const_format_args!(
|
$crate::panicking::panic_fmt($crate::const_format_args!(
|
||||||
"error throwing Java exception -- failed resolving error class: {e}"
|
"error throwing Java exception -- failed resolving error class: {e}"
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
Ok(class) => match env_copy.new_string($crate::__export::must_use({
|
Ok(class) => match env_copy.new_string($crate::__export::must_use({
|
||||||
let res = $crate::fmt::format($crate::__export::format_args!("{e:?}"));
|
let res = $crate::fmt::format($crate::__export::format_args!("{e:?}"));
|
||||||
res
|
res
|
||||||
})) {
|
})) {
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
$crate::panicking::panic_fmt($crate::const_format_args!(
|
$crate::panicking::panic_fmt($crate::const_format_args!(
|
||||||
"error throwing Java exception -- failed creating error string: {e}"
|
"error throwing Java exception -- failed creating error string: {e}"
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
Ok(msg) => match env_copy.new_object(
|
Ok(msg) => match env_copy.new_object(
|
||||||
class,
|
class,
|
||||||
"(Ljava/lang/String;)V",
|
"(Ljava/lang/String;)V",
|
||||||
&[jni::objects::JValueGen::Object(&msg)],
|
&[jni::objects::JValueGen::Object(&msg)],
|
||||||
) {
|
) {
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
$crate::panicking::panic_fmt($crate::const_format_args!(
|
$crate::panicking::panic_fmt($crate::const_format_args!(
|
||||||
"error throwing Java exception -- failed creating object: {e}"
|
"error throwing Java exception -- failed creating object: {e}"
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
Ok(obj) => match env_copy.throw(jni::objects::JThrowable::from(obj)) {
|
Ok(obj) => match env_copy.throw(jni::objects::JThrowable::from(obj)) {
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
$crate::panicking::panic_fmt($crate::const_format_args!(
|
$crate::panicking::panic_fmt($crate::const_format_args!(
|
||||||
"error throwing Java exception -- failed throwing: {e}"
|
"error throwing Java exception -- failed throwing: {e}"
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
Ok(_) => return std::ptr::null_mut(),
|
Ok(_) => return std::ptr::null_mut(),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Ok(ret) => match ret.into_java(&mut env_copy) {
|
Ok(ret) => match ret.into_java(&mut env_copy) {
|
||||||
Ok(fin) => return fin,
|
Ok(fin) => return fin,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
let _ = env_copy.throw_new(
|
let _ = env_copy.throw_new(
|
||||||
"java/lang/RuntimeException",
|
"java/lang/RuntimeException",
|
||||||
$crate::__export::must_use({
|
$crate::__export::must_use({
|
||||||
let res = $crate::fmt::format($crate::__export::format_args!("{e:?}"));
|
let res = $crate::fmt::format($crate::__export::format_args!("{e:?}"));
|
||||||
res
|
res
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
return std::ptr::null_mut();
|
return std::ptr::null_mut();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue