-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlib.rs
More file actions
80 lines (73 loc) · 2.43 KB
/
lib.rs
File metadata and controls
80 lines (73 loc) · 2.43 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/// Helper macro to generate all required functions for an iterator. We use iterators to go over any collection of data
/// that exists on the Rust side (e.g.: definitions, documents, declarations, references). The goal is to avoid eager
/// allocation of large collections when possible.
///
/// Note: structs must be defined manually so that the cbindgen can see them. The actual methods are not extern "C" and
/// so they can be expanded from the macro.
///
/// # Example
///
/// ```ignore
/// pub struct FoosIter {
/// entries: Box<[Foo]>,
/// index: usize,
/// }
///
/// iterator!(FoosIter, entries: Foo);
/// ```
macro_rules! iterator {
($name:ident, $field:ident : $entry:ty) => {
impl $name {
#[must_use]
pub fn new($field: Box<[$entry]>) -> *mut $name {
Box::into_raw(Box::new($name { $field, index: 0 }))
}
/// # Safety
/// `iter` must be a valid pointer returned by `new`, or null.
pub unsafe fn len(iter: *const Self) -> usize {
if iter.is_null() {
return 0;
}
unsafe { (&*iter).$field.len() }
}
/// # Safety
/// - `iter` must be a valid pointer returned by `new`, or null.
/// - `out` must be a valid, writable pointer, or null.
pub unsafe fn next(iter: *mut Self, out: *mut $entry) -> bool {
if iter.is_null() || out.is_null() {
return false;
}
let it = unsafe { &mut *iter };
if it.index >= it.$field.len() {
return false;
}
let entry = it.$field[it.index];
it.index += 1;
unsafe {
*out = entry;
}
true
}
/// # Safety
/// `iter` must be a pointer returned by `new` (or null). Must not be used after.
pub unsafe fn free(iter: *mut Self) {
if iter.is_null() {
return;
}
unsafe {
let _ = Box::from_raw(iter);
}
}
}
};
}
pub mod declaration_api;
pub mod definition_api;
pub mod diagnostic_api;
pub mod document_api;
pub mod graph_api;
pub mod location_api;
pub mod name_api;
pub mod reference_api;
pub mod signature_api;
pub mod utils;