Skip to content

Debug impl of Any could be improved #46261

Closed
@mzabaluev

Description

@mzabaluev

The Debug impl of Any is currently very simple, it always returns "Any":

use std::any::Any;

/// I don't care if this downcasts to a string,
/// it's all "Any" to me.
fn print_any(something: &Any) {
    println!("{:?}", something);
}

fn main() {
    print_any(&String::from("Hello"));
}

This shows up in panic messages when e.g. unwrap() is called on a thread::Result of a panicked thread. The implementation could do what the default panic hook already does and try to downcast the reference to &'static str and String (and maybe also Cow<'static, str>, though that might be very unusual) and, if successful, include the contents into the output or simply delegate to the string's Debug.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions