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