Skip to content

Commit 2c27b4d

Browse files
committed
glib: Simplify as_ptr() implementation and add new function to borrow values from pointers directly
1 parent b1513a6 commit 2c27b4d

File tree

4 files changed

+47
-13
lines changed

4 files changed

+47
-13
lines changed

glib/src/boxed.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,19 @@ macro_rules! glib_boxed_wrapper {
4141
#[doc = "Return the inner pointer to the underlying C value."]
4242
#[inline]
4343
pub fn as_ptr(&self) -> *mut $ffi_name {
44-
$crate::translate::ToGlibPtr::to_glib_none(&self.inner).0 as *mut _
44+
unsafe { *(self as *const Self as *const *const $ffi_name) as *mut $ffi_name }
45+
}
46+
47+
#[doc = "Borrows the underlying C value."]
48+
#[inline]
49+
pub unsafe fn from_glib_ptr_borrow<'a>(ptr: *const *const $ffi_name) -> &'a Self {
50+
&*(ptr as *const Self)
51+
}
52+
53+
#[doc = "Borrows the underlying C value mutably."]
54+
#[inline]
55+
pub unsafe fn from_glib_ptr_borrow_mut<'a>(ptr: *mut *mut $ffi_name) -> &'a mut Self {
56+
&mut *(ptr as *mut Self)
4557
}
4658
}
4759

@@ -316,9 +328,8 @@ macro_rules! glib_boxed_wrapper {
316328
unsafe fn from_value(value: &'a $crate::Value) -> Self {
317329
debug_assert_eq!(std::mem::size_of::<Self>(), std::mem::size_of::<$crate::ffi::gpointer>());
318330
let value = &*(value as *const $crate::Value as *const $crate::gobject_ffi::GValue);
319-
let ptr = &value.data[0].v_pointer as *const $crate::ffi::gpointer as *const *const $ffi_name;
320-
debug_assert!(!(*ptr).is_null());
321-
&*(ptr as *const $name $(<$($generic),+>)?)
331+
debug_assert!(!value.data[0].v_pointer.is_null());
332+
<$name $(<$($generic),+>)?>::from_glib_ptr_borrow(&value.data[0].v_pointer as *const $crate::ffi::gpointer as *const *const $ffi_name)
322333
}
323334
}
324335

glib/src/boxed_inline.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,18 @@ macro_rules! glib_boxed_inline_wrapper {
157157
pub fn as_ptr(&self) -> *mut $ffi_name {
158158
&self.inner as *const $ffi_name as *mut _
159159
}
160+
161+
#[doc = "Borrows the underlying C value."]
162+
#[inline]
163+
pub unsafe fn from_glib_ptr_borrow<'a>(ptr: *const $ffi_name) -> &'a Self {
164+
&*(ptr as *const Self)
165+
}
166+
167+
#[doc = "Borrows the underlying C value mutably."]
168+
#[inline]
169+
pub unsafe fn from_glib_ptr_borrow_mut<'a>(ptr: *mut $ffi_name) -> &'a mut Self {
170+
&mut *(ptr as *mut Self)
171+
}
160172
}
161173

162174
#[doc(hidden)]

glib/src/object.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ pub unsafe trait ObjectType:
4646

4747
fn as_object_ref(&self) -> &ObjectRef;
4848
fn as_ptr(&self) -> *mut Self::GlibType;
49+
50+
unsafe fn from_glib_ptr_borrow<'a>(ptr: *const *const Self::GlibType) -> &'a Self;
4951
}
5052

5153
// rustdoc-stripper-ignore-next
@@ -918,7 +920,12 @@ macro_rules! glib_object_wrapper {
918920

919921
#[inline]
920922
fn as_ptr(&self) -> *mut Self::GlibType {
921-
$crate::translate::ToGlibPtr::to_glib_none(&*self.inner).0 as *mut _
923+
unsafe { *(self as *const Self as *const *const $ffi_name) as *mut $ffi_name }
924+
}
925+
926+
#[inline]
927+
unsafe fn from_glib_ptr_borrow<'a>(ptr: *const *const Self::GlibType) -> &'a Self {
928+
&*(ptr as *const Self)
922929
}
923930
}
924931

@@ -1246,10 +1253,9 @@ macro_rules! glib_object_wrapper {
12461253
unsafe fn from_value(value: &'a $crate::Value) -> Self {
12471254
debug_assert_eq!(std::mem::size_of::<Self>(), std::mem::size_of::<$crate::ffi::gpointer>());
12481255
let value = &*(value as *const $crate::Value as *const $crate::gobject_ffi::GValue);
1249-
let ptr = &value.data[0].v_pointer as *const $crate::ffi::gpointer as *const *const $ffi_name;
1250-
debug_assert!(!(*ptr).is_null());
1251-
debug_assert_ne!((**(ptr as *const *const $crate::gobject_ffi::GObject)).ref_count, 0);
1252-
&*(ptr as *const $name $(<$($generic),+>)?)
1256+
debug_assert!(!value.data[0].v_pointer.is_null());
1257+
debug_assert_ne!((*(value.data[0].v_pointer as *const $crate::gobject_ffi::GObject)).ref_count, 0);
1258+
<$name $(<$($generic),+>)? as $crate::object::ObjectType>::from_glib_ptr_borrow(&value.data[0].v_pointer as *const $crate::ffi::gpointer as *const *const $ffi_name)
12531259
}
12541260
}
12551261

glib/src/shared.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@ macro_rules! glib_shared_wrapper {
3939
#[doc = "Return the inner pointer to the underlying C value."]
4040
#[inline]
4141
pub fn as_ptr(&self) -> *mut $ffi_name {
42-
$crate::translate::ToGlibPtr::to_glib_none(&self.inner).0 as *mut _
42+
unsafe { *(self as *const Self as *const *const $ffi_name) as *mut $ffi_name }
43+
}
44+
45+
#[doc = "Borrows the underlying C value."]
46+
#[inline]
47+
pub unsafe fn from_glib_ptr_borrow<'a>(ptr: *const *const $ffi_name) -> &'a Self {
48+
&*(ptr as *const Self)
4349
}
4450
}
4551

@@ -357,9 +363,8 @@ macro_rules! glib_shared_wrapper {
357363
unsafe fn from_value(value: &'a $crate::Value) -> Self {
358364
debug_assert_eq!(std::mem::size_of::<Self>(), std::mem::size_of::<$crate::ffi::gpointer>());
359365
let value = &*(value as *const $crate::Value as *const $crate::gobject_ffi::GValue);
360-
let ptr = &value.data[0].v_pointer as *const $crate::ffi::gpointer as *const *const $ffi_name;
361-
debug_assert!(!(*ptr).is_null());
362-
&*(ptr as *const $name $(<$($generic),+>)?)
366+
debug_assert!(!value.data[0].v_pointer.is_null());
367+
<$name $(<$($generic),+>)?>::from_glib_ptr_borrow(&value.data[0].v_pointer as *const $crate::ffi::gpointer as *const *const $ffi_name)
363368
}
364369
}
365370

0 commit comments

Comments
 (0)