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