Skip to content

Add new FromGlibPtrBorrow2 and FromGlibPtrBorrowMut2 traits #1788

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion glib/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ macro_rules! glib_boxed_wrapper {

#[doc = "Borrows the underlying C value."]
#[inline]
#[deprecated = "Use FromGlibBorrow2 trait"]
pub unsafe fn from_glib_ptr_borrow(ptr: &*mut $ffi_name) -> &Self {
debug_assert_eq!(
std::mem::size_of::<Self>(),
Expand All @@ -58,6 +59,7 @@ macro_rules! glib_boxed_wrapper {

#[doc = "Borrows the underlying C value mutably."]
#[inline]
#[deprecated = "Use FromGlibBorrowMut2 trait"]
pub unsafe fn from_glib_ptr_borrow_mut(ptr: &mut *mut $ffi_name) -> &mut Self {
debug_assert_eq!(
std::mem::size_of::<Self>(),
Expand Down Expand Up @@ -252,6 +254,45 @@ macro_rules! glib_boxed_wrapper {
}
}

#[doc(hidden)]
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::FromGlibPtrBorrow2<*mut $ffi_name> for $name $(<$($generic),+>)? {
#[inline]
unsafe fn from_glib_borrow2(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(hidden)]
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::FromGlibPtrBorrow2<*const $ffi_name> for $name $(<$($generic),+>)? {
#[inline]
unsafe fn from_glib_borrow2(ptr: &*const $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 *const $ffi_name as *const Self)
}
}

#[doc(hidden)]
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::FromGlibPtrBorrowMut2<*mut $ffi_name> for $name $(<$($generic),+>)? {
#[inline]
unsafe fn from_glib_borrow_mut2(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)
}
}

#[doc(hidden)]
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::FromGlibContainerAsVec<*mut $ffi_name, *mut *mut $ffi_name> for $name $(<$($generic),+>)? {
unsafe fn from_glib_none_num_as_vec(ptr: *mut *mut $ffi_name, num: usize) -> Vec<Self> {
Expand Down Expand Up @@ -363,7 +404,7 @@ macro_rules! glib_boxed_wrapper {
#[inline]
unsafe fn from_value(value: &'a $crate::Value) -> Self {
let value = &*(value as *const $crate::Value as *const $crate::gobject_ffi::GValue);
<$name $(<$($generic),+>)?>::from_glib_ptr_borrow(&*(&value.data[0].v_pointer as *const $crate::ffi::gpointer as *const *mut $ffi_name))
<$name $(<$($generic),+>)? as $crate::translate::FromGlibPtrBorrow2<*mut $ffi_name>>::from_glib_borrow2(&*(&value.data[0].v_pointer as *const $crate::ffi::gpointer as *const *mut $ffi_name))
}
}

Expand Down
29 changes: 28 additions & 1 deletion glib/src/boxed_inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,15 @@ macro_rules! glib_boxed_inline_wrapper {

#[doc = "Borrows the underlying C value."]
#[inline]
#[deprecated = "Use FromGlibBorrow2 trait"]
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]
#[deprecated = "Use FromGlibBorrowMut2 trait"]
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 Expand Up @@ -229,6 +231,7 @@ macro_rules! glib_boxed_inline_wrapper {
}

#[inline]
#[allow(unused_unsafe)]
fn to_glib_full(&self) -> *const $ffi_name {
unsafe {
let copy = |$copy_arg: *const $ffi_name| $copy_expr;
Expand Down Expand Up @@ -430,6 +433,30 @@ macro_rules! glib_boxed_inline_wrapper {
}
}

#[doc(hidden)]
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::FromGlibPtrBorrow2<$ffi_name> for $name $(<$($generic),+>)? {
#[inline]
unsafe fn from_glib_borrow2(ptr: &$ffi_name) -> &Self {
debug_assert_eq!(
std::mem::size_of::<Self>(),
std::mem::size_of::<$ffi_name>()
);
&*(ptr as *const $ffi_name as *const Self)
}
}

#[doc(hidden)]
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::FromGlibPtrBorrowMut2<$ffi_name> for $name $(<$($generic),+>)? {
#[inline]
unsafe fn from_glib_borrow_mut2(ptr: &mut $ffi_name) -> &mut Self {
debug_assert_eq!(
std::mem::size_of::<Self>(),
std::mem::size_of::<$ffi_name>()
);
&mut *(ptr as *mut $ffi_name as *mut Self)
}
}

#[doc(hidden)]
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::FromGlibContainerAsVec<*mut $ffi_name, *mut $ffi_name> for $name $(<$($generic),+>)? {
unsafe fn from_glib_none_num_as_vec(ptr: *mut $ffi_name, num: usize) -> Vec<Self> {
Expand Down Expand Up @@ -593,7 +620,7 @@ macro_rules! glib_boxed_inline_wrapper {
unsafe fn from_value(value: &'_ $crate::Value) -> Self {
let ptr = $crate::gobject_ffi::g_value_get_boxed($crate::translate::ToGlibPtr::to_glib_none(value).0);
debug_assert!(!ptr.is_null());
&*(ptr as *const $ffi_name as *const $name $(<$($generic),+>)?)
<$name $(<$($generic),+>)? as $crate::translate::FromGlibPtrBorrow2<$ffi_name>>::from_glib_borrow2(&*(ptr as *const $ffi_name))
}
}

Expand Down
40 changes: 9 additions & 31 deletions glib/src/gobject/interface_info.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,18 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::gobject_ffi;
use crate::{gobject_ffi, translate::*};

#[derive(Debug, Copy, Clone)]
#[doc(alias = "GInterfaceInfo")]
#[repr(transparent)]
pub struct InterfaceInfo(pub(crate) gobject_ffi::GInterfaceInfo);

impl InterfaceInfo {
// rustdoc-stripper-ignore-next
/// Returns a `GInterfaceInfo` pointer.
#[doc(hidden)]
#[inline]
pub fn as_ptr(&self) -> *mut gobject_ffi::GInterfaceInfo {
&self.0 as *const gobject_ffi::GInterfaceInfo as *mut _
}

// rustdoc-stripper-ignore-next
/// Borrows the underlying C value mutably.
#[doc(hidden)]
#[inline]
pub unsafe fn from_glib_ptr_borrow_mut<'a>(
ptr: *mut gobject_ffi::GInterfaceInfo,
) -> &'a mut Self {
&mut *(ptr as *mut Self)
}
wrapper! {
#[derive(Debug)]
#[doc(alias = "GInterfaceInfo")]
pub struct InterfaceInfo(BoxedInline<gobject_ffi::GInterfaceInfo>);
}

unsafe impl Send for InterfaceInfo {}
unsafe impl Sync for InterfaceInfo {}

impl Default for InterfaceInfo {
// rustdoc-stripper-ignore-next
/// Creates a new InterfaceInfo with default value.
fn default() -> Self {
Self(gobject_ffi::GInterfaceInfo {
interface_init: None,
interface_finalize: None,
interface_data: ::std::ptr::null_mut(),
})
unsafe { Self::uninitialized() }
}
}
45 changes: 9 additions & 36 deletions glib/src/gobject/type_info.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,18 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::gobject_ffi;
use crate::{gobject_ffi, translate::*};

#[derive(Debug, Copy, Clone)]
#[doc(alias = "GTypeInfo")]
#[repr(transparent)]
pub struct TypeInfo(pub(crate) gobject_ffi::GTypeInfo);

impl TypeInfo {
// rustdoc-stripper-ignore-next
/// Returns a `GTypeInfo` pointer.
#[doc(hidden)]
#[inline]
pub fn as_ptr(&self) -> *mut gobject_ffi::GTypeInfo {
&self.0 as *const gobject_ffi::GTypeInfo as *mut _
}

// rustdoc-stripper-ignore-next
/// Borrows the underlying C value mutably.
#[doc(hidden)]
#[inline]
pub unsafe fn from_glib_ptr_borrow_mut<'a>(ptr: *mut gobject_ffi::GTypeInfo) -> &'a mut Self {
&mut *(ptr as *mut Self)
}
wrapper! {
#[derive(Debug)]
#[doc(alias = "GTypeInfo")]
pub struct TypeInfo(BoxedInline<gobject_ffi::GTypeInfo>);
}

unsafe impl Send for TypeInfo {}
unsafe impl Sync for TypeInfo {}

impl Default for TypeInfo {
// rustdoc-stripper-ignore-next
/// Creates a new TypeInfo with default value.
fn default() -> Self {
Self(gobject_ffi::GTypeInfo {
class_size: 0u16,
base_init: None,
base_finalize: None,
class_init: None,
class_finalize: None,
class_data: ::std::ptr::null(),
instance_size: 0,
n_preallocs: 0,
instance_init: None,
value_table: ::std::ptr::null(),
})
unsafe { Self::uninitialized() }
}
}
45 changes: 9 additions & 36 deletions glib/src/gobject/type_value_table.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,18 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::gobject_ffi;
use crate::{gobject_ffi, translate::*};

#[derive(Debug, Copy, Clone)]
#[doc(alias = "GTypeValueTable")]
#[repr(transparent)]
pub struct TypeValueTable(pub(crate) gobject_ffi::GTypeValueTable);

impl TypeValueTable {
// rustdoc-stripper-ignore-next
/// Returns a `GTypeValueTable` pointer.
#[doc(hidden)]
#[inline]
pub fn as_ptr(&self) -> *mut gobject_ffi::GTypeValueTable {
&self.0 as *const gobject_ffi::GTypeValueTable as *mut _
}

// rustdoc-stripper-ignore-next
/// Borrows the underlying C value mutably.
#[doc(hidden)]
#[inline]
pub unsafe fn from_glib_ptr_borrow_mut<'a>(
ptr: *mut gobject_ffi::GTypeValueTable,
) -> &'a mut Self {
&mut *(ptr as *mut Self)
}
wrapper! {
#[derive(Debug)]
#[doc(alias = "GTypeValueTable")]
pub struct TypeValueTable(BoxedInline<gobject_ffi::GTypeValueTable>);
}

unsafe impl Send for TypeValueTable {}
unsafe impl Sync for TypeValueTable {}

impl Default for TypeValueTable {
// rustdoc-stripper-ignore-next
/// Creates a new TypeValueTable with default value.
fn default() -> Self {
Self(gobject_ffi::GTypeValueTable {
value_init: None,
value_free: None,
value_copy: None,
value_peek_pointer: None,
collect_format: ::std::ptr::null(),
collect_value: None,
lcopy_format: ::std::ptr::null(),
lcopy_value: None,
})
unsafe { Self::uninitialized() }
}
}
38 changes: 27 additions & 11 deletions glib/src/match_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,6 @@ impl MatchInfo<'_> {
pub fn as_ptr(&self) -> *mut ffi::GMatchInfo {
self.inner.as_ptr()
}
#[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)
}
}

#[doc(hidden)]
Expand Down Expand Up @@ -149,6 +139,32 @@ impl FromGlibPtrBorrow<*const ffi::GMatchInfo> for MatchInfo<'_> {
}
}

impl<'a> FromGlibPtrBorrow2<*mut ffi::GMatchInfo> for MatchInfo<'a> {
#[doc = "Borrows the underlying C value."]
#[inline]
unsafe fn from_glib_borrow2(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)
}
}

impl<'a> FromGlibPtrBorrow2<*const ffi::GMatchInfo> for MatchInfo<'a> {
#[doc = "Borrows the underlying C value."]
#[inline]
unsafe fn from_glib_borrow2(ptr: &*const 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 *const ffi::GMatchInfo as *const Self)
}
}

#[doc(hidden)]
impl IntoGlibPtr<*mut ffi::GMatchInfo> for MatchInfo<'_> {
#[inline]
Expand Down Expand Up @@ -200,7 +216,7 @@ unsafe impl<'a, 'input: 'a> crate::value::FromValue<'a> for &'a MatchInfo<'input
#[inline]
unsafe fn from_value(value: &'a crate::Value) -> Self {
let value = &*(value as *const crate::Value as *const crate::gobject_ffi::GValue);
<MatchInfo<'input>>::from_glib_ptr_borrow(
<MatchInfo<'input>>::from_glib_borrow2(
&*(&value.data[0].v_pointer as *const crate::ffi::gpointer
as *const *mut ffi::GMatchInfo),
)
Expand Down
Loading
Loading