diff --git a/glib-macros/src/boxed_derive.rs b/glib-macros/src/boxed_derive.rs index ab51b68da0d9..c6e254b43582 100644 --- a/glib-macros/src/boxed_derive.rs +++ b/glib-macros/src/boxed_derive.rs @@ -192,7 +192,9 @@ pub fn impl_boxed(input: &syn::DeriveInput) -> syn::Result { #impl_from_value - unsafe impl #crate_ident::translate::TransparentPtrType for #name {} + unsafe impl #crate_ident::translate::TransparentType for #name { + type GlibType = #name; + } impl #crate_ident::translate::GlibPtrDefault for #name { type GlibType = *mut #name; diff --git a/glib-macros/tests/test.rs b/glib-macros/tests/test.rs index 95471891ded5..c5e40237a02a 100644 --- a/glib-macros/tests/test.rs +++ b/glib-macros/tests/test.rs @@ -1,7 +1,7 @@ // Take a look at the license at the top of the repository in the LICENSE file. use glib::{ - collections::PtrSlice, + collections::Slice, prelude::*, translate::{FromGlib, IntoGlib}, }; @@ -185,19 +185,16 @@ fn derive_boxed_nullable() { } #[test] -fn boxed_transparent_ptr() { +fn boxed_transparent() { #[derive(Clone, Debug, PartialEq, Eq, glib::Boxed)] #[boxed_type(name = "MyBoxed")] struct MyBoxed(String); let vec = vec![MyBoxed(String::from("abc")), MyBoxed(String::from("dfg"))]; - // PtrSlice requires TransparentPtrType trait - let ptr_slice = PtrSlice::from(vec); - assert_eq!( - ptr_slice.last(), - Some(MyBoxed(String::from("dfg"))).as_ref() - ); + // Slice requires TransparentType trait + let slice = Slice::from(vec); + assert_eq!(slice.last(), Some(MyBoxed(String::from("dfg"))).as_ref()); } #[test] diff --git a/glib/src/collections/ptr_slice.rs b/glib/src/collections/ptr_slice.rs index 5788e0e4b603..4c19b0655ad5 100644 --- a/glib/src/collections/ptr_slice.rs +++ b/glib/src/collections/ptr_slice.rs @@ -601,6 +601,11 @@ impl PtrSlice { /// Creates a new empty slice. #[inline] pub fn new() -> Self { + debug_assert_eq!( + mem::size_of::(), + mem::size_of::<::GlibType>() + ); + PtrSlice { ptr: ptr::NonNull::dangling(), len: 0, diff --git a/glib/src/collections/slice.rs b/glib/src/collections/slice.rs index 74c30a4f0938..eb674c342480 100644 --- a/glib/src/collections/slice.rs +++ b/glib/src/collections/slice.rs @@ -403,6 +403,7 @@ impl Slice { /// Borrows a C array. #[inline] pub unsafe fn from_glib_borrow_num<'a>(ptr: *const T::GlibType, len: usize) -> &'a [T] { + debug_assert_eq!(mem::size_of::(), mem::size_of::()); debug_assert!(!ptr.is_null() || len == 0); if len == 0 { @@ -416,6 +417,7 @@ impl Slice { /// Borrows a mutable C array. #[inline] pub unsafe fn from_glib_borrow_num_mut<'a>(ptr: *mut T::GlibType, len: usize) -> &'a mut [T] { + debug_assert_eq!(mem::size_of::(), mem::size_of::()); debug_assert!(!ptr.is_null() || len == 0); if len == 0 { @@ -432,6 +434,7 @@ impl Slice { ptr: *const *const T::GlibType, len: usize, ) -> &'a [&'a T] { + debug_assert_eq!(mem::size_of::(), mem::size_of::()); debug_assert!(!ptr.is_null() || len == 0); if len == 0 { @@ -448,6 +451,7 @@ impl Slice { ptr: *mut *mut T::GlibType, len: usize, ) -> &'a mut [&'a mut T] { + debug_assert_eq!(mem::size_of::(), mem::size_of::()); debug_assert!(!ptr.is_null() || len == 0); if len == 0 { @@ -522,6 +526,8 @@ impl Slice { /// Creates a new empty slice. #[inline] pub fn new() -> Self { + debug_assert_eq!(mem::size_of::(), mem::size_of::()); + Slice { ptr: ptr::NonNull::dangling(), len: 0,