Make Rust functions return ExactSizeIterator instead of just Iterator (#438)

* Add CBufferIter::size_hint

* Make Rust functions return ExactSizeIterator instead of just Iterator

* Fix wrong CBufferIter::size_hint
This commit is contained in:
Tuấn-Anh Nguyễn 2019-08-28 23:28:47 +07:00 committed by Max Brunsfeld
parent d96ba09391
commit f0999d7ac0
2 changed files with 10 additions and 3 deletions

View file

@ -22,15 +22,22 @@ impl<T: Copy> Iterator for CBufferIter<T> {
fn next(&mut self) -> Option<Self::Item> {
let i = self.i;
self.i += 1;
if i >= self.count {
None
} else {
self.i += 1;
Some(unsafe { *self.ptr.offset(i as isize) })
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
let remaining = self.count - self.i;
(remaining, Some(remaining))
}
}
impl<T: Copy> ExactSizeIterator for CBufferIter<T> {}
impl<T> Drop for CBufferIter<T> {
fn drop(&mut self) {
unsafe { free_ptr(self.ptr as *mut c_void); }