Skip to content

Commit

Permalink
into_raw for Bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
yodaldevoid committed Dec 1, 2024
1 parent 2834c96 commit 56698f3
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,12 @@ unsafe impl<'a> Binding<'a> for Context {
unsafe fn from_raw(_: &'a Self::Container, raw: *mut Self::CType) -> Self {
Self { raw }
}

unsafe fn into_raw(self) -> *mut Self::CType {
let raw = self.raw;
std::mem::forget(self);
raw
}
}

// ===== helper functions =====
Expand Down
18 changes: 18 additions & 0 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,12 @@ unsafe impl<'a> Binding<'a> for DataTree<'a> {
) -> DataTree<'a> {
DataTree { context, raw }
}

unsafe fn into_raw(self) -> *mut Self::CType {
let raw = self.raw;
std::mem::forget(self);
raw
}
}

unsafe impl Send for DataTree<'_> {}
Expand Down Expand Up @@ -1427,6 +1433,12 @@ unsafe impl<'a> Binding<'a> for DataNodeRef<'a> {
) -> DataNodeRef<'a> {
DataNodeRef { tree, raw }
}

unsafe fn into_raw(self) -> *mut Self::CType {
let raw = self.raw;
std::mem::forget(self);
raw
}
}

impl<'a> NodeIterable<'a> for DataNodeRef<'a> {
Expand Down Expand Up @@ -1517,6 +1529,12 @@ unsafe impl<'a> Binding<'a> for Metadata<'a> {
) -> Metadata<'a> {
Metadata { dnode, raw }
}

unsafe fn into_raw(self) -> *mut Self::CType {
let raw = self.raw;
std::mem::forget(self);
raw
}
}

impl PartialEq for Metadata<'_> {
Expand Down
44 changes: 44 additions & 0 deletions src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,12 @@ unsafe impl<'a> Binding<'a> for SchemaModule<'a> {
) -> SchemaModule<'a> {
SchemaModule { context, raw }
}

unsafe fn into_raw(self) -> *mut Self::CType {
let raw = self.raw;
std::mem::forget(self);
raw
}
}

impl PartialEq for SchemaModule<'_> {
Expand Down Expand Up @@ -506,6 +512,12 @@ unsafe impl<'a> Binding<'a> for SchemaSubmodule<'a> {
) -> SchemaSubmodule<'a> {
SchemaSubmodule { module, raw }
}

unsafe fn into_raw(self) -> *mut Self::CType {
let raw = self.raw;
std::mem::forget(self);
raw
}
}

impl PartialEq for SchemaSubmodule<'_> {
Expand Down Expand Up @@ -1098,6 +1110,12 @@ unsafe impl<'a> Binding<'a> for SchemaNode<'a> {
};
SchemaNode { context, raw, kind }
}

unsafe fn into_raw(self) -> *mut Self::CType {
let raw = self.raw;
std::mem::forget(self);
raw
}
}

impl<'a> NodeIterable<'a> for SchemaNode<'a> {
Expand Down Expand Up @@ -1165,6 +1183,12 @@ unsafe impl<'a> Binding<'a> for SchemaStmtMust<'a> {
_marker: std::marker::PhantomData,
}
}

unsafe fn into_raw(self) -> *mut Self::CType {
let raw = self.raw;
std::mem::forget(self);
raw
}
}

unsafe impl Send for SchemaStmtMust<'_> {}
Expand Down Expand Up @@ -1200,6 +1224,14 @@ unsafe impl<'a> Binding<'a> for SchemaStmtWhen<'a> {
_marker: std::marker::PhantomData,
}
}

unsafe fn into_raw(mut self) -> *mut Self::CType {
// This isn't the actual pointer originally returned by libyang, but it
// shouldn't matter.
let raw = std::ptr::addr_of_mut!(self.raw);
std::mem::forget(self);
raw
}
}

unsafe impl Send for SchemaStmtWhen<'_> {}
Expand Down Expand Up @@ -1245,6 +1277,12 @@ unsafe impl<'a> Binding<'a> for SchemaLeafType<'a> {
) -> SchemaLeafType<'a> {
SchemaLeafType { context, raw }
}

unsafe fn into_raw(self) -> *mut Self::CType {
let raw = self.raw;
std::mem::forget(self);
raw
}
}

unsafe impl Send for SchemaLeafType<'_> {}
Expand Down Expand Up @@ -1344,6 +1382,12 @@ unsafe impl<'a> Binding<'a> for SchemaExtInstance<'a> {
) -> SchemaExtInstance<'a> {
SchemaExtInstance { context, raw }
}

unsafe fn into_raw(self) -> *mut Self::CType {
let raw = self.raw;
std::mem::forget(self);
raw
}
}

unsafe impl Send for SchemaExtInstance<'_> {}
Expand Down
2 changes: 2 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,6 @@ where
Some(Self::from_raw(container, raw))
}
}

unsafe fn into_raw(self) -> *mut Self::CType;
}

0 comments on commit 56698f3

Please sign in to comment.