use super::FREE_FN; use std::os::raw::c_void; /// A raw pointer and a length, exposed as an iterator. pub struct CBufferIter { ptr: *mut T, count: usize, i: usize, } impl CBufferIter { pub unsafe fn new(ptr: *mut T, count: usize) -> Self { Self { ptr, count, i: 0 } } } impl Iterator for CBufferIter { type Item = T; fn next(&mut self) -> Option { let i = self.i; if i >= self.count { None } else { self.i += 1; Some(unsafe { *self.ptr.offset(i as isize) }) } } fn size_hint(&self) -> (usize, Option) { let remaining = self.count - self.i; (remaining, Some(remaining)) } } impl ExactSizeIterator for CBufferIter {} impl Drop for CBufferIter { fn drop(&mut self) { unsafe { (FREE_FN)(self.ptr as *mut c_void) }; } }