use std::{
future::Future,
ops::{Deref, DerefMut},
pin::Pin,
};
pub type Cleanup = Pin<Box<dyn Future<Output = ()>>>;
pub struct Temporary<T> {
pub value: T,
pub cleanup: Cleanup,
}
impl<T> Deref for Temporary<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.value
}
}
impl<T> DerefMut for Temporary<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.value
}
}
impl<T> Temporary<T> {
pub fn new(value: T, cleanup: Cleanup) -> Self {
Self { value, cleanup }
}
pub async fn cleanup(self) {
let Self { value, cleanup } = self;
drop(value);
cleanup.await;
}
}