Skip to content

Commit e43b28c

Browse files
moved Debug from derive to impl_ptr in bevy_ptr (#18042)
# Objective Fixes #17988 ## Solution Added two Debug impls to the `impl_ptr` macro - one for Aligned and one for Unaligned. ## Testing No tests have been added. Would a test guaranteeing debug layout be appropriate? --- ## Showcase The debug representation of a `Ptr<'_, Aligned>` follows. `PtrMut` and `OwningPtr` are similar. `Ptr<Aligned>(0x0123456789ab)`
1 parent b73811d commit e43b28c

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

crates/bevy_ptr/src/lib.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@
99

1010
use core::{
1111
cell::UnsafeCell,
12-
fmt::{self, Formatter, Pointer},
12+
fmt::{self, Debug, Formatter, Pointer},
1313
marker::PhantomData,
1414
mem::ManuallyDrop,
1515
num::NonZeroUsize,
1616
ptr::{self, NonNull},
1717
};
1818

1919
/// Used as a type argument to [`Ptr`], [`PtrMut`] and [`OwningPtr`] to specify that the pointer is aligned.
20-
#[derive(Copy, Clone)]
20+
#[derive(Debug, Copy, Clone)]
2121
pub struct Aligned;
2222

2323
/// Used as a type argument to [`Ptr`], [`PtrMut`] and [`OwningPtr`] to specify that the pointer is not aligned.
24-
#[derive(Copy, Clone)]
24+
#[derive(Debug, Copy, Clone)]
2525
pub struct Unaligned;
2626

2727
/// Trait that is only implemented for [`Aligned`] and [`Unaligned`] to work around the lack of ability
@@ -159,7 +159,7 @@ impl<'a, T: ?Sized> From<&'a mut T> for ConstNonNull<T> {
159159
///
160160
/// It may be helpful to think of this type as similar to `&'a dyn Any` but without
161161
/// the metadata and able to point to data that does not correspond to a Rust type.
162-
#[derive(Copy, Clone, Debug)]
162+
#[derive(Copy, Clone)]
163163
#[repr(transparent)]
164164
pub struct Ptr<'a, A: IsAligned = Aligned>(NonNull<u8>, PhantomData<(&'a u8, A)>);
165165

@@ -174,7 +174,6 @@ pub struct Ptr<'a, A: IsAligned = Aligned>(NonNull<u8>, PhantomData<(&'a u8, A)>
174174
///
175175
/// It may be helpful to think of this type as similar to `&'a mut dyn Any` but without
176176
/// the metadata and able to point to data that does not correspond to a Rust type.
177-
#[derive(Debug)]
178177
#[repr(transparent)]
179178
pub struct PtrMut<'a, A: IsAligned = Aligned>(NonNull<u8>, PhantomData<(&'a mut u8, A)>);
180179

@@ -194,7 +193,6 @@ pub struct PtrMut<'a, A: IsAligned = Aligned>(NonNull<u8>, PhantomData<(&'a mut
194193
///
195194
/// It may be helpful to think of this type as similar to `&'a mut ManuallyDrop<dyn Any>` but
196195
/// without the metadata and able to point to data that does not correspond to a Rust type.
197-
#[derive(Debug)]
198196
#[repr(transparent)]
199197
pub struct OwningPtr<'a, A: IsAligned = Aligned>(NonNull<u8>, PhantomData<(&'a mut u8, A)>);
200198

@@ -265,6 +263,19 @@ macro_rules! impl_ptr {
265263
Pointer::fmt(&self.0, f)
266264
}
267265
}
266+
267+
impl Debug for $ptr<'_, Aligned> {
268+
#[inline]
269+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
270+
write!(f, "{}<Aligned>({:?})", stringify!($ptr), self.0)
271+
}
272+
}
273+
impl Debug for $ptr<'_, Unaligned> {
274+
#[inline]
275+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
276+
write!(f, "{}<Unaligned>({:?})", stringify!($ptr), self.0)
277+
}
278+
}
268279
};
269280
}
270281

0 commit comments

Comments
 (0)