Skip to content

Commit

Permalink
into_raw for Bindings
Browse files Browse the repository at this point in the history
SchemaStmtWhen was modified to preserve the pointer returned by
lysc_node_when rather than dereferencing it. This is to prevent pointee
lifetime issues when returning the pointer to a pointer that
SchemaStmtWhen::into_raw requires.
  • Loading branch information
yodaldevoid committed Dec 1, 2024
1 parent d596554 commit fdb469f
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 4 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
67 changes: 63 additions & 4 deletions src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub struct SchemaStmtMust<'a> {
/// YANG when substatement.
#[derive(Clone, Debug)]
pub struct SchemaStmtWhen<'a> {
raw: *mut ffi::lysc_when,
raw: *mut *mut ffi::lysc_when,
_marker: std::marker::PhantomData<&'a Context>,
}

Expand Down 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 All @@ -1046,12 +1076,12 @@ impl<'a> SchemaStmtWhen<'a> {

/// description substatement.
pub fn description(&self) -> Option<&str> {
char_ptr_to_opt_str(unsafe { (*self.raw).dsc })
char_ptr_to_opt_str(unsafe { (**self.raw).dsc })
}

/// reference substatement.
pub fn reference(&self) -> Option<&str> {
char_ptr_to_opt_str(unsafe { (*self.raw).ref_ })
char_ptr_to_opt_str(unsafe { (**self.raw).ref_ })
}
}

Expand All @@ -1063,12 +1093,21 @@ unsafe impl<'a> Binding<'a> for SchemaStmtWhen<'a> {
_context: &'a Context,
raw: *mut *mut ffi::lysc_when,
) -> SchemaStmtWhen<'_> {
let raw = unsafe { *raw };
SchemaStmtWhen {
raw,
_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 SchemaStmtWhen<'_> {}
Expand Down Expand Up @@ -1114,6 +1153,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 +1262,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 fdb469f

Please sign in to comment.