use super::array::Il2cppArray;
use std::ffi::c_void;
use std::marker::PhantomData;
#[repr(C)]
pub struct Il2cppList<T: Copy> {
pub klass: *mut c_void,
pub monitor: *mut c_void,
pub items: *mut Il2cppArray<T>,
pub size: i32,
pub version: i32,
_phantom: PhantomData<T>,
}
impl<T: Copy> Il2cppList<T> {
pub fn items_array(&self) -> Option<&Il2cppArray<T>> {
if self.items.is_null() {
None
} else {
unsafe { Some(&*self.items) }
}
}
pub fn items_array_mut(&mut self) -> Option<&mut Il2cppArray<T>> {
if self.items.is_null() {
None
} else {
unsafe { Some(&mut *self.items) }
}
}
pub fn get(&self, index: usize) -> Option<T> {
if index >= self.size as usize {
return None;
}
self.items_array().map(|arr| arr.get(index))
}
pub fn at(&self, index: usize) -> T {
self.get(index).expect("Index out of bounds")
}
pub fn set(&mut self, index: usize, value: T) -> bool {
if index >= self.size as usize {
return false;
}
if let Some(arr) = self.items_array_mut() {
arr.set(index, value);
true
} else {
false
}
}
pub fn to_vec(&self) -> Vec<T> {
let mut result = Vec::with_capacity(self.size as usize);
for i in 0..self.size as usize {
if let Some(item) = self.get(i) {
result.push(item);
}
}
result
}
pub fn get_pointer(&self) -> Option<*const T> {
self.items_array().map(|arr| arr.get_pointer())
}
pub fn iter(&self) -> impl Iterator<Item = T> + '_ {
(0..self.size as usize).filter_map(|i| self.get(i))
}
}