Decouple Sink trait
#93
Merged
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.
Closes #71.
This PR addresses the issues mentioned by #71 -- downstream implementors are required to write a large amount of boilerplate code and manually add dependencies (such as
atomiccrate forAtomic) when creating their own sinks. This is currently the main blocker for implementing third-party crates (I'm currently working on and planning to implementspdlog-telegram,spdlog-opentelemetry, etc. crates).The benefits of this PR can be simply seen in the diff of file
spdlog/examples/05_sink.rs, and I will elaborate on more details.Explanation
3 new items are introduced in this PR.
The definition of
Sinkhas been changed toClearly, some original methods of
Sinkhave been extracted into a separate traitSinkAccess, which has a blanket implementation:Based on the above design, downstream implementors have 2 approaches to implement a custom sink.
Easy approach
When users do not need complex customization, they can directly use
SinkProp, and the blanket implementation ofGetSinkPropwill automatically handle the boilerplate code part (SinkAccess).Advanced approach
When complex customizations are required, users directly implement
SinkAccessfor their Sink, andSinkPropandGetSinkPropare not needed here.Drawbacks
Unresolved questions
struct SinkProp,trait GetSinkPropandtrait SinkAccess.XXXSinkBuilder::formatterandSinkProp::set_formatterfromBox<dyn Formatter>toF: impl Formatter(by placingBox::newinside the function) to make it easier for callers? (This will bring about an additional breaking change.)