diff --git a/src/context.rs b/src/context.rs index 723e0da..f6d0866 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::os::unix::ffi::OsStrExt; use std::path::Path; @@ -112,6 +113,13 @@ 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 { + let ctx = ManuallyDrop::new(self); + ctx.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 bd5e9d7..25d695a 100644 --- a/src/data.rs +++ b/src/data.rs @@ -11,6 +11,7 @@ use std::ffi::CStr; use std::ffi::CString; use std::os::raw::{c_char, c_void}; use std::os::unix::io::AsRawFd; +use std::mem::ManuallyDrop; use std::slice; use crate::context::Context; @@ -384,6 +385,13 @@ 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 { + let node = ManuallyDrop::new(self); + node.raw + } + /// Parse (and validate) input data as a YANG data tree. pub fn parse_file( context: &'a Context, @@ -739,6 +747,13 @@ impl<'a> Drop for DataTree<'a> { // ===== impl DataNodeRef ===== impl<'a, 'b> DataNodeRef<'a, 'b> { + /// Returns a mutable raw pointer to the underlying C library representation + /// of the data node reference. + pub fn into_raw(self) -> *mut ffi::lyd_node { + let node = ManuallyDrop::new(self); + node.raw + } + /// Schema definition of this node. pub fn schema(&self) -> SchemaNode<'_> { let raw = unsafe { (*self.raw).schema }; @@ -936,7 +951,7 @@ impl<'a, 'b> DataNodeRef<'a, 'b> { 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, @@ -973,7 +988,7 @@ impl<'a, 'b> DataNodeRef<'a, 'b> { 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(), @@ -1018,7 +1033,7 @@ impl<'a, 'b> DataNodeRef<'a, 'b> { 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(), @@ -1057,7 +1072,7 @@ impl<'a, 'b> DataNodeRef<'a, 'b> { 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, @@ -1151,6 +1166,12 @@ unsafe impl Sync for DataNodeRef<'_, '_> {} // ===== impl Metadata ===== impl<'a, 'b> Metadata<'a, 'b> { + /// 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 d1995a7..4c0c5f4 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -173,7 +173,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 } @@ -389,6 +389,12 @@ unsafe impl Sync for SchemaModule<'_> {} // ===== 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; @@ -1000,6 +1006,12 @@ unsafe impl Sync for SchemaNode<'_> {} impl<'a> SchemaStmtMust<'a> { // 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 }) @@ -1044,6 +1056,12 @@ unsafe impl Sync for SchemaStmtMust<'_> {} impl<'a> SchemaStmtWhen<'a> { // 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 }) @@ -1077,6 +1095,12 @@ unsafe impl Sync for SchemaStmtWhen<'_> {} // ===== impl SchemaLeafType ===== impl<'a> SchemaLeafType<'a> { + /// 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 }; @@ -1122,6 +1146,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 };