Add methods to get access to the wrapped Write reference.#17
Open
ilyvion wants to merge 1 commit intoeyre-rs:masterfrom
Open
Add methods to get access to the wrapped Write reference.#17ilyvion wants to merge 1 commit intoeyre-rs:masterfrom
Write reference.#17ilyvion wants to merge 1 commit intoeyre-rs:masterfrom
Conversation
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.
I'm using the
CodeFormatterwith a small shim that forwards writes to itsfmt::Writeimplementation to its wrappedio::Write, as I want to write formatted code directly to theio::Writestream rather than writing to a byte or string buffer first. It looks like this:Because
fmt::Erroris only used as a "flag," it doesn't contain any information about what caused the error, as the documentation also points out:Consequently, my
FmtWriterhas a second field,Option<io::Error>, which stores the real error that happened, before passing on anfmt::Erroras thefmt::Writeexpects.The way I've worked with this until now, is as follows:
This works fine when the
fmt_writerandformatterare used where they are created, but fall apart if you want to start refactoring this code into smaller functions, because nowformatterhas a&mutborrow offmt_writer, so you can't pass them both at the same time.With my changes, I only need to pass along a
&mut formatterto the smaller functions, and then I can handle errors like this instead: