use std::marker::PhantomData;
use crate::{
objects::JObject,
sys::{jarray, jobject},
};
use super::TypeArray;
#[cfg(doc)]
use crate::JNIEnv;
#[repr(transparent)]
#[derive(Debug)]
pub struct JPrimitiveArray<'local, T: TypeArray> {
obj: JObject<'local>,
lifetime: PhantomData<&'local T>,
}
impl<'local, T: TypeArray> AsRef<JPrimitiveArray<'local, T>> for JPrimitiveArray<'local, T> {
fn as_ref(&self) -> &JPrimitiveArray<'local, T> {
self
}
}
impl<'local, T: TypeArray> AsMut<JPrimitiveArray<'local, T>> for JPrimitiveArray<'local, T> {
fn as_mut(&mut self) -> &mut JPrimitiveArray<'local, T> {
self
}
}
impl<'local, T: TypeArray> AsRef<JObject<'local>> for JPrimitiveArray<'local, T> {
fn as_ref(&self) -> &JObject<'local> {
&self.obj
}
}
impl<'local, T: TypeArray> ::std::ops::Deref for JPrimitiveArray<'local, T> {
type Target = JObject<'local>;
fn deref(&self) -> &Self::Target {
&self.obj
}
}
impl<'local, T: TypeArray> From<JPrimitiveArray<'local, T>> for JObject<'local> {
fn from(other: JPrimitiveArray<'local, T>) -> JObject {
other.obj
}
}
impl<'local, T: TypeArray> From<JObject<'local>> for JPrimitiveArray<'local, T> {
fn from(other: JObject) -> Self {
unsafe { Self::from_raw(other.into_raw()) }
}
}
impl<'local, 'obj_ref, T: TypeArray> From<&'obj_ref JObject<'local>>
for &'obj_ref JPrimitiveArray<'local, T>
{
fn from(other: &'obj_ref JObject<'local>) -> Self {
unsafe { &*(other as *const JObject<'local> as *const JPrimitiveArray<'local, T>) }
}
}
impl<'local, T: TypeArray> std::default::Default for JPrimitiveArray<'local, T> {
fn default() -> Self {
Self {
obj: JObject::null(),
lifetime: PhantomData,
}
}
}
impl<'local, T: TypeArray> JPrimitiveArray<'local, T> {
pub unsafe fn from_raw(raw: jarray) -> Self {
Self {
obj: JObject::from_raw(raw as jobject),
lifetime: PhantomData,
}
}
pub fn into_raw(self) -> jarray {
self.obj.into_raw() as jarray
}
}
pub type JBooleanArray<'local> = JPrimitiveArray<'local, crate::sys::jboolean>;
pub type JByteArray<'local> = JPrimitiveArray<'local, crate::sys::jbyte>;
pub type JCharArray<'local> = JPrimitiveArray<'local, crate::sys::jchar>;
pub type JShortArray<'local> = JPrimitiveArray<'local, crate::sys::jshort>;
pub type JIntArray<'local> = JPrimitiveArray<'local, crate::sys::jint>;
pub type JLongArray<'local> = JPrimitiveArray<'local, crate::sys::jlong>;
pub type JFloatArray<'local> = JPrimitiveArray<'local, crate::sys::jfloat>;
pub type JDoubleArray<'local> = JPrimitiveArray<'local, crate::sys::jdouble>;
pub unsafe trait AsJArrayRaw<'local>: AsRef<JObject<'local>> {
fn as_jarray_raw(&self) -> jarray {
self.as_ref().as_raw() as jarray
}
}
unsafe impl<'local, T: TypeArray> AsJArrayRaw<'local> for JPrimitiveArray<'local, T> {}