diff --git a/src/context.rs b/src/context.rs index 9cc6f85..748692c 100644 --- a/src/context.rs +++ b/src/context.rs @@ -9,6 +9,7 @@ use bitflags::bitflags; use std::collections::HashMap; use std::ffi::CString; +use std::mem::ManuallyDrop; use std::os::raw::{c_char, c_void}; use std::path::Path; use std::slice; @@ -126,6 +127,12 @@ impl Context { Ok(Context { raw: context }) } + /// Returns a mutable raw pointer to the underlying C library representation + /// of the libyang context. + pub fn into_raw(self) -> *mut ffi::ly_ctx { + ManuallyDrop::new(self).raw + } + /// Add the search path into libyang context. pub fn set_searchdir>( &mut self, diff --git a/src/data.rs b/src/data.rs index 2d418f8..4316aa0 100644 --- a/src/data.rs +++ b/src/data.rs @@ -10,6 +10,7 @@ use bitflags::bitflags; use core::ffi::{c_char, c_void}; use std::ffi::CStr; use std::ffi::CString; +use std::mem::ManuallyDrop; use std::slice; use crate::context::Context; @@ -413,6 +414,12 @@ impl<'a> DataTree<'a> { } } + /// Returns a mutable raw pointer to the underlying C library representation + /// of the root node of the YANG data tree. + pub fn into_raw(self) -> *mut ffi::lyd_node { + ManuallyDrop::new(self).raw + } + unsafe fn reroot(&mut self, raw: *mut ffi::lyd_node) { if self.raw.is_null() { let mut dnode = DataNodeRef::from_raw(self, raw); @@ -1067,6 +1074,12 @@ unsafe impl Sync for DataTreeOwningRef<'_> {} // ===== impl DataNodeRef ===== impl<'a> DataNodeRef<'a> { + /// Returns a mutable raw pointer to the underlying C library representation + /// of the data node reference. + pub fn as_raw(&self) -> *mut ffi::lyd_node { + self.raw + } + /// Schema definition of this node. pub fn schema(&self) -> SchemaNode<'_> { let raw = unsafe { (*self.raw).schema }; @@ -1264,7 +1277,7 @@ impl<'a> DataNodeRef<'a> { ffi::lyd_new_inner( self.raw(), module - .map(|module| module.raw()) + .map(|module| module.as_raw()) .unwrap_or(std::ptr::null_mut()), name_cstr.as_ptr(), 0, @@ -1301,7 +1314,7 @@ impl<'a> DataNodeRef<'a> { ffi::lyd_new_list2( self.raw(), module - .map(|module| module.raw()) + .map(|module| module.as_raw()) .unwrap_or(std::ptr::null_mut()), name_cstr.as_ptr(), keys_cstr.as_ptr(), @@ -1346,7 +1359,7 @@ impl<'a> DataNodeRef<'a> { ffi::lyd_new_list3( self.raw(), module - .map(|module| module.raw()) + .map(|module| module.as_raw()) .unwrap_or(std::ptr::null_mut()), name_cstr.as_ptr(), keys.as_mut_ptr(), @@ -1385,7 +1398,7 @@ impl<'a> DataNodeRef<'a> { ffi::lyd_new_term( self.raw(), module - .map(|module| module.raw()) + .map(|module| module.as_raw()) .unwrap_or(std::ptr::null_mut()), name_cstr.as_ptr(), value_ptr, @@ -1479,6 +1492,12 @@ unsafe impl Sync for DataNodeRef<'_> {} // ===== impl Metadata ===== impl<'a> Metadata<'a> { + /// Returns a mutable raw pointer to the underlying C library representation + /// of the data element metadata. + pub fn as_raw(&self) -> *mut ffi::lyd_meta { + self.raw + } + /// Metadata name. pub fn name(&self) -> &str { char_ptr_to_str(unsafe { (*self.raw).name }) diff --git a/src/schema.rs b/src/schema.rs index 7d85c91..283e6ac 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -179,7 +179,7 @@ pub enum DataValue { impl<'a> SchemaModule<'a> { /// Returns a mutable raw pointer to the underlying C library representation /// of the module. - pub(crate) fn raw(&self) -> *mut ffi::lys_module { + pub fn as_raw(&self) -> *mut ffi::lys_module { self.raw } @@ -465,6 +465,12 @@ unsafe impl Sync for SchemaModule<'_> {} // ===== impl SchemaSubmodule ===== impl SchemaSubmodule<'_> { + /// Returns a mutable raw pointer to the underlying C library representation + /// of the sub-module. + pub fn as_raw(&self) -> *mut ffi::lysp_submodule { + self.raw + } + /// Print schema tree in the specified format into a string. pub fn print_string( &self, @@ -520,6 +526,12 @@ unsafe impl Sync for SchemaSubmodule<'_> {} // ===== impl SchemaNode ===== impl<'a> SchemaNode<'a> { + /// Returns a mutable raw pointer to the underlying C library representation + /// of the node. + pub fn as_raw(&self) -> *mut ffi::lysc_node { + self.raw + } + #[doc(hidden)] fn check_flag(&self, flag: u32) -> bool { let flags = unsafe { (*self.raw).flags } as u32; @@ -1131,6 +1143,12 @@ unsafe impl Sync for SchemaNode<'_> {} impl SchemaStmtMust<'_> { // TODO: XPath condition + /// Returns a mutable raw pointer to the underlying C library representation + /// of the must statement. + pub fn as_raw(&self) -> *mut ffi::lysc_must { + self.raw + } + /// description substatement. pub fn description(&self) -> Option<&str> { char_ptr_to_opt_str(unsafe { (*self.raw).dsc }) @@ -1175,6 +1193,12 @@ unsafe impl Sync for SchemaStmtMust<'_> {} impl SchemaStmtWhen<'_> { // TODO: XPath condition + /// Returns a mutable raw pointer to the underlying C library representation + /// of the when statement. + pub fn as_raw(&self) -> *mut ffi::lysc_when { + self.raw + } + /// description substatement. pub fn description(&self) -> Option<&str> { char_ptr_to_opt_str(unsafe { (*self.raw).dsc }) @@ -1208,6 +1232,12 @@ unsafe impl Sync for SchemaStmtWhen<'_> {} // ===== impl SchemaLeafType ===== impl SchemaLeafType<'_> { + /// Returns a mutable raw pointer to the underlying C library representation + /// of the leaf type. + pub fn as_raw(&self) -> *mut ffi::lysc_type { + self.raw + } + /// Returns the resolved base type. pub fn base_type(&self) -> DataValueType { let base_type = unsafe { (*self.raw).basetype }; @@ -1253,6 +1283,12 @@ unsafe impl Sync for SchemaLeafType<'_> {} // ===== impl SchemaExtInstance ===== impl<'a> SchemaExtInstance<'a> { + /// Returns a mutable raw pointer to the underlying C library representation + /// of the extension instance. + pub fn as_raw(&self) -> *mut ffi::lysc_ext_instance { + self.raw + } + /// Returns the optional extension's argument. pub fn argument(&self) -> Option { let argument = unsafe { (*self.raw).argument };