Skip to content

Commit

Permalink
Merge pull request #22 from yodaldevoid/into_raw_yang2
Browse files Browse the repository at this point in the history
[yang2] `into_raw` for `Bindings`
  • Loading branch information
rwestphal authored Dec 10, 2024
2 parents 4e5b2bd + 9cda30a commit 996153e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
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::os::unix::ffi::OsStrExt;
use std::path::Path;
Expand Down Expand Up @@ -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<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 @@ -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;
Expand Down Expand Up @@ -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<F: AsRawFd>(
context: &'a Context,
Expand Down Expand Up @@ -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 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 @@ -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,
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 })
Expand Down
32 changes: 31 additions & 1 deletion src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

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

0 comments on commit 996153e

Please sign in to comment.