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 d596554 commit 6628579
Show file tree
Hide file tree
Showing 4 changed files with 96 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 @@ -502,6 +502,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
26 changes: 26 additions & 0 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,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<'a> Send for DataTree<'a> {}
Expand Down Expand Up @@ -1099,6 +1105,16 @@ unsafe impl<'a, 'b> Binding<'a> for DataNodeRef<'a, 'b> {
) -> DataNodeRef<'a, 'b> {
DataNodeRef { tree, raw }
}

unsafe fn into_raw(self) -> *mut Self::CType {
let raw = self.raw;
#[allow(clippy::forget_non_drop)]
// Allow the forget here despite DataNodeRef not having a Drop impl. If
// a Drop impl ever is added, we don't want this to start behaving
// incorrectly.
std::mem::forget(self);
raw
}
}

impl<'a, 'b> NodeIterable<'a> for DataNodeRef<'a, 'b> {
Expand Down Expand Up @@ -1189,6 +1205,16 @@ unsafe impl<'a, 'b> Binding<'a> for Metadata<'a, 'b> {
) -> Metadata<'a, 'b> {
Metadata { dnode, raw }
}

unsafe fn into_raw(self) -> *mut Self::CType {
let raw = self.raw;
#[allow(clippy::forget_non_drop)]
// Allow the forget here despite DataNodeRef not having a Drop impl. If
// a Drop impl ever is added, we don't want this to start behaving
// incorrectly.
std::mem::forget(self);
raw
}
}

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

unsafe fn into_raw(self) -> *mut Self::CType {
let raw = self.raw;
#[allow(clippy::forget_non_drop)]
// Allow the forget here despite DataNodeRef not having a Drop impl. If
// a Drop impl ever is added, we don't want this to start behaving
// incorrectly.
std::mem::forget(self);
raw
}
}

impl<'a> PartialEq for SchemaModule<'a> {
Expand Down Expand Up @@ -967,6 +977,16 @@ unsafe impl<'a> Binding<'a> for SchemaNode<'a> {
};
SchemaNode { context, raw, kind }
}

unsafe fn into_raw(self) -> *mut Self::CType {
let raw = self.raw;
#[allow(clippy::forget_non_drop)]
// Allow the forget here despite DataNodeRef not having a Drop impl. If
// a Drop impl ever is added, we don't want this to start behaving
// incorrectly.
std::mem::forget(self);
raw
}
}

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

unsafe fn into_raw(self) -> *mut Self::CType {
let raw = self.raw;
#[allow(clippy::forget_non_drop)]
// Allow the forget here despite DataNodeRef not having a Drop impl. If
// a Drop impl ever is added, we don't want this to start behaving
// incorrectly.
std::mem::forget(self);
raw
}
}

unsafe impl Send for SchemaStmtMust<'_> {}
Expand Down Expand Up @@ -1069,6 +1099,18 @@ 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);
#[allow(clippy::forget_non_drop)]
// Allow the forget here despite DataNodeRef not having a Drop impl. If
// a Drop impl ever is added, we don't want this to start behaving
// incorrectly.
std::mem::forget(self);
raw
}
}

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

unsafe fn into_raw(self) -> *mut Self::CType {
let raw = self.raw;
#[allow(clippy::forget_non_drop)]
// Allow the forget here despite DataNodeRef not having a Drop impl. If
// a Drop impl ever is added, we don't want this to start behaving
// incorrectly.
std::mem::forget(self);
raw
}
}

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

unsafe fn into_raw(self) -> *mut Self::CType {
let raw = self.raw;
#[allow(clippy::forget_non_drop)]
// Allow the forget here despite DataNodeRef not having a Drop impl. If
// a Drop impl ever is added, we don't want this to start behaving
// incorrectly.
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 6628579

Please sign in to comment.