feat(rust_bindings): provide into_raw() functions for treesitter structs

This commit is contained in:
Vhyrro 2023-03-13 18:52:39 +01:00 committed by Andrew Hlynskyi
parent 342527ebbd
commit 3d396e120b

View file

@ -1,4 +1,4 @@
mod ffi;
pub mod ffi;
mod util;
#[cfg(unix)]
@ -9,7 +9,7 @@ use std::{
ffi::CStr,
fmt, hash, iter,
marker::PhantomData,
mem::MaybeUninit,
mem::{ManuallyDrop, MaybeUninit},
ops,
os::raw::{c_char, c_void},
ptr::{self, NonNull},
@ -334,6 +334,11 @@ impl Language {
Some(id)
}
}
/// Consumes the [Language], returning a raw pointer to the underlying C structure.
pub fn into_raw(self) -> *const ffi::TSLanguage {
self.0
}
}
impl Parser {
@ -691,6 +696,14 @@ impl Parser {
ffi::ts_parser_set_cancellation_flag(self.0.as_ptr(), ptr::null());
}
}
/// Consumes the [Parser], returning a raw pointer to the underlying C structure.
pub fn into_raw(mut self) -> *mut ffi::TSParser {
self.stop_printing_dot_graphs();
self.set_logger(None);
ManuallyDrop::new(self).0.as_ptr()
}
}
impl Drop for Parser {
@ -785,6 +798,11 @@ impl Tree {
let fd = file.as_raw_fd();
unsafe { ffi::ts_tree_print_dot_graph(self.0.as_ptr(), fd) }
}
/// Consumes the [Tree], returning a raw pointer to the underlying C structure.
pub fn into_raw(self) -> *mut ffi::TSTree {
ManuallyDrop::new(self).0.as_ptr()
}
}
impl fmt::Debug for Tree {
@ -1190,6 +1208,11 @@ impl<'tree> Node<'tree> {
let edit = edit.into();
unsafe { ffi::ts_node_edit(&mut self.0 as *mut ffi::TSNode, &edit) }
}
/// Consumes the [Node], returning a raw pointer to the underlying C structure.
pub fn into_raw(self) -> *mut ffi::TSNode {
&mut ManuallyDrop::new(self).0
}
}
impl<'a> PartialEq for Node<'a> {
@ -1324,6 +1347,11 @@ impl<'a> TreeCursor<'a> {
pub fn reset(&mut self, node: Node<'a>) {
unsafe { ffi::ts_tree_cursor_reset(&mut self.0, node.0) };
}
/// Consumes the [TreeCursor], returning a raw pointer to the underlying C structure.
pub fn into_raw(self) -> *mut ffi::TSTreeCursor {
&mut ManuallyDrop::new(self).0
}
}
impl<'a> Clone for TreeCursor<'a> {
@ -1819,6 +1847,11 @@ impl Query {
));
}
}
/// Consumes the [Query], returning a raw pointer to the underlying C structure.
pub fn into_raw(self) -> *mut ffi::TSQuery {
ManuallyDrop::new(self).ptr.as_ptr()
}
}
impl QueryCursor {
@ -1926,6 +1959,11 @@ impl QueryCursor {
}
self
}
/// Consumes the [QueryCursor], returning a raw pointer to the underlying C structure.
pub fn into_raw(self) -> *mut ffi::TSQueryCursor {
ManuallyDrop::new(self).ptr.as_ptr()
}
}
impl<'a, 'tree> QueryMatch<'a, 'tree> {