Skip to content

Commit 84192fa

Browse files
committed
glib: collections: Add some more debug assertions about type sizes
1 parent 6d9a350 commit 84192fa

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

glib/src/collections/ptr_slice.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,11 @@ impl<T: TransparentPtrType> PtrSlice<T> {
601601
/// Creates a new empty slice.
602602
#[inline]
603603
pub fn new() -> Self {
604+
debug_assert_eq!(
605+
mem::size_of::<T>(),
606+
mem::size_of::<<T as GlibPtrDefault>::GlibType>()
607+
);
608+
604609
PtrSlice {
605610
ptr: ptr::NonNull::dangling(),
606611
len: 0,

glib/src/collections/slice.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ impl<T: TransparentType> Slice<T> {
403403
/// Borrows a C array.
404404
#[inline]
405405
pub unsafe fn from_glib_borrow_num<'a>(ptr: *const T::GlibType, len: usize) -> &'a [T] {
406+
debug_assert_eq!(mem::size_of::<T>(), mem::size_of::<T::GlibType>());
406407
debug_assert!(!ptr.is_null() || len == 0);
407408

408409
if len == 0 {
@@ -416,6 +417,7 @@ impl<T: TransparentType> Slice<T> {
416417
/// Borrows a mutable C array.
417418
#[inline]
418419
pub unsafe fn from_glib_borrow_num_mut<'a>(ptr: *mut T::GlibType, len: usize) -> &'a mut [T] {
420+
debug_assert_eq!(mem::size_of::<T>(), mem::size_of::<T::GlibType>());
419421
debug_assert!(!ptr.is_null() || len == 0);
420422

421423
if len == 0 {
@@ -432,6 +434,7 @@ impl<T: TransparentType> Slice<T> {
432434
ptr: *const *const T::GlibType,
433435
len: usize,
434436
) -> &'a [&'a T] {
437+
debug_assert_eq!(mem::size_of::<T>(), mem::size_of::<T::GlibType>());
435438
debug_assert!(!ptr.is_null() || len == 0);
436439

437440
if len == 0 {
@@ -448,6 +451,7 @@ impl<T: TransparentType> Slice<T> {
448451
ptr: *mut *mut T::GlibType,
449452
len: usize,
450453
) -> &'a mut [&'a mut T] {
454+
debug_assert_eq!(mem::size_of::<T>(), mem::size_of::<T::GlibType>());
451455
debug_assert!(!ptr.is_null() || len == 0);
452456

453457
if len == 0 {
@@ -522,6 +526,8 @@ impl<T: TransparentType> Slice<T> {
522526
/// Creates a new empty slice.
523527
#[inline]
524528
pub fn new() -> Self {
529+
debug_assert_eq!(mem::size_of::<T>(), mem::size_of::<T::GlibType>());
530+
525531
Slice {
526532
ptr: ptr::NonNull::dangling(),
527533
len: 0,

0 commit comments

Comments
 (0)