metrics_generic/
metrics_cleanup.rs

1// Copyright 2022 The ChromiumOS Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5//! Provides an tool for metrics client cleanup which may hold global state.
6
7/// Ensures any cleanup necessary is performed on drop. Can be used to ensure cleanup is done
8/// regardless of how the caller exits. Should be idempotent.
9pub struct MetricsClientDestructor(Box<dyn FnMut()>);
10impl MetricsClientDestructor {
11    pub fn new<T: 'static + FnMut()>(cleanup: T) -> Self {
12        MetricsClientDestructor(Box::new(cleanup))
13    }
14    /// A convenience method for immediately dropping self and invoking drop logic on the contained
15    /// object.
16    pub fn cleanup(self) {}
17}
18impl Drop for MetricsClientDestructor {
19    fn drop(&mut self) {
20        self.0();
21    }
22}