-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add FFI-compatible
Return<'env, T>
for returning objects from a nat…
…ive method call.
- Loading branch information
Showing
3 changed files
with
50 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use std::marker::PhantomData; | ||
use std::ptr::null_mut; | ||
|
||
use jni_sys::jobject; | ||
|
||
use crate::ReferenceType; | ||
|
||
/// FFI: Use **Return\<java::lang::Object\>** instead of jobject. This represents a (null?) JNI function call return value. | ||
/// | ||
/// Unlike most Java reference types from this library, this *can* be null. | ||
/// | ||
/// FFI safe where a jobject is safe, assuming you match your types correctly. | ||
#[repr(transparent)] | ||
pub struct Return<'env, T: ReferenceType> { | ||
object: jobject, | ||
_class: PhantomData<&'env T>, | ||
} | ||
|
||
impl<'env, T: ReferenceType> Return<'env, T> { | ||
pub unsafe fn from_raw(object: jobject) -> Self { | ||
Self { | ||
object, | ||
_class: PhantomData, | ||
} | ||
} | ||
|
||
pub fn null() -> Self { | ||
Self { | ||
object: null_mut(), | ||
_class: PhantomData, | ||
} | ||
} | ||
|
||
pub fn as_raw(&self) -> jobject { | ||
self.object | ||
} | ||
} |