subscriber: add lifetime parameter to MakeWriter
(#781)
#1654
+359
−418
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.
This backports PR #781 from
master
.Motivation
Currently, the
tracing-subscriber
crate has theMakeWriter
trait forcustomizing the io writer used by
fmt
. This trait is necessary (ratherthan simply using a
Write
instance) because the default implementationperforms the IO on the thread where an event was recorded, meaning that
a separate writer needs to be acquired by each thread (either by calling
a function like
io::stdout
, by locking a sharedWrite
instance,etc).
Right now there is a blanket impl for
Fn() -> T where T: Write
. Thisworks fine with functions like
io::stdout
. However, the other commoncase for this trait is locking a shared writer.
Therefore, it makes sense to see an implementation like this:
Unfortunately, it's impossible to write this. Since
MakeWriter
alwaystakes an
&self
parameter and returnsSelf::Writer
, the genericparameter is unbounded:
This essentially precludes any
MakeWriter
impl where the writer isborrowed from the type implementing
MakeWriter
. This is a significantblow to the usefulness of the trait. For example, it prevented the use
of
MakeWriter
intracing-flame
as suggested in#631 (comment).
Proposal
This PR changes
MakeWriter
to be generic over a lifetime'a
:The
self
parameter is now borrowed for the&'a
lifetime, so it isokay to return a writer borrowed from
self
, such as in theMutex
case.
I've also added an impl of
MakeWriter
forMutex<T> where T: Writer
.Unfortunately, this is a breaking change and will need to wait until we
release
tracing-subscriber
0.3.Fixes #675.
Signed-off-by: Eliza Weisman [email protected]
Motivation
Solution