use std::marker::PhantomData;
use std::os::raw::c_void;
#[repr(C)]
#[derive(Default, Copy, Clone, Debug)]
pub struct RUBase {
_unused: [u8; 0],
}
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct RUArray {
pub delete_callback: extern "C" fn(data: *const c_void),
pub priv_data: *const c_void,
pub elements: *const c_void,
pub count: i32,
}
pub struct PrimitiveArray<T> {
data: RUArray,
index: usize,
_marker: PhantomData<T>,
}
impl<T> PrimitiveArray<T> {
pub fn new(data: RUArray) -> PrimitiveArray<T> {
PrimitiveArray {
index: 0,
_marker: PhantomData,
data,
}
}
}
impl<T> Drop for PrimitiveArray<T> {
fn drop(&mut self) {
(self.data.delete_callback)(self.data.elements);
}
}
impl<T> Iterator for PrimitiveArray<T>
where
T: std::marker::Copy,
{
type Item = T;
fn next(&mut self) -> Option<T> {
if self.index >= self.data.count as usize {
None
} else {
unsafe {
let data = std::slice::from_raw_parts(
self.data.priv_data as *const T,
self.data.count as usize,
);
let index = self.index;
self.index += 1;
Some(data[index])
}
}
}
}