Skip to content

Use fmt::from_fn in audit renderers when MSRV reaches 1.93 #25

Description

@dekobon

Summary

The plain and summary audit renderers in crates/host-identity-cli/src/lib.rs currently allocate a helper String on the error path via one_line(err) so embedded newlines don't break the one-line-per-outcome contract. fmt::from_fn (stable since Rust 1.93) would let us stream the sanitised characters directly through the formatter without the intermediate String, eliminating one allocation per errored outcome.

Current state

// crates/host-identity-cli/src/lib.rs
fn one_line(err: &impl fmt::Display) -> String {
    err.to_string().replace(['\n', '\r'], " ")
}

render_audit_plain and render_audit_summary call one_line(err) on the Errored arm. The allocation is small and only happens on the error path, so this isn't hot — but it's removable without behavior change once MSRV allows.

Blocked by

  • Workspace MSRV is 1.85 (see Cargo.toml rust-version).
  • fmt::from_fn stabilised in Rust 1.93.

Proposed refactor (when MSRV bumps)

fn one_line<'a>(err: &'a impl fmt::Display) -> impl fmt::Display + 'a {
    fmt::from_fn(move |f| {
        for ch in err.to_string().chars() {
            let ch = if ch == '\n' || ch == '\r' { ' ' } else { ch };
            f.write_char(ch)?;
        }
        Ok(())
    })
}

Callers use it the same way; only the intermediate String disappears.

Tracking

Re-visit next time the workspace rust-version is bumped to 1.93+ (Cargo.toml).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestlow-priorityNice-to-have, cosmetic, or affects a rarely-used code path

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions