From f0999d7ac0c2a96a493cee91ce7dff78d34dcaf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tu=E1=BA=A5n-Anh=20Nguy=E1=BB=85n?= Date: Wed, 28 Aug 2019 23:28:47 +0700 Subject: [PATCH] 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 --- lib/binding_rust/lib.rs | 4 ++-- lib/binding_rust/util.rs | 9 ++++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/binding_rust/lib.rs b/lib/binding_rust/lib.rs index 23315ea4..0ee168da 100644 --- a/lib/binding_rust/lib.rs +++ b/lib/binding_rust/lib.rs @@ -448,7 +448,7 @@ impl Tree { TreePropertyCursor::new(self, property_sheet, source) } - pub fn changed_ranges(&self, other: &Tree) -> impl Iterator { + pub fn changed_ranges(&self, other: &Tree) -> impl ExactSizeIterator { 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> { + pub fn children(&self) -> impl ExactSizeIterator> { let me = self.clone(); (0..self.child_count()) .into_iter() diff --git a/lib/binding_rust/util.rs b/lib/binding_rust/util.rs index df73f830..e62e371f 100644 --- a/lib/binding_rust/util.rs +++ b/lib/binding_rust/util.rs @@ -22,15 +22,22 @@ impl Iterator for CBufferIter { fn next(&mut self) -> Option { 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) { + let remaining = self.count - self.i; + (remaining, Some(remaining)) + } } +impl ExactSizeIterator for CBufferIter {} + impl Drop for CBufferIter { fn drop(&mut self) { unsafe { free_ptr(self.ptr as *mut c_void); }