Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[yang3] into_raw for Bindings #21

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<P: AsRef<Path>>(
&mut self,
Expand Down
27 changes: 23 additions & 4 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 };
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 })
Expand Down
38 changes: 37 additions & 1 deletion src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 })
Expand Down Expand Up @@ -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 })
Expand Down Expand Up @@ -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 };
Expand Down Expand Up @@ -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<String> {
let argument = unsafe { (*self.raw).argument };
Expand Down
Loading