• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use crate::{
2     objects::JObject,
3     sys::{jobject, jobjectArray},
4 };
5 
6 use super::AsJArrayRaw;
7 
8 /// Lifetime'd representation of a [`jobjectArray`] which wraps a [`JObject`] reference
9 #[repr(transparent)]
10 #[derive(Debug)]
11 pub struct JObjectArray<'local>(JObject<'local>);
12 
13 impl<'local> AsRef<JObjectArray<'local>> for JObjectArray<'local> {
as_ref(&self) -> &JObjectArray<'local>14     fn as_ref(&self) -> &JObjectArray<'local> {
15         self
16     }
17 }
18 
19 impl<'local> AsRef<JObject<'local>> for JObjectArray<'local> {
as_ref(&self) -> &JObject<'local>20     fn as_ref(&self) -> &JObject<'local> {
21         self
22     }
23 }
24 
25 impl<'local> ::std::ops::Deref for JObjectArray<'local> {
26     type Target = JObject<'local>;
27 
deref(&self) -> &Self::Target28     fn deref(&self) -> &Self::Target {
29         &self.0
30     }
31 }
32 
33 impl<'local> From<JObjectArray<'local>> for JObject<'local> {
from(other: JObjectArray) -> JObject34     fn from(other: JObjectArray) -> JObject {
35         other.0
36     }
37 }
38 
39 /// This conversion assumes that the `JObject` is a pointer to a class object.
40 impl<'local> From<JObject<'local>> for JObjectArray<'local> {
from(other: JObject) -> Self41     fn from(other: JObject) -> Self {
42         unsafe { Self::from_raw(other.into_raw()) }
43     }
44 }
45 
46 /// This conversion assumes that the `JObject` is a pointer to a class object.
47 impl<'local, 'obj_ref> From<&'obj_ref JObject<'local>> for &'obj_ref JObjectArray<'local> {
from(other: &'obj_ref JObject<'local>) -> Self48     fn from(other: &'obj_ref JObject<'local>) -> Self {
49         // Safety: `JObjectArray` is `repr(transparent)` around `JObject`.
50         unsafe { &*(other as *const JObject<'local> as *const JObjectArray<'local>) }
51     }
52 }
53 
54 impl<'local> std::default::Default for JObjectArray<'local> {
default() -> Self55     fn default() -> Self {
56         Self(JObject::null())
57     }
58 }
59 
60 unsafe impl<'local> AsJArrayRaw<'local> for JObjectArray<'local> {}
61 
62 impl<'local> JObjectArray<'local> {
63     /// Creates a [`JObjectArray`] that wraps the given `raw` [`jobjectArray`]
64     ///
65     /// # Safety
66     ///
67     /// `raw` may be a null pointer. If `raw` is not a null pointer, then:
68     ///
69     /// * `raw` must be a valid raw JNI local reference.
70     /// * There must not be any other `JObject` representing the same local reference.
71     /// * The lifetime `'local` must not outlive the local reference frame that the local reference
72     ///   was created in.
from_raw(raw: jobjectArray) -> Self73     pub unsafe fn from_raw(raw: jobjectArray) -> Self {
74         Self(JObject::from_raw(raw as jobject))
75     }
76 
77     /// Unwrap to the raw jni type.
into_raw(self) -> jobjectArray78     pub fn into_raw(self) -> jobjectArray {
79         self.0.into_raw() as jobjectArray
80     }
81 }
82