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 #[must_use]
42 pub const unsafe fn from_raw(ptr: *const TSLanguage) -> Self {
43 Self(ptr)
44 }
45
46 #[must_use]
48 pub fn into_raw(self) -> *const TSLanguage {
49 ManuallyDrop::new(self).0
50 }
51}
52
53impl Parser {
54 #[must_use]
60 pub const unsafe fn from_raw(ptr: *mut TSParser) -> Self {
61 Self(unsafe { NonNull::new_unchecked(ptr) })
62 }
63
64 #[must_use]
72 pub fn into_raw(self) -> *mut TSParser {
73 ManuallyDrop::new(self).0.as_ptr()
74 }
75}
76
77impl ParseState {
78 #[must_use]
84 pub const unsafe fn from_raw(ptr: *mut TSParseState) -> Self {
85 Self(unsafe { NonNull::new_unchecked(ptr) })
86 }
87
88 #[must_use]
90 pub fn into_raw(self) -> *mut TSParseState {
91 ManuallyDrop::new(self).0.as_ptr()
92 }
93}
94
95impl Tree {
96 #[must_use]
102 pub const unsafe fn from_raw(ptr: *mut TSTree) -> Self {
103 Self(unsafe { NonNull::new_unchecked(ptr) })
104 }
105
106 #[must_use]
108 pub fn into_raw(self) -> *mut TSTree {
109 ManuallyDrop::new(self).0.as_ptr()
110 }
111}
112
113impl Node<'_> {
114 #[must_use]
120 pub const unsafe fn from_raw(raw: TSNode) -> Self {
121 Self(raw, PhantomData)
122 }
123
124 #[must_use]
126 pub fn into_raw(self) -> TSNode {
127 ManuallyDrop::new(self).0
128 }
129}
130
131impl TreeCursor<'_> {
132 #[must_use]
138 pub const unsafe fn from_raw(raw: TSTreeCursor) -> Self {
139 Self(raw, PhantomData)
140 }
141
142 #[must_use]
144 pub fn into_raw(self) -> TSTreeCursor {
145 ManuallyDrop::new(self).0
146 }
147}
148
149impl Query {
150 pub unsafe fn from_raw(ptr: *mut TSQuery, source: &str) -> Result<Self, QueryError> {
156 unsafe { Self::from_raw_parts(ptr, source) }
157 }
158
159 #[must_use]
161 pub fn into_raw(self) -> *mut TSQuery {
162 ManuallyDrop::new(self).ptr.as_ptr()
163 }
164}
165
166impl QueryCursor {
167 #[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 #[must_use]
181 pub fn into_raw(self) -> *mut TSQueryCursor {
182 ManuallyDrop::new(self).ptr.as_ptr()
183 }
184}
185
186impl QueryCursorState {
187 #[must_use]
193 pub const unsafe fn from_raw(ptr: *mut TSQueryCursorState) -> Self {
194 Self(unsafe { NonNull::new_unchecked(ptr) })
195 }
196
197 #[must_use]
199 pub fn into_raw(self) -> *mut TSQueryCursorState {
200 ManuallyDrop::new(self).0.as_ptr()
201 }
202}
203
204impl LookaheadIterator {
205 #[must_use]
211 pub const unsafe fn from_raw(ptr: *mut TSLookaheadIterator) -> Self {
212 Self(unsafe { NonNull::new_unchecked(ptr) })
213 }
214
215 #[must_use]
217 pub fn into_raw(self) -> *mut TSLookaheadIterator {
218 ManuallyDrop::new(self).0.as_ptr()
219 }
220}