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 #[must_use]
38 pub const unsafe fn from_raw(ptr: *const TSLanguage) -> Self {
39 Self(ptr)
40 }
41
42 #[must_use]
44 pub fn into_raw(self) -> *const TSLanguage {
45 ManuallyDrop::new(self).0
46 }
47}
48
49impl Parser {
50 #[must_use]
56 pub const unsafe fn from_raw(ptr: *mut TSParser) -> Self {
57 Self(NonNull::new_unchecked(ptr))
58 }
59
60 #[must_use]
68 pub fn into_raw(self) -> *mut TSParser {
69 ManuallyDrop::new(self).0.as_ptr()
70 }
71}
72
73impl ParseState {
74 #[must_use]
80 pub const unsafe fn from_raw(ptr: *mut TSParseState) -> Self {
81 Self(NonNull::new_unchecked(ptr))
82 }
83
84 #[must_use]
86 pub fn into_raw(self) -> *mut TSParseState {
87 ManuallyDrop::new(self).0.as_ptr()
88 }
89}
90
91impl Tree {
92 #[must_use]
98 pub const unsafe fn from_raw(ptr: *mut TSTree) -> Self {
99 Self(NonNull::new_unchecked(ptr))
100 }
101
102 #[must_use]
104 pub fn into_raw(self) -> *mut TSTree {
105 ManuallyDrop::new(self).0.as_ptr()
106 }
107}
108
109impl Node<'_> {
110 #[must_use]
116 pub const unsafe fn from_raw(raw: TSNode) -> Self {
117 Self(raw, PhantomData)
118 }
119
120 #[must_use]
122 pub fn into_raw(self) -> TSNode {
123 ManuallyDrop::new(self).0
124 }
125}
126
127impl TreeCursor<'_> {
128 #[must_use]
134 pub const unsafe fn from_raw(raw: TSTreeCursor) -> Self {
135 Self(raw, PhantomData)
136 }
137
138 #[must_use]
140 pub fn into_raw(self) -> TSTreeCursor {
141 ManuallyDrop::new(self).0
142 }
143}
144
145impl Query {
146 pub unsafe fn from_raw(ptr: *mut TSQuery, source: &str) -> Result<Self, QueryError> {
152 Self::from_raw_parts(ptr, source)
153 }
154
155 #[must_use]
157 pub fn into_raw(self) -> *mut TSQuery {
158 ManuallyDrop::new(self).ptr.as_ptr()
159 }
160}
161
162impl QueryCursor {
163 #[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 #[must_use]
177 pub fn into_raw(self) -> *mut TSQueryCursor {
178 ManuallyDrop::new(self).ptr.as_ptr()
179 }
180}
181
182impl QueryCursorState {
183 #[must_use]
189 pub const unsafe fn from_raw(ptr: *mut TSQueryCursorState) -> Self {
190 Self(NonNull::new_unchecked(ptr))
191 }
192
193 #[must_use]
195 pub fn into_raw(self) -> *mut TSQueryCursorState {
196 ManuallyDrop::new(self).0.as_ptr()
197 }
198}
199
200impl LookaheadIterator {
201 #[must_use]
207 pub const unsafe fn from_raw(ptr: *mut TSLookaheadIterator) -> Self {
208 Self(NonNull::new_unchecked(ptr))
209 }
210
211 #[must_use]
213 pub fn into_raw(self) -> *mut TSLookaheadIterator {
214 ManuallyDrop::new(self).0.as_ptr()
215 }
216}