-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
draft: Add lifetime'd jarray wrapper types
- Loading branch information
Showing
12 changed files
with
516 additions
and
210 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,82 @@ | ||
use crate::{ | ||
objects::JObject, | ||
sys::{jobject, jobjectArray}, | ||
}; | ||
|
||
/// Lifetime'd representation of a [`jobjectArray`] which wraps a [`JObject`] reference | ||
#[repr(transparent)] | ||
#[derive(Debug)] | ||
pub struct JObjectArray<'local>(JObject<'local>); | ||
|
||
impl<'local> AsRef<JObjectArray<'local>> for JObjectArray<'local> { | ||
fn as_ref(&self) -> &JObjectArray<'local> { | ||
self | ||
} | ||
} | ||
|
||
impl<'local> AsRef<JObject<'local>> for JObjectArray<'local> { | ||
fn as_ref(&self) -> &JObject<'local> { | ||
self | ||
} | ||
} | ||
|
||
impl<'local> ::std::ops::Deref for JObjectArray<'local> { | ||
type Target = JObject<'local>; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl<'local> From<JObjectArray<'local>> for JObject<'local> { | ||
fn from(other: JObjectArray) -> JObject { | ||
other.0 | ||
} | ||
} | ||
|
||
/// This conversion assumes that the `JObject` is a pointer to a class object. | ||
impl<'local> From<JObject<'local>> for JObjectArray<'local> { | ||
fn from(other: JObject) -> Self { | ||
unsafe { Self::from_raw(other.into_raw()) } | ||
} | ||
} | ||
|
||
/// This conversion assumes that the `JObject` is a pointer to a class object. | ||
impl<'local, 'obj_ref> From<&'obj_ref JObject<'local>> for &'obj_ref JObjectArray<'local> { | ||
fn from(other: &'obj_ref JObject<'local>) -> Self { | ||
// Safety: `JObjectArray` is `repr(transparent)` around `JObject`. | ||
unsafe { &*(other as *const JObject<'local> as *const JObjectArray<'local>) } | ||
} | ||
} | ||
|
||
impl<'local> std::default::Default for JObjectArray<'local> { | ||
fn default() -> Self { | ||
Self(JObject::null()) | ||
} | ||
} | ||
|
||
impl<'local> JObjectArray<'local> { | ||
/// Creates a [`JObjectArray`] that wraps the given `raw` [`jclass`] | ||
/// | ||
/// # Safety | ||
/// | ||
/// `raw` may be a null pointer. If `raw` is not a null pointer, then: | ||
/// | ||
/// * `raw` must be a valid raw JNI local reference. | ||
/// * There must not be any other `JObject` representing the same local reference. | ||
/// * The lifetime `'local` must not outlive the local reference frame that the local reference | ||
/// was created in. | ||
pub unsafe fn from_raw(raw: jobjectArray) -> Self { | ||
Self(JObject::from_raw(raw as jobject)) | ||
} | ||
|
||
/// Returns the raw JNI pointer. | ||
pub fn as_raw(&self) -> jobjectArray { | ||
self.0.as_raw() as jobjectArray | ||
} | ||
|
||
/// Unwrap to the raw jni type. | ||
pub fn into_raw(self) -> jobjectArray { | ||
self.0.into_raw() as jobjectArray | ||
} | ||
} |
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
Oops, something went wrong.