Skip to content

Commit

Permalink
glib: Move various assertions from FromValue to `from_glib_ptr_borr…
Browse files Browse the repository at this point in the history
…ow()`

The latter is just called from the former, and the assertions are valid
for all callers of the latter.
  • Loading branch information
sdroege committed Apr 30, 2024
1 parent f1cb9ed commit 1827f28
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
12 changes: 10 additions & 2 deletions glib/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,22 @@ macro_rules! glib_boxed_wrapper {
#[doc = "Borrows the underlying C value."]
#[inline]
pub unsafe fn from_glib_ptr_borrow(ptr: &*mut $ffi_name) -> &Self {
debug_assert_eq!(
std::mem::size_of::<Self>(),
std::mem::size_of::<$crate::ffi::gpointer>()
);
debug_assert!(!ptr.is_null());
&*(ptr as *const *mut $ffi_name as *const Self)
}

#[doc = "Borrows the underlying C value mutably."]
#[inline]
pub unsafe fn from_glib_ptr_borrow_mut(ptr: &mut *mut $ffi_name) -> &mut Self {
debug_assert_eq!(
std::mem::size_of::<Self>(),
std::mem::size_of::<$crate::ffi::gpointer>()
);
debug_assert!(!ptr.is_null());
&mut *(ptr as *mut *mut $ffi_name as *mut Self)
}
}
Expand Down Expand Up @@ -351,9 +361,7 @@ macro_rules! glib_boxed_wrapper {

#[inline]
unsafe fn from_value(value: &'a $crate::Value) -> Self {
debug_assert_eq!(std::mem::size_of::<Self>(), std::mem::size_of::<$crate::ffi::gpointer>());
let value = &*(value as *const $crate::Value as *const $crate::gobject_ffi::GValue);
debug_assert!(!value.data[0].v_pointer.is_null());
<$name $(<$($generic),+>)?>::from_glib_ptr_borrow(&*(&value.data[0].v_pointer as *const $crate::ffi::gpointer as *const *mut $ffi_name))
}
}
Expand Down
2 changes: 2 additions & 0 deletions glib/src/boxed_inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,14 @@ macro_rules! glib_boxed_inline_wrapper {
#[doc = "Borrows the underlying C value."]
#[inline]
pub unsafe fn from_glib_ptr_borrow<'a>(ptr: *const $ffi_name) -> &'a Self {
debug_assert!(!ptr.is_null());
&*(ptr as *const Self)
}

#[doc = "Borrows the underlying C value mutably."]
#[inline]
pub unsafe fn from_glib_ptr_borrow_mut<'a>(ptr: *mut $ffi_name) -> &'a mut Self {
debug_assert!(!ptr.is_null());
&mut *(ptr as *mut Self)
}
}
Expand Down
10 changes: 5 additions & 5 deletions glib/src/match_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ impl MatchInfo<'_> {
#[doc = "Borrows the underlying C value."]
#[inline]
pub unsafe fn from_glib_ptr_borrow(ptr: &*mut ffi::GMatchInfo) -> &Self {
debug_assert_eq!(
std::mem::size_of::<Self>(),
std::mem::size_of::<crate::ffi::gpointer>()
);
debug_assert!(!ptr.is_null());
&*(ptr as *const *mut ffi::GMatchInfo as *const Self)
}
}
Expand Down Expand Up @@ -194,12 +199,7 @@ unsafe impl<'a, 'input: 'a> crate::value::FromValue<'a> for &'a MatchInfo<'input

#[inline]
unsafe fn from_value(value: &'a crate::Value) -> Self {
debug_assert_eq!(
std::mem::size_of::<Self>(),
std::mem::size_of::<crate::ffi::gpointer>()
);
let value = &*(value as *const crate::Value as *const crate::gobject_ffi::GValue);
debug_assert!(!value.data[0].v_pointer.is_null());
<MatchInfo<'input>>::from_glib_ptr_borrow(
&*(&value.data[0].v_pointer as *const crate::ffi::gpointer
as *const *mut ffi::GMatchInfo),
Expand Down
9 changes: 6 additions & 3 deletions glib/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,12 @@ macro_rules! glib_object_wrapper {

#[inline]
unsafe fn from_glib_ptr_borrow(ptr: &*mut Self::GlibType) -> &Self {
debug_assert_eq!(
std::mem::size_of::<Self>(),
std::mem::size_of::<$crate::ffi::gpointer>()
);
debug_assert!(!ptr.is_null());
debug_assert_ne!((*(*ptr as *const $crate::gobject_ffi::GObject)).ref_count, 0);
&*(ptr as *const *mut $ffi_name as *const Self)
}
}
Expand Down Expand Up @@ -1067,10 +1073,7 @@ macro_rules! glib_object_wrapper {

#[inline]
unsafe fn from_value(value: &'a $crate::Value) -> Self {
debug_assert_eq!(std::mem::size_of::<Self>(), std::mem::size_of::<$crate::ffi::gpointer>());
let value = &*(value as *const $crate::Value as *const $crate::gobject_ffi::GValue);
debug_assert!(!value.data[0].v_pointer.is_null());
debug_assert_ne!((*(value.data[0].v_pointer as *const $crate::gobject_ffi::GObject)).ref_count, 0);
<$name $(<$($generic),+>)? as $crate::object::ObjectType>::from_glib_ptr_borrow(&*(&value.data[0].v_pointer as *const $crate::ffi::gpointer as *const *mut $ffi_name))
}
}
Expand Down
7 changes: 5 additions & 2 deletions glib/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ macro_rules! glib_shared_wrapper {
#[doc = "Borrows the underlying C value."]
#[inline]
pub unsafe fn from_glib_ptr_borrow(ptr: &*mut $ffi_name) -> &Self {
debug_assert_eq!(
std::mem::size_of::<Self>(),
std::mem::size_of::<$crate::ffi::gpointer>()
);
debug_assert!(!ptr.is_null());
&*(ptr as *const *mut $ffi_name as *const Self)
}
}
Expand Down Expand Up @@ -371,9 +376,7 @@ macro_rules! glib_shared_wrapper {

#[inline]
unsafe fn from_value(value: &'a $crate::Value) -> Self {
debug_assert_eq!(std::mem::size_of::<Self>(), std::mem::size_of::<$crate::ffi::gpointer>());
let value = &*(value as *const $crate::Value as *const $crate::gobject_ffi::GValue);
debug_assert!(!value.data[0].v_pointer.is_null());
<$name $(<$($generic),+>)?>::from_glib_ptr_borrow(&*(&value.data[0].v_pointer as *const $crate::ffi::gpointer as *const *mut $ffi_name))
}
}
Expand Down

0 comments on commit 1827f28

Please sign in to comment.