diff --git a/lib/binding_rust/ffi.rs b/lib/binding_rust/ffi.rs index 685ed765..9f900c35 100644 --- a/lib/binding_rust/ffi.rs +++ b/lib/binding_rust/ffi.rs @@ -7,3 +7,58 @@ include!("./bindings.rs"); extern "C" { pub(crate) fn dup(fd: std::os::raw::c_int) -> std::os::raw::c_int; } + +use crate::{Language, Node, Parser, Query, QueryCursor, Tree, TreeCursor}; +use std::mem::ManuallyDrop; + +impl Language { + /// Consumes the [Language], returning a raw pointer to the underlying C structure. + pub fn into_raw(self) -> *const TSLanguage { + ManuallyDrop::new(self).0 + } +} + +impl Parser { + /// Consumes the [Parser], returning a raw pointer to the underlying C structure. + pub fn into_raw(mut self) -> *mut TSParser { + self.stop_printing_dot_graphs(); + self.set_logger(None); + + ManuallyDrop::new(self).0.as_ptr() + } +} + +impl Tree { + /// Consumes the [Tree], returning a raw pointer to the underlying C structure. + pub fn into_raw(self) -> *mut TSTree { + ManuallyDrop::new(self).0.as_ptr() + } +} + +impl<'tree> Node<'tree> { + /// Consumes the [Node], returning a raw pointer to the underlying C structure. + pub fn into_raw(self) -> *mut TSNode { + &mut ManuallyDrop::new(self).0 + } +} + +impl<'a> TreeCursor<'a> { + /// Consumes the [TreeCursor], returning a raw pointer to the underlying C structure. + pub fn into_raw(self) -> *mut TSTreeCursor { + &mut ManuallyDrop::new(self).0 + } +} + +impl Query { + /// Consumes the [Query], returning a raw pointer to the underlying C structure. + pub fn into_raw(self) -> *mut TSQuery { + ManuallyDrop::new(self).ptr.as_ptr() + } +} + +impl QueryCursor { + /// Consumes the [QueryCursor], returning a raw pointer to the underlying C structure. + pub fn into_raw(self) -> *mut TSQueryCursor { + ManuallyDrop::new(self).ptr.as_ptr() + } +} diff --git a/lib/binding_rust/lib.rs b/lib/binding_rust/lib.rs index 16e3cd4b..3746bcdd 100644 --- a/lib/binding_rust/lib.rs +++ b/lib/binding_rust/lib.rs @@ -9,7 +9,7 @@ use std::{ ffi::CStr, fmt, hash, iter, marker::PhantomData, - mem::{ManuallyDrop, MaybeUninit}, + mem::MaybeUninit, ops, os::raw::{c_char, c_void}, ptr::{self, NonNull}, @@ -334,11 +334,6 @@ impl Language { Some(id) } } - - /// Consumes the [Language], returning a raw pointer to the underlying C structure. - pub fn into_raw(self) -> *const ffi::TSLanguage { - ManuallyDrop::new(self).0 - } } impl Parser { @@ -696,14 +691,6 @@ 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 { @@ -798,11 +785,6 @@ 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 { @@ -1208,11 +1190,6 @@ 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> { @@ -1347,11 +1324,6 @@ 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> { @@ -1847,11 +1819,6 @@ 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 { @@ -1959,11 +1926,6 @@ 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> {