Add Display for Escape and escaped() convenience#10
Open
leighmcculloch wants to merge 2 commits intomainfrom
Open
Add Display for Escape and escaped() convenience#10leighmcculloch wants to merge 2 commits intomainfrom
leighmcculloch wants to merge 2 commits intomainfrom
Conversation
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>>`.
There was a problem hiding this comment.
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::DisplayforEscape<I>(withI::IntoIter: Clone) to enableformat!/write!usage without intermediate buffers. - Add
escaped(&T)convenience function returningEscape<&[u8]>forAsRef<[u8]>inputs like&str,&String, and&Vec<u8>. - Add tests covering
Displayformatting and&strinput 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.
Member
Author
|
Build failures should be fixed by: |
mootz12
approved these changes
Apr 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Implement
core::fmt::DisplayforEscape<I>so it can be used directly informat!andwrite!without allocating an intermediate buffer. Add anescaped()free function that accepts anyAsRef<[u8]>type — including&str,&String, and&Vec<u8>— returning anEscape<&[u8]>ready to display.Why
Downstream callers (e.g. stellar-cli) had to allocate a
Stringand write a localsanitizehelper just to embed escaped output in a log line. WithDisplayonEscapeandescaped()accepting&str, the same result is a singleformat!argument with no allocation and no.as_bytes()danc\e.Close #9