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