java_spaghetti/
as_jvalue.rs

1use jni_sys::*;
2
3#[doc(hidden)] // You should generally not be interacting with this type directly, but it must be public for codegen.
4/// By implementing this you assert that you're constructing a valid jvalue for the given argument type (e.g. valid
5/// jobject pointer if the function is supposed to take a jobject)
6pub unsafe trait AsJValue {
7    fn as_jvalue(&self) -> jvalue;
8}
9
10unsafe impl AsJValue for bool {
11    fn as_jvalue(&self) -> jvalue {
12        jvalue {
13            z: if *self { JNI_TRUE } else { JNI_FALSE },
14        }
15    }
16}
17unsafe impl AsJValue for jbyte {
18    fn as_jvalue(&self) -> jvalue {
19        jvalue { b: *self }
20    }
21}
22unsafe impl AsJValue for jchar {
23    fn as_jvalue(&self) -> jvalue {
24        jvalue { c: *self }
25    }
26}
27unsafe impl AsJValue for jshort {
28    fn as_jvalue(&self) -> jvalue {
29        jvalue { s: *self }
30    }
31}
32unsafe impl AsJValue for jint {
33    fn as_jvalue(&self) -> jvalue {
34        jvalue { i: *self }
35    }
36}
37unsafe impl AsJValue for jlong {
38    fn as_jvalue(&self) -> jvalue {
39        jvalue { j: *self }
40    }
41}
42unsafe impl AsJValue for jfloat {
43    fn as_jvalue(&self) -> jvalue {
44        jvalue { f: *self }
45    }
46}
47unsafe impl AsJValue for jdouble {
48    fn as_jvalue(&self) -> jvalue {
49        jvalue { d: *self }
50    }
51}
52//unsafe impl AsJValue for jobject  { fn as_jvalue(&self) -> jvalue { jvalue { l: *self } } } // do NOT implement, no guarantee any given jobject is actually safe!