tree_sitter/
ffi.rs

1#![allow(dead_code)]
2#![allow(non_upper_case_globals)]
3#![allow(non_camel_case_types)]
4#![allow(clippy::missing_const_for_fn)]
5
6#[cfg(feature = "bindgen")]
7include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
8
9#[cfg(not(feature = "bindgen"))]
10include!("./bindings.rs");
11
12#[cfg(unix)]
13#[cfg(feature = "std")]
14extern "C" {
15    pub(crate) fn _ts_dup(fd: std::os::raw::c_int) -> std::os::raw::c_int;
16}
17
18#[cfg(windows)]
19#[cfg(feature = "std")]
20extern "C" {
21    pub(crate) fn _ts_dup(handle: *mut std::os::raw::c_void) -> std::os::raw::c_int;
22}
23
24use core::{marker::PhantomData, mem::ManuallyDrop, ptr::NonNull, str};
25
26use crate::{
27    Language, LookaheadIterator, Node, ParseState, Parser, Query, QueryCursor, QueryCursorState,
28    QueryError, Tree, TreeCursor,
29};
30
31impl Language {
32    /// Reconstructs a [`Language`] from a raw pointer.
33    ///
34    /// # Safety
35    ///
36    /// `ptr` must be non-null.
37    #[must_use]
38    pub const unsafe fn from_raw(ptr: *const TSLanguage) -> Self {
39        Self(ptr)
40    }
41
42    /// Consumes the [`Language`], returning a raw pointer to the underlying C structure.
43    #[must_use]
44    pub fn into_raw(self) -> *const TSLanguage {
45        ManuallyDrop::new(self).0
46    }
47}
48
49impl Parser {
50    /// Reconstructs a [`Parser`] from a raw pointer.
51    ///
52    /// # Safety
53    ///
54    /// `ptr` must be non-null.
55    #[must_use]
56    pub const unsafe fn from_raw(ptr: *mut TSParser) -> Self {
57        Self(NonNull::new_unchecked(ptr))
58    }
59
60    /// Consumes the [`Parser`], returning a raw pointer to the underlying C structure.
61    ///
62    /// # Safety
63    ///
64    /// It's a caller responsibility to adjust parser's state
65    /// like disable logging or dot graphs printing if this
66    /// may cause issues like use after free.
67    #[must_use]
68    pub fn into_raw(self) -> *mut TSParser {
69        ManuallyDrop::new(self).0.as_ptr()
70    }
71}
72
73impl ParseState {
74    /// Reconstructs a [`ParseState`] from a raw pointer
75    ///
76    /// # Safety
77    ///
78    /// `ptr` must be non-null.
79    #[must_use]
80    pub const unsafe fn from_raw(ptr: *mut TSParseState) -> Self {
81        Self(NonNull::new_unchecked(ptr))
82    }
83
84    /// Consumes the [`ParseState`], returning a raw pointer to the underlying C structure.
85    #[must_use]
86    pub fn into_raw(self) -> *mut TSParseState {
87        ManuallyDrop::new(self).0.as_ptr()
88    }
89}
90
91impl Tree {
92    /// Reconstructs a [`Tree`] from a raw pointer.
93    ///
94    /// # Safety
95    ///
96    /// `ptr` must be non-null.
97    #[must_use]
98    pub const unsafe fn from_raw(ptr: *mut TSTree) -> Self {
99        Self(NonNull::new_unchecked(ptr))
100    }
101
102    /// Consumes the [`Tree`], returning a raw pointer to the underlying C structure.
103    #[must_use]
104    pub fn into_raw(self) -> *mut TSTree {
105        ManuallyDrop::new(self).0.as_ptr()
106    }
107}
108
109impl Node<'_> {
110    /// Reconstructs a [`Node`] from a raw pointer.
111    ///
112    /// # Safety
113    ///
114    /// `ptr` must be non-null.
115    #[must_use]
116    pub const unsafe fn from_raw(raw: TSNode) -> Self {
117        Self(raw, PhantomData)
118    }
119
120    /// Consumes the [`Node`], returning a raw pointer to the underlying C structure.
121    #[must_use]
122    pub fn into_raw(self) -> TSNode {
123        ManuallyDrop::new(self).0
124    }
125}
126
127impl TreeCursor<'_> {
128    /// Reconstructs a [`TreeCursor`] from a raw pointer.
129    ///
130    /// # Safety
131    ///
132    /// `ptr` must be non-null.
133    #[must_use]
134    pub const unsafe fn from_raw(raw: TSTreeCursor) -> Self {
135        Self(raw, PhantomData)
136    }
137
138    /// Consumes the [`TreeCursor`], returning a raw pointer to the underlying C structure.
139    #[must_use]
140    pub fn into_raw(self) -> TSTreeCursor {
141        ManuallyDrop::new(self).0
142    }
143}
144
145impl Query {
146    /// Reconstructs a [`Query`] from a raw pointer.
147    ///
148    /// # Safety
149    ///
150    /// `ptr` must be non-null.
151    pub unsafe fn from_raw(ptr: *mut TSQuery, source: &str) -> Result<Self, QueryError> {
152        Self::from_raw_parts(ptr, source)
153    }
154
155    /// Consumes the [`Query`], returning a raw pointer to the underlying C structure.
156    #[must_use]
157    pub fn into_raw(self) -> *mut TSQuery {
158        ManuallyDrop::new(self).ptr.as_ptr()
159    }
160}
161
162impl QueryCursor {
163    /// Reconstructs a [`QueryCursor`] from a raw pointer.
164    ///
165    /// # Safety
166    ///
167    /// `ptr` must be non-null.
168    #[must_use]
169    pub const unsafe fn from_raw(ptr: *mut TSQueryCursor) -> Self {
170        Self {
171            ptr: NonNull::new_unchecked(ptr),
172        }
173    }
174
175    /// Consumes the [`QueryCursor`], returning a raw pointer to the underlying C structure.
176    #[must_use]
177    pub fn into_raw(self) -> *mut TSQueryCursor {
178        ManuallyDrop::new(self).ptr.as_ptr()
179    }
180}
181
182impl QueryCursorState {
183    /// Reconstructs a [`QueryCursorState`] from a raw pointer.
184    ///
185    /// # Safety
186    ///
187    /// `ptr` must be non-null.
188    #[must_use]
189    pub const unsafe fn from_raw(ptr: *mut TSQueryCursorState) -> Self {
190        Self(NonNull::new_unchecked(ptr))
191    }
192
193    /// Consumes the [`QueryCursorState`], returning a raw pointer to the underlying C structure.
194    #[must_use]
195    pub fn into_raw(self) -> *mut TSQueryCursorState {
196        ManuallyDrop::new(self).0.as_ptr()
197    }
198}
199
200impl LookaheadIterator {
201    /// Reconstructs a [`LookaheadIterator`] from a raw pointer.
202    ///
203    /// # Safety
204    ///
205    /// `ptr` must be non-null.
206    #[must_use]
207    pub const unsafe fn from_raw(ptr: *mut TSLookaheadIterator) -> Self {
208        Self(NonNull::new_unchecked(ptr))
209    }
210
211    /// Consumes the [`LookaheadIterator`], returning a raw pointer to the underlying C structure.
212    #[must_use]
213    pub fn into_raw(self) -> *mut TSLookaheadIterator {
214        ManuallyDrop::new(self).0.as_ptr()
215    }
216}