Skip to main content

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