Skip to content

Commit 1827f28

Browse files
committed
glib: Move various assertions from FromValue to from_glib_ptr_borrow()
The latter is just called from the former, and the assertions are valid for all callers of the latter.
1 parent f1cb9ed commit 1827f28

File tree

5 files changed

+28
-12
lines changed

5 files changed

+28
-12
lines changed

glib/src/boxed.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,22 @@ macro_rules! glib_boxed_wrapper {
4848
#[doc = "Borrows the underlying C value."]
4949
#[inline]
5050
pub unsafe fn from_glib_ptr_borrow(ptr: &*mut $ffi_name) -> &Self {
51+
debug_assert_eq!(
52+
std::mem::size_of::<Self>(),
53+
std::mem::size_of::<$crate::ffi::gpointer>()
54+
);
55+
debug_assert!(!ptr.is_null());
5156
&*(ptr as *const *mut $ffi_name as *const Self)
5257
}
5358

5459
#[doc = "Borrows the underlying C value mutably."]
5560
#[inline]
5661
pub unsafe fn from_glib_ptr_borrow_mut(ptr: &mut *mut $ffi_name) -> &mut Self {
62+
debug_assert_eq!(
63+
std::mem::size_of::<Self>(),
64+
std::mem::size_of::<$crate::ffi::gpointer>()
65+
);
66+
debug_assert!(!ptr.is_null());
5767
&mut *(ptr as *mut *mut $ffi_name as *mut Self)
5868
}
5969
}
@@ -351,9 +361,7 @@ macro_rules! glib_boxed_wrapper {
351361

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

glib/src/boxed_inline.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,14 @@ macro_rules! glib_boxed_inline_wrapper {
172172
#[doc = "Borrows the underlying C value."]
173173
#[inline]
174174
pub unsafe fn from_glib_ptr_borrow<'a>(ptr: *const $ffi_name) -> &'a Self {
175+
debug_assert!(!ptr.is_null());
175176
&*(ptr as *const Self)
176177
}
177178

178179
#[doc = "Borrows the underlying C value mutably."]
179180
#[inline]
180181
pub unsafe fn from_glib_ptr_borrow_mut<'a>(ptr: *mut $ffi_name) -> &'a mut Self {
182+
debug_assert!(!ptr.is_null());
181183
&mut *(ptr as *mut Self)
182184
}
183185
}

glib/src/match_info.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ impl MatchInfo<'_> {
3939
#[doc = "Borrows the underlying C value."]
4040
#[inline]
4141
pub unsafe fn from_glib_ptr_borrow(ptr: &*mut ffi::GMatchInfo) -> &Self {
42+
debug_assert_eq!(
43+
std::mem::size_of::<Self>(),
44+
std::mem::size_of::<crate::ffi::gpointer>()
45+
);
46+
debug_assert!(!ptr.is_null());
4247
&*(ptr as *const *mut ffi::GMatchInfo as *const Self)
4348
}
4449
}
@@ -194,12 +199,7 @@ unsafe impl<'a, 'input: 'a> crate::value::FromValue<'a> for &'a MatchInfo<'input
194199

195200
#[inline]
196201
unsafe fn from_value(value: &'a crate::Value) -> Self {
197-
debug_assert_eq!(
198-
std::mem::size_of::<Self>(),
199-
std::mem::size_of::<crate::ffi::gpointer>()
200-
);
201202
let value = &*(value as *const crate::Value as *const crate::gobject_ffi::GValue);
202-
debug_assert!(!value.data[0].v_pointer.is_null());
203203
<MatchInfo<'input>>::from_glib_ptr_borrow(
204204
&*(&value.data[0].v_pointer as *const crate::ffi::gpointer
205205
as *const *mut ffi::GMatchInfo),

glib/src/object.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,12 @@ macro_rules! glib_object_wrapper {
731731

732732
#[inline]
733733
unsafe fn from_glib_ptr_borrow(ptr: &*mut Self::GlibType) -> &Self {
734+
debug_assert_eq!(
735+
std::mem::size_of::<Self>(),
736+
std::mem::size_of::<$crate::ffi::gpointer>()
737+
);
738+
debug_assert!(!ptr.is_null());
739+
debug_assert_ne!((*(*ptr as *const $crate::gobject_ffi::GObject)).ref_count, 0);
734740
&*(ptr as *const *mut $ffi_name as *const Self)
735741
}
736742
}
@@ -1067,10 +1073,7 @@ macro_rules! glib_object_wrapper {
10671073

10681074
#[inline]
10691075
unsafe fn from_value(value: &'a $crate::Value) -> Self {
1070-
debug_assert_eq!(std::mem::size_of::<Self>(), std::mem::size_of::<$crate::ffi::gpointer>());
10711076
let value = &*(value as *const $crate::Value as *const $crate::gobject_ffi::GValue);
1072-
debug_assert!(!value.data[0].v_pointer.is_null());
1073-
debug_assert_ne!((*(value.data[0].v_pointer as *const $crate::gobject_ffi::GObject)).ref_count, 0);
10741077
<$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))
10751078
}
10761079
}

glib/src/shared.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ macro_rules! glib_shared_wrapper {
4646
#[doc = "Borrows the underlying C value."]
4747
#[inline]
4848
pub unsafe fn from_glib_ptr_borrow(ptr: &*mut $ffi_name) -> &Self {
49+
debug_assert_eq!(
50+
std::mem::size_of::<Self>(),
51+
std::mem::size_of::<$crate::ffi::gpointer>()
52+
);
53+
debug_assert!(!ptr.is_null());
4954
&*(ptr as *const *mut $ffi_name as *const Self)
5055
}
5156
}
@@ -371,9 +376,7 @@ macro_rules! glib_shared_wrapper {
371376

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

0 commit comments

Comments
 (0)