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. 9 pub struct MetricsClientDestructor(Box<dyn FnMut()>); 10 impl MetricsClientDestructor { new<T: 'static + FnMut()>(cleanup: T) -> Self11 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. cleanup(self)16 pub fn cleanup(self) {} 17 } 18 impl Drop for MetricsClientDestructor { drop(&mut self)19 fn drop(&mut self) { 20 self.0(); 21 } 22 } 23