Skip to content

Commit 96ad8e5

Browse files
committed
Auto merge of #65013 - petertodd:2019-maybeuninit-debug, r=sfackler
Implement Debug for MaybeUninit Precedent: `UnsafeCell` implements `Debug` even though it can't actually display the value. I noticed this omission while writing the following: ``` #[derive(Debug)] pub struct SliceInitializer<'a, T> { marker: PhantomData<&'a mut T>, uninit: &'a mut [MaybeUninit<T>], written: usize, } ``` ...which currently unergonomically fails to compile. `UnsafeCell` does require `T: Debug`. Because of things like the above I think it'd be better to leave that requirement off. In fact, I'd also suggest removing that requirement for `UnsafeCell` too, which again I noticed in some low-level real world code.
2 parents 42f93de + 8fad66b commit 96ad8e5

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/libcore/mem/maybe_uninit.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use crate::any::type_name;
2+
use crate::fmt;
13
use crate::intrinsics;
24
use crate::mem::ManuallyDrop;
35

@@ -232,6 +234,13 @@ impl<T: Copy> Clone for MaybeUninit<T> {
232234
}
233235
}
234236

237+
#[stable(feature = "maybe_uninit_debug", since = "1.41.0")]
238+
impl<T> fmt::Debug for MaybeUninit<T> {
239+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
240+
f.pad(type_name::<Self>())
241+
}
242+
}
243+
235244
impl<T> MaybeUninit<T> {
236245
/// Creates a new `MaybeUninit<T>` initialized with the given value.
237246
/// It is safe to call [`assume_init`] on the return value of this function.

0 commit comments

Comments
 (0)