Struct metrics_util::RecoverableRecorder
source · pub struct RecoverableRecorder<R> { /* private fields */ }
Expand description
Wraps a recorder to allow for recovering it after being installed.
Installing a recorder generally involves providing an owned value, which means that it is not possible to recover the recorder after it has been installed. For some recorder implementations, it can be important to perform finalization before the application exits, which is not possible if the application cannot consume the recorder.
RecoverableRecorder
allows wrapping a recorder such that a weak reference to it is installed
globally, while the recorder itself is held by RecoverableRecorder
. This allows for recovering
the recorder whenever the application chooses.
As a drop guard
While RecoverableRecorder
provides a method to manually recover the recorder directly, one
particular benefit is that due to how the recorder is wrapped, when RecoverableRecorder
is
dropped, and the last active reference to it is dropped, the recorder itself will be dropped.
This allows using RecoverableRecorder
as a drop guard, ensuring that by dropping it, the
recorder itself will be dropped, and any finalization logic implemented for the recorder will be
run.
Implementations§
source§impl<R: Recorder + 'static> RecoverableRecorder<R>
impl<R: Recorder + 'static> RecoverableRecorder<R>
sourcepub fn from_recorder(recorder: R) -> Result<Self, SetRecorderError>
pub fn from_recorder(recorder: R) -> Result<Self, SetRecorderError>
Creates a new RecoverableRecorder
, wrapping the given recorder.
A weakly-referenced version of the recorder is installed globally, while the original
recorder is held within RecoverableRecorder
, and can be recovered by calling into_inner
.
Errors
If a recorder is already installed, an error is returned.
sourcepub fn into_inner(self) -> R
pub fn into_inner(self) -> R
Consumes this wrapper, returning the original recorder.
This method will loop until there are no active weak references to the recorder. It is not advised to call this method under heavy load, as doing so is not deterministic or ordered and may block for an indefinite amount of time.