1 use crate::{objects::JObject, sys::jobject};
2 
3 /// Lifetime'd representation of a `jobject` that is an instance of the
4 /// ByteBuffer Java class. Just a `JObject` wrapped in a new class.
5 #[repr(transparent)]
6 #[derive(Debug)]
7 pub struct JByteBuffer<'local>(JObject<'local>);
8 
9 impl<'local> AsRef<JByteBuffer<'local>> for JByteBuffer<'local> {
as_ref(&self) -> &JByteBuffer<'local>10     fn as_ref(&self) -> &JByteBuffer<'local> {
11         self
12     }
13 }
14 
15 impl<'local> AsRef<JObject<'local>> for JByteBuffer<'local> {
as_ref(&self) -> &JObject<'local>16     fn as_ref(&self) -> &JObject<'local> {
17         self
18     }
19 }
20 
21 impl<'local> ::std::ops::Deref for JByteBuffer<'local> {
22     type Target = JObject<'local>;
23 
deref(&self) -> &Self::Target24     fn deref(&self) -> &Self::Target {
25         &self.0
26     }
27 }
28 
29 impl<'local> From<JByteBuffer<'local>> for JObject<'local> {
from(other: JByteBuffer) -> JObject30     fn from(other: JByteBuffer) -> JObject {
31         other.0
32     }
33 }
34 
35 impl<'local> From<JObject<'local>> for JByteBuffer<'local> {
from(other: JObject) -> Self36     fn from(other: JObject) -> Self {
37         unsafe { Self::from_raw(other.into_raw()) }
38     }
39 }
40 
41 impl<'local, 'obj_ref> From<&'obj_ref JObject<'local>> for &'obj_ref JByteBuffer<'local> {
from(other: &'obj_ref JObject<'local>) -> Self42     fn from(other: &'obj_ref JObject<'local>) -> Self {
43         // Safety: `JByteBuffer` is `repr(transparent)` around `JObject`.
44         unsafe { &*(other as *const JObject<'local> as *const JByteBuffer<'local>) }
45     }
46 }
47 
48 impl<'local> std::default::Default for JByteBuffer<'local> {
default() -> Self49     fn default() -> Self {
50         Self(JObject::null())
51     }
52 }
53 
54 impl<'local> JByteBuffer<'local> {
55     /// Creates a [`JByteBuffer`] that wraps the given `raw` [`jobject`]
56     ///
57     /// # Safety
58     /// No runtime check is made to verify that the given [`jobject`] is an instance of
59     /// a `ByteBuffer`.
from_raw(raw: jobject) -> Self60     pub unsafe fn from_raw(raw: jobject) -> Self {
61         Self(JObject::from_raw(raw as jobject))
62     }
63 
64     /// Unwrap to the raw jni type.
into_raw(self) -> jobject65     pub fn into_raw(self) -> jobject {
66         self.0.into_raw() as jobject
67     }
68 }
69