Skip to content

Commit b1513a6

Browse files
committed
glib: Add new marker traits for transparent types
1 parent b018db2 commit b1513a6

File tree

6 files changed

+44
-0
lines changed

6 files changed

+44
-0
lines changed

glib/src/boxed.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ macro_rules! glib_boxed_wrapper {
5959
type GlibType = *mut $ffi_name;
6060
}
6161

62+
#[doc(hidden)]
63+
unsafe impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::TransparentPtrType for $name $(<$($generic),+>)? {}
64+
6265
#[doc(hidden)]
6366
impl<'a $(, $($generic $(: $bound $(+ $bound2)*)?),+)?> $crate::translate::ToGlibPtr<'a, *const $ffi_name> for $name $(<$($generic),+>)? {
6467
type Storage = std::marker::PhantomData<&'a $crate::boxed::Boxed<$ffi_name, Self>>;

glib/src/boxed_inline.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ macro_rules! glib_boxed_inline_wrapper {
164164
type GlibType = *mut $ffi_name;
165165
}
166166

167+
#[doc(hidden)]
168+
unsafe impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::TransparentType for $name $(<$($generic),+>)? {
169+
type GlibType = $ffi_name;
170+
}
171+
167172
#[doc(hidden)]
168173
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::Uninitialized for $name $(<$($generic),+>)? {
169174
#[inline]

glib/src/object.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,9 @@ macro_rules! glib_object_wrapper {
903903
type GlibType = *mut $ffi_name;
904904
}
905905

906+
#[doc(hidden)]
907+
unsafe impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::TransparentPtrType for $name $(<$($generic),+>)? {}
908+
906909
#[doc(hidden)]
907910
unsafe impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::object::ObjectType for $name $(<$($generic),+>)? {
908911
type GlibType = $ffi_name;

glib/src/shared.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ macro_rules! glib_shared_wrapper {
7171
type GlibType = *mut $ffi_name;
7272
}
7373

74+
#[doc(hidden)]
75+
unsafe impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::TransparentPtrType for $name $(<$($generic),+>)? {}
76+
7477
#[doc(hidden)]
7578
impl<'a $(, $($generic $(: $bound $(+ $bound2)*)?),+)?> $crate::translate::ToGlibPtr<'a, *mut $ffi_name> for $name $(<$($generic),+>)? {
7679
type Storage = std::marker::PhantomData<&'a $crate::shared::Shared<$ffi_name, Self>>;

glib/src/translate.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,10 @@ impl IntoGlib for char {
352352
}
353353
}
354354

355+
unsafe impl TransparentType for char {
356+
type GlibType = u32;
357+
}
358+
355359
impl IntoGlib for Option<char> {
356360
type GlibType = u32;
357361

@@ -1998,6 +2002,10 @@ impl FromGlibContainerAsVec<bool, *mut ffi::gboolean> for bool {
19982002

19992003
macro_rules! impl_from_glib_container_as_vec_fundamental {
20002004
($name:ty) => {
2005+
unsafe impl TransparentType for $name {
2006+
type GlibType = $name;
2007+
}
2008+
20012009
impl FromGlibContainerAsVec<$name, *const $name> for $name {
20022010
unsafe fn from_glib_none_num_as_vec(ptr: *const $name, num: usize) -> Vec<Self> {
20032011
if num == 0 || ptr.is_null() {
@@ -2605,6 +2613,24 @@ where
26052613
}
26062614
}
26072615

2616+
/// Trait for types that have the same memory representation as a pointer to their FFI type.
2617+
///
2618+
/// Values of types implementing this trait can be transmuted to pointers of the FFI type,
2619+
/// references to pointers of pointers to the FFI type.
2620+
pub unsafe trait TransparentPtrType: Clone + Sized + GlibPtrDefault {}
2621+
2622+
/// Trait for types that have the same memory representation as their FFI type.
2623+
///
2624+
/// Values of types implementing this trait can be transmuted directly to the FFI type, references
2625+
/// to pointers to the FFI type.
2626+
pub unsafe trait TransparentType: Clone + Sized {
2627+
type GlibType;
2628+
}
2629+
2630+
unsafe impl<T: TransparentPtrType> TransparentType for T {
2631+
type GlibType = <T as GlibPtrDefault>::GlibType;
2632+
}
2633+
26082634
#[cfg(test)]
26092635
mod tests {
26102636
use std::{collections::HashMap, fs};

glib/src/types.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ use crate::{translate::*, Slice};
1414
#[repr(transparent)]
1515
pub struct Type(ffi::GType);
1616

17+
unsafe impl TransparentType for Type {
18+
type GlibType = ffi::GType;
19+
}
20+
1721
impl Type {
1822
// rustdoc-stripper-ignore-next
1923
/// An invalid `Type` used as error return value in some functions

0 commit comments

Comments
 (0)