From 3d396e120b6cd574cdfbaf72a6cd0d14ae79bf38 Mon Sep 17 00:00:00 2001 From: Vhyrro Date: Mon, 13 Mar 2023 18:52:39 +0100 Subject: [PATCH] feat(rust_bindings): provide `into_raw()` functions for treesitter structs --- lib/binding_rust/lib.rs | 42 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/lib/binding_rust/lib.rs b/lib/binding_rust/lib.rs index 579bf8e2..568bad25 100644 --- a/lib/binding_rust/lib.rs +++ b/lib/binding_rust/lib.rs @@ -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> {