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