mock: improve ergonomics when an ExpectedSpan
is needed
#3097
+340
−200
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.
Motivation
Many of the methods on
MockCollector
take anExpectedSpan
. Thisoften requires significant boilerplate. For example, to expect that a
span with a specific name enters and then exits, the following code is
needed:
Solution
In order to make using
tracing-mock
more ergonomic and also morecompact, the
MockCollector
andMockSubscriber
methods that previoustook an
ExpectedSpan
, are now generic overInto<ExpectedSpan>
.There are currently 3 implementations of
From
forExpectedSpan
whichallow the following shorthand uses:
T: Into<String>
- anExpectedSpan
will be created that expects tohave a name specified by
T
.&ExpectedId
- anExpectedSpan
will be created that expects to havethe expected Id. A reference is taken and cloned internally because the
caller always needs to use an
ExpectedId
in at least 2 calls to themock collector/subscriber.
&ExpectedSpan
- The expected span is taken by reference and cloned.In Rust, taking a reference to an object and immediately cloning it is
an anti-pattern. It is considered better to force the user to clone
outside the API to make the cloning explict.
However, in the case of a testing framework, it seems reasonable to
prefer a more concise API, rather than having it more explicit.
To reduce the size of this PR and to avoid unnecessary churn in other
crates, the tests within the tracing repo which use
tracing-mock
willnot be updated to use the new
Into<ExpectedSpan>
capabilities. The newAPI is backwards compatible and those tests can remain as they are.