• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use crate::{
2     objects::JObject,
3     sys::{jclass, jobject},
4 };
5 
6 /// Lifetime'd representation of a `jclass`. Just a `JObject` wrapped in a new
7 /// class.
8 #[repr(transparent)]
9 #[derive(Clone, Copy, Debug)]
10 pub struct JClass<'a>(JObject<'a>);
11 
12 impl<'a> ::std::ops::Deref for JClass<'a> {
13     type Target = JObject<'a>;
14 
deref(&self) -> &Self::Target15     fn deref(&self) -> &Self::Target {
16         &self.0
17     }
18 }
19 
20 impl<'a> From<JClass<'a>> for JObject<'a> {
from(other: JClass) -> JObject21     fn from(other: JClass) -> JObject {
22         other.0
23     }
24 }
25 
26 /// This conversion assumes that the `JObject` is a pointer to a class object.
27 impl<'a> From<JObject<'a>> for JClass<'a> {
from(other: JObject) -> Self28     fn from(other: JObject) -> Self {
29         unsafe { Self::from_raw(other.into_raw()) }
30     }
31 }
32 
33 impl<'a> std::default::Default for JClass<'a> {
default() -> Self34     fn default() -> Self {
35         Self(JObject::null())
36     }
37 }
38 
39 impl<'a> JClass<'a> {
40     /// Creates a [`JClass`] that wraps the given `raw` [`jclass`]
41     ///
42     /// # Safety
43     ///
44     /// Expects a valid pointer or `null`
from_raw(raw: jclass) -> Self45     pub unsafe fn from_raw(raw: jclass) -> Self {
46         Self(JObject::from_raw(raw as jobject))
47     }
48 
49     /// Unwrap to the raw jni type.
into_raw(self) -> jclass50     pub fn into_raw(self) -> jclass {
51         self.0.into_raw() as jclass
52     }
53 }
54