Skip to content

Commit 74dcb78

Browse files
committed
docs and cleanup the type parameters
1 parent eb289cf commit 74dcb78

File tree

1 file changed

+58
-38
lines changed

1 file changed

+58
-38
lines changed

tracing-subscriber/src/reload.rs

Lines changed: 58 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
//! Wrapper for a `Collect` or `Subscribe` to allow it to be dynamically reloaded.
22
//!
3-
//! This module provides a type implementing [`Subscribe`] which wraps another type implementing
4-
//! the [`Subscribe`] trait, allowing the wrapped type to be replaced with another
3+
//! This module provides a type implementing [`Subscribe`] and [`Filter`]
4+
//! which wraps another type implementing the corresponding trait. This
5+
//! allows the wrapped type to be replaced with another
56
//! instance of that type at runtime.
67
//!
7-
//! This can be used in cases where a subset of `Collect` functionality
8+
//! This can be used in cases where a subset of `Collect` or `Filter` functionality
89
//! should be dynamically reconfigured, such as when filtering directives may
910
//! change at runtime. Note that this subscriber introduces a (relatively small)
1011
//! amount of overhead, and should thus only be used as needed.
1112
//!
13+
//! ## Note
14+
//!
15+
//! //! The [`Subscribe`] implementation is unable to implement downcasting functionality,
16+
//! so certain `Subscribers` will fail to reload if wrapped in a `reload::Subscriber`.
17+
//!
18+
//! If you only want to be able to dynamically change the
19+
//! `Filter` on your layer, prefer wrapping that `Filter` in the `reload::Subscriber`.
20+
//!
1221
//! [`Subscribe`]: crate::Subscribe
22+
//! [`Filter`]: crate::subscribe::Filter
1323
use crate::subscribe;
1424
use crate::sync::RwLock;
1525

@@ -23,7 +33,10 @@ use tracing_core::{
2333
span, Event, Metadata,
2434
};
2535

26-
/// Wraps a `Collect` or `Subscribe`, allowing it to be reloaded dynamically at runtime.
36+
/// Wraps a `Filter` or `Subscribe`, allowing it to be reloaded dynamically at runtime.
37+
///
38+
/// [`Filter`]: crate::subscribe::Filter
39+
/// [`Subscribe`]: crate::Subscribe
2740
#[derive(Debug)]
2841
pub struct Subscriber<S> {
2942
// TODO(eliza): this once used a `crossbeam_util::ShardedRwLock`. We may
@@ -119,25 +132,6 @@ where
119132
}
120133
}
121134

122-
impl<S> Subscriber<S> {
123-
/// Wraps the given `Subscribe`, returning a subscriber and a `Handle` that allows
124-
/// the inner type to be modified at runtime.
125-
pub fn new(inner: S) -> (Self, Handle<S>) {
126-
let this = Self {
127-
inner: Arc::new(RwLock::new(inner)),
128-
};
129-
let handle = this.handle();
130-
(this, handle)
131-
}
132-
133-
/// Returns a `Handle` that can be used to reload the wrapped `Subscribe`.
134-
pub fn handle(&self) -> Handle<S> {
135-
Handle {
136-
inner: Arc::downgrade(&self.inner),
137-
}
138-
}
139-
}
140-
141135
#[cfg(all(feature = "registry", feature = "std"))]
142136
#[cfg_attr(docsrs, doc(cfg(all(feature = "registry", feature = "std"))))]
143137
impl<S, C> crate::subscribe::Filter<C> for Subscriber<S>
@@ -191,24 +185,50 @@ where
191185
}
192186
}
193187

