1 use std::marker::PhantomData; 2 3 use crate::sys::jobject; 4 5 /// Wrapper around `sys::jobject` that adds a lifetime. This prevents it from 6 /// outliving the context in which it was acquired and getting GC'd out from 7 /// under us. It matches C's representation of the raw pointer, so it can be 8 /// used in any of the extern function argument positions that would take a 9 /// `jobject`. 10 /// 11 /// Most other types in the `objects` module deref to this, as they do in the C 12 /// representation. 13 #[repr(transparent)] 14 #[derive(Clone, Copy, Debug)] 15 pub struct JObject<'a> { 16 internal: jobject, 17 lifetime: PhantomData<&'a ()>, 18 } 19 20 impl<'a> From<jobject> for JObject<'a> { from(other: jobject) -> Self21 fn from(other: jobject) -> Self { 22 JObject { 23 internal: other, 24 lifetime: PhantomData, 25 } 26 } 27 } 28 29 impl<'a> ::std::ops::Deref for JObject<'a> { 30 type Target = jobject; 31 deref(&self) -> &Self::Target32 fn deref(&self) -> &Self::Target { 33 &self.internal 34 } 35 } 36 37 impl<'a> JObject<'a> { 38 /// Unwrap to the internal jni type. into_inner(self) -> jobject39 pub fn into_inner(self) -> jobject { 40 self.internal 41 } 42 43 /// Creates a new null object null() -> JObject<'a>44 pub fn null() -> JObject<'a> { 45 (::std::ptr::null_mut() as jobject).into() 46 } 47 } 48