refactor!: DynDisplay API to mirror std::fmt::Display#4
Merged
Conversation
Introduce dyf::Formatter<'a> as a per-value formatting context that
bundles the FormatSpec and output sink, mirroring std::fmt::Formatter.
Formatter<'a> implements std::fmt::Write so write!(f, ...) works
directly inside dyn_fmt.
Other changes:
- Add pub type Result = std::result::Result<(), Error>
- Replace FormatSpec::fill_and_align with write_aligned writing
directly to the sink (removes one String allocation per argument)
- Rename pipeline Formatter<'s> to Writer<'s>
- Add Error::Write and Error::NamedArgument; named/positional arguments
({0}, {name}) now return Err instead of silently formatting positionally
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.
Summary
Refactor the
DynDisplaytrait API to mirrorstd::fmt::Display, introducing aFormattertype that implementsstd::fmt::Writefor natural use ofwrite!inside formatting implementations.Motivation
The previous API returned
Result<String, Error>fromdyn_fmt, requiring manual alignment and intermediate allocations. The new design follows Rust's standard formatting conventions, enabling zero-allocation streaming to the output sink and leveraging the familiarwrite!macro.Changes
Formatter<'a>struct bundlingFormatSpecand output sink, implementingstd::fmt::Writepub type Result = std::result::Result<(), Error>for trait consistencyDynDisplay::dyn_fmtsignature fromfn(&self, &FormatSpec) -> Result<String, Error>tofn(&self, &mut Formatter<'_>) -> ResultFormatSpec::fill_and_alignwithwrite_alignedthat writes directly to sink (removes per-argument String allocation)Formatter<'s>toWriter<'s>to avoid naming conflictError::Writevariant for fmt write failuresError::NamedArgumentvariant; named/positional arguments ({0},{name}) now returnErrinstead of silently formatting positionallysrc/imp.rsto use new APIBreaking Changes
DynDisplay::dyn_fmtsignature change requires all implementers to migrateFormatterstruct renamed toWriterin the public APIfill_and_alignremoved; alignment now handled automatically viaFormatter::finishTesting
Existing test suite passes with updated implementations. All
dformat!usage remains unchanged.Notes
This aligns the library's mental model with
std::fmt, making custom implementations more intuitive for Rust developers.