Make Tree::changed_ranges return an Iterator instead of a Vec (#437)
* Make Tree::changed_ranges return an Iterator instead of a Vec * Remove CBufferIter.free parameter
This commit is contained in:
parent
d5b5d473ab
commit
d96ba09391
5 changed files with 49 additions and 19 deletions
38
lib/binding_rust/util.rs
Normal file
38
lib/binding_rust/util.rs
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
use std::os::raw::c_void;
|
||||
|
||||
extern "C" {
|
||||
#[link_name = "rust_tree_sitter_free"]
|
||||
pub fn free_ptr(ptr: *mut c_void);
|
||||
}
|
||||
|
||||
pub struct CBufferIter<T> {
|
||||
ptr: *mut T,
|
||||
count: usize,
|
||||
i: usize,
|
||||
}
|
||||
|
||||
impl<T> CBufferIter<T> {
|
||||
pub unsafe fn new(ptr: *mut T, count: usize) -> Self {
|
||||
Self { ptr, count, i: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Copy> Iterator for CBufferIter<T> {
|
||||
type Item = T;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let i = self.i;
|
||||
self.i += 1;
|
||||
if i >= self.count {
|
||||
None
|
||||
} else {
|
||||
Some(unsafe { *self.ptr.offset(i as isize) })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Drop for CBufferIter<T> {
|
||||
fn drop(&mut self) {
|
||||
unsafe { free_ptr(self.ptr as *mut c_void); }
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue