Skip to main content

jellybean_tree_sitter/
ffi.rs

1#![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    /// Reconstructs a [`Language`] from a raw pointer.
22    ///
23    /// # Safety
24    ///
25    /// `ptr` must be non-null.
26    pub unsafe fn from_raw(ptr: *const TSLanguage) -> Language {
27        Language(ptr)
28    }
29
30    /// Consumes the [`Language`], returning a raw pointer to the underlying C structure.
31    pub fn into_raw(self) -> *const TSLanguage {
32        ManuallyDrop::new(self).0
33    }
34}
35
36impl Parser {
37    /// Reconstructs a [`Parser`] from a raw pointer.
38    ///
39    /// # Safety
40    ///
41    /// `ptr` must be non-null.
42    pub unsafe fn from_raw(ptr: *mut TSParser) -> Parser {
43        Parser(NonNull::new_unchecked(ptr))
44    }
45
46    /// Consumes the [`Parser`], returning a raw pointer to the underlying C structure.
47    ///
48    /// # Safety
49    ///
50    /// It's a caller responsibility to adjust parser's state
51    /// like disable logging or dot graphs printing if this
52    /// may cause issues like use after free.
53    pub fn into_raw(self) -> *mut TSParser {
54        ManuallyDrop::new(self).0.as_ptr()
55    }
56}
57
58impl Tree {
59    /// Reconstructs a [`Tree`] from a raw pointer.
60    ///
61    /// # Safety
62    ///
63    /// `ptr` must be non-null.
64    pub unsafe fn from_raw(ptr: *mut TSTree) -> Tree {
65        Tree(NonNull::new_unchecked(ptr))
66    }
67
68    /// Consumes the [`Tree`], returning a raw pointer to the underlying C structure.
69    pub fn into_raw(self) -> *mut TSTree {
70        ManuallyDrop::new(self).0.as_ptr()
71    }
72}
73
74impl<'tree> Node<'tree> {
75    /// Reconstructs a [`Node`] from a raw pointer.
76    ///
77    /// # Safety
78    ///
79    /// `ptr` must be non-null.
80    pub unsafe fn from_raw(raw: TSNode) -> Node<'tree> {
81        Node(raw, PhantomData)
82    }
83
84    /// Consumes the [`Node`], returning a raw pointer to the underlying C structure.
85    pub fn into_raw(self) -> TSNode {
86        ManuallyDrop::new(self).0
87    }
88}
89
90impl<'a> TreeCursor<'a> {
91    /// Reconstructs a [`TreeCursor`] from a raw pointer.
92    ///
93    /// # Safety
94    ///
95    /// `ptr` must be non-null.
96    pub unsafe fn from_raw(raw: TSTreeCursor) -> TreeCursor<'a> {
97        TreeCursor(raw, PhantomData)
98    }
99
100    /// Consumes the [`TreeCursor`], returning a raw pointer to the underlying C structure.
101    pub fn into_raw(self) -> TSTreeCursor {
102        ManuallyDrop::new(self).0
103    }
104}
105
106impl Query {
107    /// Reconstructs a [`Query`] from a raw pointer.
108    ///
109    /// # Safety
110    ///
111    /// `ptr` must be non-null.
112    pub unsafe fn from_raw(ptr: *mut TSQuery, source: &str) -> Result<Query, QueryError> {
113        Query::from_raw_parts(ptr, source)
114    }
115
116    /// Consumes the [`Query`], returning a raw pointer to the underlying C structure.
117    pub fn into_raw(self) -> *mut TSQuery {
118        ManuallyDrop::new(self).ptr.as_ptr()
119    }
120}
121
122impl QueryCursor {
123    /// Reconstructs a [`QueryCursor`] from a raw pointer.
124    ///
125    /// # Safety
126    ///
127    /// `ptr` must be non-null.
128    pub unsafe fn from_raw(ptr: *mut TSQueryCursor) -> QueryCursor {
129        QueryCursor {
130            ptr: NonNull::new_unchecked(ptr),
131        }
132    }
133
134    /// Consumes the [`QueryCursor`], returning a raw pointer to the underlying C structure.
135    pub fn into_raw(self) -> *mut TSQueryCursor {
136        ManuallyDrop::new(self).ptr.as_ptr()
137    }
138}
139
140impl LookaheadIterator {
141    /// Reconstructs a [`LookaheadIterator`] from a raw pointer.
142    ///
143    /// # Safety
144    ///
145    /// `ptr` must be non-null.
146    pub unsafe fn from_raw(ptr: *mut TSLookaheadIterator) -> LookaheadIterator {
147        LookaheadIterator(NonNull::new_unchecked(ptr))
148    }
149
150    /// Consumes the [`LookaheadIterator`], returning a raw pointer to the underlying C structure.
151    pub fn into_raw(self) -> *mut TSLookaheadIterator {
152        ManuallyDrop::new(self).0.as_ptr()
153    }
154}