From 5cc79488d865b31bd189d81923b324ef36563999 Mon Sep 17 00:00:00 2001 From: Gabriel Smith Date: Sun, 8 Dec 2024 07:15:46 -0500 Subject: [PATCH] as_raw/into_raw for structs Adds methods to get the raw pointer to the libyang structures held by many structures. into_raw has been implemented where the struct manages the pointer (has a Drop impl to free/release the internal struture), and as_raw has been implemented where the struct is just a wrapper. --- src/context.rs | 7 +++++++ src/data.rs | 27 +++++++++++++++++++++++---- src/schema.rs | 32 +++++++++++++++++++++++++++++++- 3 files changed, 61 insertions(+), 5 deletions(-) diff --git a/src/context.rs b/src/context.rs index 723e0da..8874095 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,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 e133230..07c555e 100644 --- a/src/data.rs +++ b/src/data.rs @@ -9,6 +9,7 @@ use bitflags::bitflags; use std::ffi::CStr; use std::ffi::CString; +use std::mem::ManuallyDrop; use std::os::raw::{c_char, c_void}; use std::os::unix::io::AsRawFd; use std::slice; @@ -384,6 +385,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 + } + /// Parse (and validate) input data as a YANG data tree. pub fn parse_file( context: &'a Context, @@ -739,6 +746,12 @@ impl Drop for DataTree<'_> { // ===== 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 { + ManuallyDrop::new(self).raw + } + /// Schema definition of this node. pub fn schema(&self) -> SchemaNode<'_> { let raw = unsafe { (*self.raw).schema }; @@ -936,7 +949,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 +986,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 +1031,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 +1070,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 +1164,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 762d3a4..4d978a0 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 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 }) @@ -1044,6 +1056,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 }) @@ -1077,6 +1095,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 }; @@ -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 };