Forward Filtered::downcast_raw to wrapped Layer#1619
Conversation
If `Filtered::downcast_raw` can't resolve the type given to it, forward the type to the wrapped `Layer`'s `downcast_raw`, in case that `Layer` has overridden that method.
hawkw
left a comment
There was a problem hiding this comment.
looks great! i had a couple minor suggestions regarding the test, but take it or leave it!
| use tracing_subscriber::filter::Targets; | ||
| use tracing_subscriber::prelude::*; | ||
| use tracing_subscriber::Layer; | ||
|
|
There was a problem hiding this comment.
it may also be nice to add a test for downcasting to the Layer type itself, ensuring that normal downcasting works? this PR doesn't affect that, so we certainly don't need to also add it, but it might be nice to have if we're going to have a module of downcast_raw tests. could also be added in a follow-up change!
There was a problem hiding this comment.
OK, I added downcast_ref_to_inner_layer_and_filter for this. Let me know if it needs anything more.
hawkw
left a comment
There was a problem hiding this comment.
this looks great, thanks again!
Not to push my luck too far (so tell me to buzz off if you want), but any idea when this would make it into a crates.io release? |
I want to publish a new |
# 0.2.25 (October 5, 2021) This release fixes an issue where a `Layer` implementation's custom `downcast_raw` implementation was lost when wrapping that layer with a per-layer filter. ### Fixed - **registry**: Forward `Filtered::downcast_raw` to wrapped `Layer` ([#1619]) ### Added - Documentation improvements ([#1596], [#1601]) Thanks to @bryanburgers for contributing to this release! [#1619]: #1619 [#1601]: #1601 [#1596]: #1596
# 0.2.25 (October 5, 2021) This release fixes an issue where a `Layer` implementation's custom `downcast_raw` implementation was lost when wrapping that layer with a per-layer filter. ### Fixed - **registry**: Forward `Filtered::downcast_raw` to wrapped `Layer` ([#1619]) ### Added - Documentation improvements ([#1596], [#1601]) Thanks to @bryanburgers for contributing to this release! [#1619]: #1619 [#1601]: #1601 [#1596]: #1596
## Motivation I'm trying to implement a `Layer` that has something very similar to tracing-error's [`WithContext`](https://github.com/tokio-rs/tracing/blob/66cd79f72af5ebcb6f21a1017b6ce33bea05558d/tracing-error/src/layer.rs#L32) to support erasing types and getting access to the current span's state. To do that, I implement `Layer::downcast_raw` in [the same way that tracing-error does](https://github.com/tokio-rs/tracing/blob/66cd79f72af5ebcb6f21a1017b6ce33bea05558d/tracing-error/src/layer.rs#L55-L63). This works great when the layer is not filtered. However, once I filter the layer ```rust let filter = tracing_subscriber::filter::Targets::new().with_default(tracing::Level::INFO); let layer = MyLayer::new(); tracing_subscriber::registry().with(layer.with_filter(filter)).init(); ``` I'm not able to get a `WithContext` instance anymore, because `Filtered` [handles `downcast_raw`, and doesn't forward it](https://github.com/tokio-rs/tracing/blob/66cd79f72af5ebcb6f21a1017b6ce33bea05558d/tracing-subscriber/src/filter/layer_filters.rs#L379-L391) to `MyLayer::downcast_raw`. ## Solution If `Filtered::downcast_raw` does not know how to handle the given type, forward it to the wrapped layer's `Layer::downcast_raw` implementation. Fixes #1618
…rs#1619) ## Motivation I'm trying to implement a `Layer` that has something very similar to tracing-error's [`WithContext`](https://github.com/tokio-rs/tracing/blob/66cd79f72af5ebcb6f21a1017b6ce33bea05558d/tracing-error/src/layer.rs#L32) to support erasing types and getting access to the current span's state. To do that, I implement `Layer::downcast_raw` in [the same way that tracing-error does](https://github.com/tokio-rs/tracing/blob/66cd79f72af5ebcb6f21a1017b6ce33bea05558d/tracing-error/src/layer.rs#L55-L63). This works great when the layer is not filtered. However, once I filter the layer ```rust let filter = tracing_subscriber::filter::Targets::new().with_default(tracing::Level::INFO); let layer = MyLayer::new(); tracing_subscriber::registry().with(layer.with_filter(filter)).init(); ``` I'm not able to get a `WithContext` instance anymore, because `Filtered` [handles `downcast_raw`, and doesn't forward it](https://github.com/tokio-rs/tracing/blob/66cd79f72af5ebcb6f21a1017b6ce33bea05558d/tracing-subscriber/src/filter/layer_filters.rs#L379-L391) to `MyLayer::downcast_raw`. ## Solution If `Filtered::downcast_raw` does not know how to handle the given type, forward it to the wrapped layer's `Layer::downcast_raw` implementation. Fixes tokio-rs#1618
# 0.2.25 (October 5, 2021) This release fixes an issue where a `Layer` implementation's custom `downcast_raw` implementation was lost when wrapping that layer with a per-layer filter. ### Fixed - **registry**: Forward `Filtered::downcast_raw` to wrapped `Layer` ([tokio-rs#1619]) ### Added - Documentation improvements ([tokio-rs#1596], [tokio-rs#1601]) Thanks to @bryanburgers for contributing to this release! [tokio-rs#1619]: tokio-rs#1619 [tokio-rs#1601]: tokio-rs#1601 [tokio-rs#1596]: tokio-rs#1596
Motivation
I'm trying to implement a
Layerthat has something very similar to tracing-error'sWithContextto support erasing types and getting access to the current span's state.To do that, I implement
Layer::downcast_rawin the same way that tracing-error does.This works great when the layer is not filtered. However, once I filter the layer
I'm not able to get a
WithContextinstance anymore, becauseFilteredhandlesdowncast_raw, and doesn't forward it toMyLayer::downcast_raw.Solution
If
Filtered::downcast_rawdoes not know how to handle the given type, forward it to the wrapped layer'sLayer::downcast_rawimplementation.Fixes #1618