Skip to content

Commit

Permalink
fmt: add reload::Handle::with_current (#91)
Browse files Browse the repository at this point in the history
## Motivation

In some cases, when filter reloading is in use, it can be valuable to
inspect the current value of a filter without reloading it. For example,
this could be used to format the current state, or call filter-specific
methods on it.

## Solution

This branch adds a new `Handle::with_current` method. This method
invokes a closure with a borrowed reference to the filter's current
state and returns the result. This does not require acquiring a write
lock.

Signed-off-by: Eliza Weisman <[email protected]>
  • Loading branch information
hawkw authored Jun 25, 2019
1 parent eb7c28d commit 767ec8c
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion tokio-trace-fmt/src/filter/reload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,17 @@ where
where
F: Clone,
{
self.inner.upgrade().map(|inner| inner.read().clone())
self.with_current(F::clone).ok()
}

/// Invokes a closure with a borrowed reference to the current filter,
/// returning the result (or an error if the filter no longer exists).
pub fn with_current<T>(&self, f: impl FnOnce(&F) -> T) -> Result<T, Error> {
let inner = self.inner.upgrade().ok_or(Error {
kind: ErrorKind::SubscriberGone,
})?;
let inner = inner.read();
Ok(f(&*inner))
}
}

Expand Down

0 comments on commit 767ec8c

Please sign in to comment.