Make the `impl Debug for Any (+ Send)` (and `Box` forms) more useful
Nobody has claimed this yet.
- Dominant language
- Markdown
- Stars
- 6.6k
- Forks
- 1.7k
- Avg merge
- 16h 14m
- Merged PRs (30d)
- 1
Description
Belated follow-up to the discussion on https://github.com/rust-lang/rust/issues/27719
Currently, the Debug impl for Box<Any> is almost useless. It just prints "Box<Any>".
One of the main uses of Box<Any> in the stdlib is to carry the message of a panic!() invocation back to the parent, out of thread::spawn().join() or thread::catch_panic(). In this case, the Box<Any> could be one of three possibilities:
&'static str, ifpanic!()was called with a string literal and no formatting arguments.String, ifpanic!()was called with formatting arguments.- Any other
Sendtype which was passed topanic!()besides a string literal.
While the first case is technically a subset of the third, I count it as its own item because it's a common pattern when panic!() wants to give an error message but doesn't have any dynamic data to add to it.
Given the comparatively negligible overhead of downcasting, I think impl Debug for Box<Any> should test the first two cases before printing its uselessly generic message:
impl Debug for Box<Any> {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result<()>, {
if let Ok(str_slice) = self.downcast_ref::<&'static str>() {
fmt.pad(str_slice);
} else if let Ok(string) = self.downcast_ref::<String>() {
fmt.pad(string);
} else {
fmt.pad("Box<Any>");
}
}
}
Of course, this applies to impl Debug for Any (+ Send) as well.
While this is technically a breaking change since it modifies stable behavior, I doubt anyone was relying on the exact value printed by this impl, given its completely uninformative message.
In fact, this should massively reduce the occurrence of the uninformative It appears this specialization is already being done: https://github.com/rust-lang/rust/blob/master/src/libstd/panicking.rs#L33Thread ### panicked at Box`` message in ICE reports and StackOverflow questions.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the existing specialization in src/libstd/panicking.rs and the discussion linked from the issue. Check whether Debug implementations for Any, Send, and Box forms already handle string slices and String values. Done means the relevant implementations produce useful messages for those cases while retaining a generic fallback for other types.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- backend
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 28/100