Skip to content

Commit

Permalink
subscriber: remove Layer impls for Arcs (tokio-rs#1649)
Browse files Browse the repository at this point in the history
Implementing `Layer` for `Arc`s, which are immutable, breaks the
ability to implement `Layer::on_layer` with a mutable reference.
This is necessary for per-layer filtering. See
tokio-rs#1576 (comment) for
details. Therefore, the `Layer` impls for `Arc`s should not be used.

In 0.3, we have the opportunity to remove these APIs. Therefore, this PR
removes them.
  • Loading branch information
hawkw committed Oct 21, 2021
1 parent c8b91ca commit b420ae7
Showing 1 changed file with 5 additions and 41 deletions.
46 changes: 5 additions & 41 deletions tracing-subscriber/src/layer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,11 @@ where

macro_rules! layer_impl_body {
() => {
#[inline]
fn on_layer(&mut self, subscriber: &mut S) {
self.deref_mut().on_layer(subscriber);
}

#[inline]
fn new_span(&self, attrs: &span::Attributes<'_>, id: &span::Id, ctx: Context<'_, S>) {
self.deref().new_span(attrs, id, ctx)
Expand Down Expand Up @@ -1238,59 +1243,18 @@ macro_rules! layer_impl_body {
};
}

impl<L, S> Layer<S> for Arc<L>
where
L: Layer<S>,
S: Subscriber,
{
fn on_layer(&mut self, subscriber: &mut S) {
if let Some(inner) = Arc::get_mut(self) {
// XXX(eliza): this may behave weird if another `Arc` clone of this
// layer is layered onto a _different_ subscriber...but there's no
// good solution for that...
inner.on_layer(subscriber);
}
}

layer_impl_body! {}
}

impl<S> Layer<S> for Arc<dyn Layer<S> + Send + Sync>
where
S: Subscriber,
{
fn on_layer(&mut self, subscriber: &mut S) {
if let Some(inner) = Arc::get_mut(self) {
// XXX(eliza): this may behave weird if another `Arc` clone of this
// layer is layered onto a _different_ subscriber...but there's no
// good solution for that...
inner.on_layer(subscriber);
}
}

layer_impl_body! {}
}

impl<L, S> Layer<S> for Box<L>
where
L: Layer<S>,
S: Subscriber,
{
fn on_layer(&mut self, subscriber: &mut S) {
self.deref_mut().on_layer(subscriber);
}

layer_impl_body! {}
}

impl<S> Layer<S> for Box<dyn Layer<S> + Send + Sync>
where
S: Subscriber,
{
fn on_layer(&mut self, subscriber: &mut S) {
self.deref_mut().on_layer(subscriber);
}

layer_impl_body! {}
}

Expand Down

0 comments on commit b420ae7

Please sign in to comment.