Skip to content

Add Display for Escape and escaped() convenience#10

Open
leighmcculloch wants to merge 2 commits intomainfrom
escape-bytes-ergonomics-display
Open

Add Display for Escape and escaped() convenience#10
leighmcculloch wants to merge 2 commits intomainfrom
escape-bytes-ergonomics-display

Conversation

@leighmcculloch
Copy link
Copy Markdown
Member

What

Implement core::fmt::Display for Escape<I> so it can be used directly in format! and write! without allocating an intermediate buffer. Add an escaped() free function that accepts any AsRef<[u8]> type — including &str, &String, and &Vec<u8> — returning an Escape<&[u8]> ready to display.

Why

Downstream callers (e.g. stellar-cli) had to allocate a String and write a local sanitize helper just to embed escaped output in a log line. With Display on Escape and escaped() accepting &str, the same result is a single format! argument with no allocation and no .as_bytes() danc\e.

Close #9

Two additive ergonomic improvements for the common downstream use
case of embedding escaped output in log lines and CLI messages. Both
preserve every existing caller — no signatures changed.

- Implement `core::fmt::Display` for `Escape<I>` (requires
  `I::IntoIter: Clone`, the same bound as the existing `Clone` impl),
  so callers can write `format!("{}", Escape::new(bytes))` without
  allocating an intermediate buffer.

- Add `escaped<T: AsRef<[u8]> + ?Sized>(t: &T) -> Escape<&[u8]>` as a
  free function convenience wrapper. Accepts any byte-slice reference
  including `&[u8]`, `&str`, `&Vec<u8>`, and `&String`, so callers do
  not need `.as_bytes()` for string inputs.

For raw byte iterators, `Escape::new` is unchanged and continues to
accept `IntoIterator<Item: Borrow<u8>>`.
@leighmcculloch leighmcculloch requested a review from fnando April 22, 2026 03:34
@leighmcculloch leighmcculloch marked this pull request as ready for review April 22, 2026 03:34
Copilot AI review requested due to automatic review settings April 22, 2026 03:34
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds ergonomic, zero-allocation formatting support for escaped byte output by implementing Display for Escape<I> and introducing an escaped() helper that accepts AsRef<[u8]> inputs (including &str).

Changes:

  • Implement core::fmt::Display for Escape<I> (with I::IntoIter: Clone) to enable format!/write! usage without intermediate buffers.
  • Add escaped(&T) convenience function returning Escape<&[u8]> for AsRef<[u8]> inputs like &str, &String, and &Vec<u8>.
  • Add tests covering Display formatting and &str input support.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/escape.rs Adds escaped() helper and Display impl for Escape<I> to support direct formatting.
src/test/escape.rs Adds tests validating Display output and escaped() handling of &str / common reference types.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/escape.rs
@leighmcculloch
Copy link
Copy Markdown
Member Author

Build failures should be fixed by:

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.

Accept &str and implement Display for ergonomic zero-alloc escaping

3 participants