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

@ -448,7 +448,7 @@ impl Tree {
TreePropertyCursor::new(self, property_sheet, source)
}
pub fn changed_ranges(&self, other: &Tree) -> impl Iterator<Item = Range> {
pub fn changed_ranges(&self, other: &Tree) -> impl ExactSizeIterator<Item = Range> {
let mut count = 0;
unsafe {
let ptr = ffi::ts_tree_get_changed_ranges(self.0, other.0, &mut count as *mut _ as *mut u32);
@ -570,7 +570,7 @@ impl<'tree> Node<'tree> {
unsafe { ffi::ts_node_child_count(self.0) as usize }
}
pub fn children(&self) -> impl Iterator<Item = Node<'tree>> {
pub fn children(&self) -> impl ExactSizeIterator<Item = Node<'tree>> {
let me = self.clone();
(0..self.child_count())
.into_iter()

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); }