java_oxide/
as_arg.rs

1use crate::{AsJValue, AssignableTo, Global, Local, Null, Ref, ReferenceType};
2use jni_sys::jobject;
3use std::ptr::null_mut;
4
5/// A marker trait indicating this is a valid JNI reference type for Java method argument
6/// type `T`, this can be null.
7///
8/// # Safety
9///
10/// It should be implemented automatically by `java_oxide`.
11pub unsafe trait AsArg<T>: Sized + AsJValue {
12    fn as_arg(&self) -> jobject;
13}
14
15unsafe impl<T: ReferenceType, U: AsArg<T>> AsArg<T> for &U {
16    fn as_arg(&self) -> jobject {
17        U::as_arg(self)
18    }
19}
20
21unsafe impl<T: ReferenceType, U: AsArg<T>> AsArg<T> for &mut U {
22    fn as_arg(&self) -> jobject {
23        U::as_arg(self)
24    }
25}
26
27unsafe impl<T: ReferenceType> AsArg<T> for Null {
28    fn as_arg(&self) -> jobject {
29        null_mut()
30    }
31}
32
33unsafe impl<T: ReferenceType, U: AssignableTo<T>> AsArg<T> for Ref<'_, U> {
34    fn as_arg(&self) -> jobject {
35        self.as_raw()
36    }
37}
38
39unsafe impl<T: ReferenceType, U: AssignableTo<T>> AsArg<T> for Option<Ref<'_, U>> {
40    fn as_arg(&self) -> jobject {
41        self.as_ref().map(|r| r.as_raw()).unwrap_or(null_mut())
42    }
43}
44
45unsafe impl<T: ReferenceType, U: AssignableTo<T>> AsArg<T> for Option<&Ref<'_, U>> {
46    fn as_arg(&self) -> jobject {
47        self.map(|r| r.as_raw()).unwrap_or(null_mut())
48    }
49}
50
51unsafe impl<T: ReferenceType, U: AssignableTo<T>> AsArg<T> for Local<'_, U> {
52    fn as_arg(&self) -> jobject {
53        self.as_raw()
54    }
55}
56
57unsafe impl<T: ReferenceType, U: AssignableTo<T>> AsArg<T> for Option<Local<'_, U>> {
58    fn as_arg(&self) -> jobject {
59        self.as_ref().map(|r| r.as_raw()).unwrap_or(null_mut())
60    }
61}
62
63unsafe impl<T: ReferenceType, U: AssignableTo<T>> AsArg<T> for Option<&Local<'_, U>> {
64    fn as_arg(&self) -> jobject {
65        self.map(|r| r.as_raw()).unwrap_or(null_mut())
66    }
67}
68
69unsafe impl<T: ReferenceType, U: AssignableTo<T>> AsArg<T> for Global<U> {
70    fn as_arg(&self) -> jobject {
71        self.as_raw()
72    }
73}
74
75unsafe impl<T: ReferenceType, U: AssignableTo<T>> AsArg<T> for Option<Global<U>> {
76    fn as_arg(&self) -> jobject {
77        self.as_ref().map(|r| r.as_raw()).unwrap_or(null_mut())
78    }
79}
80
81unsafe impl<T: ReferenceType, U: AssignableTo<T>> AsArg<T> for Option<&Global<U>> {
82    fn as_arg(&self) -> jobject {
83        self.map(|r| r.as_raw()).unwrap_or(null_mut())
84    }
85}