• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use crate::{
2     objects::JObject,
3     sys::{jobject, jstring},
4 };
5 
6 /// Lifetime'd representation of a `jstring`. Just a `JObject` wrapped in a new
7 /// class.
8 #[repr(transparent)]
9 #[derive(Clone, Copy)]
10 pub struct JString<'a>(JObject<'a>);
11 
12 impl<'a> ::std::ops::Deref for JString<'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<JString<'a>> for JObject<'a> {
from(other: JString) -> JObject21     fn from(other: JString) -> JObject {
22         other.0
23     }
24 }
25 
26 impl<'a> From<JObject<'a>> for JString<'a> {
from(other: JObject) -> Self27     fn from(other: JObject) -> Self {
28         unsafe { Self::from_raw(other.into_raw()) }
29     }
30 }
31 
32 impl<'a> std::default::Default for JString<'a> {
default() -> Self33     fn default() -> Self {
34         Self(JObject::null())
35     }
36 }
37 
38 impl<'a> JString<'a> {
39     /// Creates a [`JString`] that wraps the given `raw` [`jstring`]
40     ///
41     /// # Safety
42     ///
43     /// Expects a valid pointer or `null`
from_raw(raw: jstring) -> Self44     pub unsafe fn from_raw(raw: jstring) -> Self {
45         Self(JObject::from_raw(raw as jobject))
46     }
47 
48     /// Unwrap to the raw jni type.
into_raw(self) -> jstring49     pub fn into_raw(self) -> jstring {
50         self.0.into_raw() as jstring
51     }
52 }
53