java_oxide/refs/
return_.rs

1use crate::ReferenceType;
2use jni_sys::jobject;
3use std::{marker::PhantomData, ptr::null_mut};
4
5/// FFI: Use **Return\<java::lang::Object\>** instead of `jobject`.  This represents a (null?) JNI function call return value.
6///
7/// Unlike most Java reference types from this library, this *can* be null. Recommended constructors are
8/// [crate::Local::into_return] and [Return::null].
9///
10/// FFI safe where a jobject is safe, assuming you match your types correctly.
11#[repr(transparent)]
12pub struct Return<'env, T: ReferenceType> {
13    object: jobject,
14    _class: PhantomData<&'env T>,
15}
16
17impl<'env, T: ReferenceType> Return<'env, T> {
18    /// Wraps a raw JNI reference.
19    ///
20    /// # Safety
21    ///
22    /// - If `object` is non-null, it must be a JNI local(?) reference to an instance of type `T`;
23    /// - `object` must keep valid for `'env` lifetime; it is not owned by `Local` or any other wrapper
24    ///   that deletes the reference on `Drop` before the JNI native method call returns.
25    pub unsafe fn from_raw(object: jobject) -> Self {
26        Self {
27            object,
28            _class: PhantomData,
29        }
30    }
31
32    /// Creates a null value to be returned from the JNI native method.
33    pub fn null() -> Self {
34        Self {
35            object: null_mut(),
36            _class: PhantomData,
37        }
38    }
39
40    /// Returns the raw JNI reference pointer. Generally it should not be used.
41    pub fn as_raw(&self) -> jobject {
42        self.object
43    }
44}
45
46impl<'env, T: ReferenceType> Default for Return<'env, T> {
47    /// This is a null value.
48    fn default() -> Self {
49        Self::null()
50    }
51}