Skip to content

Commit

Permalink
Shorten the MaybeUninit Debug implementation
Browse files Browse the repository at this point in the history
Currently the `Debug` implementation for `MaybeUninit` winds up being
pretty verbose. This struct:

    #[derive(Debug)]
    pub struct Foo {
        pub a: u32,
        pub b: &'static str,
        pub c: MaybeUninit<u32>,
        pub d: MaybeUninit<String>,
    }

Prints as:

    Foo {
        a: 0,
        b: "hello",
        c: core::mem::maybe_uninit::MaybeUninit<u32>,
        d: core::mem::maybe_uninit::MaybeUninit<alloc::string::String>,
    }

The goal is just to be a standin for content so the path prefix doesn't
add any useful information. Change the implementation to trim
`MaybeUninit`'s leading path, meaning the new result is now:

    Foo {
        a: 0,
        b: "hello",
        c: MaybeUninit<u32>,
        d: MaybeUninit<alloc::string::String>,
    }
  • Loading branch information
tgross35 committed Nov 21, 2024
1 parent 318f96a commit fafb15f
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion library/core/src/mem/maybe_uninit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,10 @@ 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>())
let full_name = type_name::<Self>();
// NB: this needs to be changed if `MaybeUninit` moves modules
let short_name = full_name.split_once("mem::maybe_uninit::").unwrap().1;
f.pad(short_name)
}
}

Expand Down

0 comments on commit fafb15f

Please sign in to comment.