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 #[must_use]
39 pub const unsafe fn from_raw(ptr: *const TSLanguage) -> Self {
40 Self(ptr)
41 }
42
43 #[must_use]
45 pub fn into_raw(self) -> *const TSLanguage {
46 ManuallyDrop::new(self).0
47 }
48}
49
50impl Parser {
51 #[must_use]
57 pub const unsafe fn from_raw(ptr: *mut TSParser) -> Self {
58 Self(unsafe { NonNull::new_unchecked(ptr) })
59 }
60
61 #[must_use]
69 pub fn into_raw(self) -> *mut TSParser {
70 ManuallyDrop::new(self).0.as_ptr()
71 }
72}
73
74impl ParseState {
75 #[must_use]
81 pub const unsafe fn from_raw(ptr: *mut TSParseState) -> Self {
82 Self(unsafe { NonNull::new_unchecked(ptr) })
83 }
84
85 #[must_use]
87 pub fn into_raw(self) -> *mut TSParseState {
88 ManuallyDrop::new(self).0.as_ptr()
89 }
90}
91
92impl Tree {
93 #[must_use]
99 pub const unsafe fn from_raw(ptr: *mut TSTree) -> Self {
100 Self(unsafe { NonNull::new_unchecked(ptr) })
101 }
102
103 #[must_use]
105 pub fn into_raw(self) -> *mut TSTree {
106 ManuallyDrop::new(self).0.as_ptr()
107 }
108}
109
110impl Node<'_> {
111 #[must_use]
117 pub const unsafe fn from_raw(raw: TSNode) -> Self {
118 Self(raw, PhantomData)
119 }
120
121 #[must_use]
123 pub fn into_raw(self) -> TSNode {
124 ManuallyDrop::new(self).0
125 }
126}
127
128impl TreeCursor<'_> {
129 #[must_use]
135 pub const unsafe fn from_raw(raw: TSTreeCursor) -> Self {
136 Self(raw, PhantomData)
137 }
138
139 #[must_use]
141 pub fn into_raw(self) -> TSTreeCursor {
142 ManuallyDrop::new(self).0
143 }
144}
145
146impl LookaheadIterator {
147 #[must_use]
153 pub const unsafe fn from_raw(ptr: *mut TSLookaheadIterator) -> Self {
154 Self(unsafe { NonNull::new_unchecked(ptr) })
155 }
156
157 #[must_use]
159 pub fn into_raw(self) -> *mut TSLookaheadIterator {
160 ManuallyDrop::new(self).0.as_ptr()
161 }
162}