188+
impl<T> Subscriber<T> {
189+
/// Wraps the given `Subscribe` or `Filter`,
190+
/// returning a subscriber or filter and a `Handle` that allows
191+
/// the inner type to be modified at runtime.
192+
///
193+
/// [`Filter`]: crate::subscribe::Filter
194+
/// [`Subscribe`]: crate::Subscribe
195+
pub fn new(inner: T) -> (Self, Handle<T>) {
196+
let this = Self {
197+
inner: Arc::new(RwLock::new(inner)),
198+
};
199+
let handle = this.handle();
200+
(this, handle)
201+
}
202+
203+
/// Returns a `Handle` that can be used to reload the wrapped `Subscribe`.
204+
pub fn handle(&self) -> Handle<T> {
205+
Handle {
206+
inner: Arc::downgrade(&self.inner),
207+
}
208+
}
209+
}
210+
194211
// ===== impl Handle =====
195212

196-
impl<S> Handle<S> {
197-
/// Replace the current subscriber with the provided `new_subscriber`.
213+
impl<T> Handle<T> {
214+
/// Replace the current subscriber or filter with the provided `new_value`.
215+
///
216+
/// **Warning:** [`Handle::reload`] cannot be used with the [`Filtered`](crate::filter::Filtered)
217+
/// subscriber; use [`Handle::modify`] instead (this [this issue] for additional details).
218+
///
219+
/// However, if the _only_ the [`Filter`] needs to be modified, use `reload::Subscriber` to
220+
/// to wrap the `Filter` directly.
198221
///
199-
/// **Warning:** The [`Filtered`](crate::filter::Filtered) type currently can't be changed
200-
/// at runtime via the [`Handle::reload`] method.
201-
/// Use the [`Handle::modify`] method to change the filter instead.
202-
/// (see <https://github.com/tokio-rs/tracing/issues/1629>)
203-
pub fn reload(&self, new_subscriber: impl Into<S>) -> Result<(), Error> {
204-
self.modify(|subscriber| {
205-
*subscriber = new_subscriber.into();
222+
/// [this issue]: https://github.com/tokio-rs/tracing/issues/1629
223+
pub fn reload(&self, new_value: impl Into<T>) -> Result<(), Error> {
224+
self.modify(|object| {
225+
*object = new_value.into();
206226
})
207227
}
208228

209229
/// Invokes a closure with a mutable reference to the current subscriber,
210230
/// allowing it to be modified in place.
211-
pub fn modify(&self, f: impl FnOnce(&mut S)) -> Result<(), Error> {
231+
pub fn modify(&self, f: impl FnOnce(&mut T)) -> Result<(), Error> {
212232
let inner = self.inner.upgrade().ok_or(Error {
213233
kind: ErrorKind::CollectorGone,
214234
})?;
@@ -235,16 +255,16 @@ impl<S> Handle<S> {
235255

236256
/// Returns a clone of the subscriber's current value if it still exists.
237257
/// Otherwise, if the collector has been dropped, returns `None`.
238-
pub fn clone_current(&self) -> Option<S>
258+
pub fn clone_current(&self) -> Option<T>
239259
where
240-
S: Clone,
260+
T: Clone,
241261
{
242-
self.with_current(S::clone).ok()
262+
self.with_current(T::clone).ok()
243263
}
244264

245265
/// Invokes a closure with a borrowed reference to the current subscriber,
246266
/// returning the result (or an error if the collector no longer exists).
247-
pub fn with_current<T>(&self, f: impl FnOnce(&S) -> T) -> Result<T, Error> {
267+
pub fn with_current<T2>(&self, f: impl FnOnce(&T) -> T2) -> Result<T2, Error> {
248268
let inner = self.inner.upgrade().ok_or(Error {
249269
kind: ErrorKind::CollectorGone,
250270
})?;
@@ -253,7 +273,7 @@ impl<S> Handle<S> {
253273
}
254274
}
255275

256-
impl<S> Clone for Handle<S> {
276+
impl<T> Clone for Handle<T> {
257277
fn clone(&self) -> Self {
258278
Handle {
259279
inner: self.inner.clone(),

0 commit comments

Comments
 (0)