about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2019-11-28 03:41:29 +0000
committerbors <bors@rust-lang.org>2019-11-28 03:41:29 +0000
commit96ad8e5fbcf00afc1ccde386b031919b4d01aa33 (patch)
tree05a0a1c735455686eb7a2b43cde8ce6cfafbe72d /src/libcore
parent42f93dedbe8218b4936002b72dbed6f296979c26 (diff)
parent8fad66b43151c5c1bbb7933e54051ae8c11fe595 (diff)
downloadrust-96ad8e5fbcf00afc1ccde386b031919b4d01aa33.tar.gz
rust-96ad8e5fbcf00afc1ccde386b031919b4d01aa33.zip
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.
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/mem/maybe_uninit.rs9
1 files changed, 9 insertions, 0 deletions
diff --git a/src/libcore/mem/maybe_uninit.rs b/src/libcore/mem/maybe_uninit.rs
index e05b40052ee..6661df2ae0d 100644
--- a/src/libcore/mem/maybe_uninit.rs
+++ b/src/libcore/mem/maybe_uninit.rs
@@ -1,3 +1,5 @@
+use crate::any::type_name;
+use crate::fmt;
 use crate::intrinsics;
 use crate::mem::ManuallyDrop;
 
@@ -232,6 +234,13 @@ impl<T: Copy> Clone for MaybeUninit<T> {
     }
 }
 
+#[stable(feature = "maybe_uninit_debug", since = "1.41.0")]
+impl<T> fmt::Debug for MaybeUninit<T> {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        f.pad(type_name::<Self>())
+    }
+}
+
 impl<T> MaybeUninit<T> {
     /// Creates a new `MaybeUninit<T>` initialized with the given value.
     /// It is safe to call [`assume_init`] on the return value of this function.