Skip to content

Figure out the default ifmt trait #8489

Closed
@alexcrichton

Description

@alexcrichton

When using ifmt, a format argument can be simple as {} which means "the next argument" formatted as "the default". Currently the default is the ? or fmt::Poly format trait, but this could be changed. There are a few options here that we could take:

  1. Stick with Poly. This means that every type can be logged via {} by default, although some output may not be as expected. This also means it's not overridable.
  2. Define default for T: ToStr. This would basically call to_str() on the argument, and it would fail if you don't ascribe to the ToStr trait. The downside of this is that if you implement ToStr you can't override your default formatting representation.
  3. Create an entirely new fmt::Default trait. All the basic types would implement this with their respective formats. This could possibly remove the need for the s, d, u, i, c, and p traits, but that may need to be a separate issue.

I think the current default of Poly is probably a bad idea, and I'm starting to lean towards the Default trait. I think that for formatting specifiers (like {:.10s}) we would want to keep the other formats, but this means that any type could implement Default however it pleases. It also means that 99% of the formats done today could just be a dead-simple {} (although that's just a guess).

One note on performance, anything implementing the Default may not have to worry about format specifiers/flags/etc because the format specifier grammar wouldn't allow specifying these flags without specifying a format string (like s or d), and there's no reason that a format string for Default has to be defined... That's kinda a side note though.

Nominating for the backwards compatibility milestone as well (especially because the default is ? today.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions