forked from tree-sitter/tree-sitter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.rs
44 lines (37 loc) · 971 Bytes
/
util.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use super::FREE_FN;
use std::os::raw::c_void;
/// A raw pointer and a length, exposed as an iterator.
pub struct CBufferIter<T> {
ptr: *mut T,
count: usize,
i: usize,
}
impl<T> CBufferIter<T> {
pub const 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;
if i >= self.count {
None
} else {
self.i += 1;
Some(unsafe { *self.ptr.add(i) })
}
}
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) {
if !self.ptr.is_null() {
unsafe { (FREE_FN)(self.ptr.cast::<c_void>()) };
}
}
}