jellybean_tree_sitter/
ffi.rs1#![allow(dead_code)]
2#![allow(non_upper_case_globals)]
3#![allow(non_camel_case_types)]
4
5#[cfg(feature = "bindgen")]
6include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
7
8#[cfg(not(feature = "bindgen"))]
9include!("./bindings.rs");
10
11extern "C" {
12 pub(crate) fn dup(fd: std::os::raw::c_int) -> std::os::raw::c_int;
13}
14
15use crate::{
16 Language, LookaheadIterator, Node, Parser, Query, QueryCursor, QueryError, Tree, TreeCursor,
17};
18use std::{marker::PhantomData, mem::ManuallyDrop, ptr::NonNull, str};
19
20impl Language {
21 pub unsafe fn from_raw(ptr: *const TSLanguage) -> Language {
27 Language(ptr)
28 }
29
30 pub fn into_raw(self) -> *const TSLanguage {
32 ManuallyDrop::new(self).0
33 }
34}
35
36impl Parser {
37 pub unsafe fn from_raw(ptr: *mut TSParser) -> Parser {
43 Parser(NonNull::new_unchecked(ptr))
44 }
45
46 pub fn into_raw(self) -> *mut TSParser {
54 ManuallyDrop::new(self).0.as_ptr()
55 }
56}
57
58impl Tree {
59 pub unsafe fn from_raw(ptr: *mut TSTree) -> Tree {
65 Tree(NonNull::new_unchecked(ptr))
66 }
67
68 pub fn into_raw(self) -> *mut TSTree {
70 ManuallyDrop::new(self).0.as_ptr()
71 }
72}
73
74impl<'tree> Node<'tree> {
75 pub unsafe fn from_raw(raw: TSNode) -> Node<'tree> {
81 Node(raw, PhantomData)
82 }
83
84 pub fn into_raw(self) -> TSNode {
86 ManuallyDrop::new(self).0
87 }
88}
89
90impl<'a> TreeCursor<'a> {
91 pub unsafe fn from_raw(raw: TSTreeCursor) -> TreeCursor<'a> {
97 TreeCursor(raw, PhantomData)
98 }
99
100 pub fn into_raw(self) -> TSTreeCursor {
102 ManuallyDrop::new(self).0
103 }
104}
105
106impl Query {
107 pub unsafe fn from_raw(ptr: *mut TSQuery, source: &str) -> Result<Query, QueryError> {
113 Query::from_raw_parts(ptr, source)
114 }
115
116 pub fn into_raw(self) -> *mut TSQuery {
118 ManuallyDrop::new(self).ptr.as_ptr()
119 }
120}
121
122impl QueryCursor {
123 pub unsafe fn from_raw(ptr: *mut TSQueryCursor) -> QueryCursor {
129 QueryCursor {
130 ptr: NonNull::new_unchecked(ptr),
131 }
132 }
133
134 pub fn into_raw(self) -> *mut TSQueryCursor {
136 ManuallyDrop::new(self).ptr.as_ptr()
137 }
138}
139
140impl LookaheadIterator {
141 pub unsafe fn from_raw(ptr: *mut TSLookaheadIterator) -> LookaheadIterator {
147 LookaheadIterator(NonNull::new_unchecked(ptr))
148 }
149
150 pub fn into_raw(self) -> *mut TSLookaheadIterator {
152 ManuallyDrop::new(self).0.as_ptr()
153 }
154}