Rust: tracking issue for `debug_non_exhaustive` feature

Created on 17 Dec 2019  路  9Comments  路  Source: rust-lang/rust

This is a tracking issue for adding the ability to add an ellipsis to the end of the Debug representation of a struct, to indicate that the struct has more fields, but that these fields are not displayable. This involves adding the finish_non_exhaustive method to DebugStruct, that produces output like

Name { field1: value1, .. }

where the .. indicate that there are more hidden fields.

Open questions

  • [ ] Should this method be implemented for DebugTuple and DebugMap as well?

    • I would probably say "no" since I can't think of any use cases for them. They could always be added later.

A-fmt B-unstable C-tracking-issue I-nominated Libs-Small Libs-Tracked T-libs

Most helpful comment

Although not its primary intention I also find this feature quite useful when implementing Debug for structs that can't be derived. Where you still want to indicate that not all fields are shown. For example:

struct Example {
    field: String,
    func: Box<dyn Fn(&str) -> bool + 'static>
}

impl fmt::Debug for Example {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("Example")
            .field("field", &self.field)
            .finish_non_exhaustive()
    }
}

All 9 comments

Any ETA as to when this might be stabilized? What is blocking this being stabilized?

AFAIK there aren't any blockers to stabilizing. Things normally live in nightly so they can be tested, but in this case it's unlikely someone will drop stable support just so they can use this feature. I think it will only be used once it's stable.

It's a small low-risk change (since the methods can be reimplemented or deprecated if they are really broken).

Although not its primary intention I also find this feature quite useful when implementing Debug for structs that can't be derived. Where you still want to indicate that not all fields are shown. For example:

struct Example {
    field: String,
    func: Box<dyn Fn(&str) -> bool + 'static>
}

impl fmt::Debug for Example {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("Example")
            .field("field", &self.field)
            .finish_non_exhaustive()
    }
}

It could be possible to extend #[derive(Debug)] to work on structs/enums with non-Derive fields, by skipping them and marking it non-exhaustive.

I also have a use-case for partially printing structs. In my case printing all of struct's fields in a meaningful way would be rather complex and require potentially-expensive FFI calls, so I'd rather omit them from debug output, but I still want to leave a hint that they exist.

I have a use case for printing expensive structs as well.

It would be nice to have a finish_non_exhaustive method on DebugTuple for my crate.

It would be nice to have a finish_non_exhaustive method on DebugTuple for my crate.

Maybe its time to revisit the open question. I could implement this if there were consensus it's a good idea.

Was this page helpful?
0 / 5 - 0 ratings