Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement #[error(fmt = ...)] #367

Merged
merged 2 commits into from
Nov 5, 2024
Merged

Implement #[error(fmt = ...)] #367

merged 2 commits into from
Nov 5, 2024

Conversation

dtolnay
Copy link
Owner

@dtolnay dtolnay commented Nov 5, 2024

Before:

#[derive(Error, Debug)]
pub enum Error {
    #[error("{code}{}", match .message {
        Some(msg) => format!(" - {}", &msg),
        None => "".to_owned(),
    })]
    Demo { code: u16, message: Option<String> },
}

After:

#[derive(Error, Debug)]
pub enum Error {
    #[error(fmt = demo_fmt)]
    Demo { code: u16, message: Option<String> },
}

fn demo_fmt(code: &u16, message: &Option<String>, formatter: &mut fmt::Formatter) -> fmt::Result {
    write!(formatter, "{code}")?;
    if let Some(msg) = message {
        write!(formatter, " - {msg}")?;
    }
    Ok(())
}

This avoids forcing allocations (or the even worse workarounds in #201) and keeps elaborate formatting logic out of the data structure.

The parameter order is designed to be usable with generic methods.

#[derive(Error, Debug)]
#[error(fmt = core::fmt::Octal::fmt)]
pub enum Error {
    I32(i32),
    I64(i64),
}

@dtolnay dtolnay linked an issue Nov 5, 2024 that may be closed by this pull request
    warning: this argument (4 byte) is passed by reference, but would be more efficient if passed by value (limit: 8 byte)
       --> tests/test_display.rs:388:16
        |
    388 |     fn pair(k: &i32, v: &i32, formatter: &mut fmt::Formatter) -> fmt::Result {
        |                ^^^^ help: consider passing by value instead: `i32`
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#trivially_copy_pass_by_ref
        = note: `-W clippy::trivially-copy-pass-by-ref` implied by `-W clippy::pedantic`
        = help: to override `-W clippy::pedantic` add `#[allow(clippy::trivially_copy_pass_by_ref)]`

    warning: this argument (4 byte) is passed by reference, but would be more efficient if passed by value (limit: 8 byte)
       --> tests/test_display.rs:388:25
        |
    388 |     fn pair(k: &i32, v: &i32, formatter: &mut fmt::Formatter) -> fmt::Result {
        |                         ^^^^ help: consider passing by value instead: `i32`
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#trivially_copy_pass_by_ref
@dtolnay dtolnay merged commit 184c100 into master Nov 5, 2024
19 checks passed
@dtolnay dtolnay deleted the fmt branch November 5, 2024 22:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Consider exposing formatter in display attribute
1 participant