fmt: add reload::Handle::with_current (#91)

## 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 <eliza@buoyant.io>
This commit is contained in:
Eliza Weisman
2019-06-24 21:48:16 -07:00
committed by GitHub
parent b9d3e87882
commit d6065ef5fa
+11 -1
View File
@@ -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))
}
}