use crate::{protocol, types::ShortUInt, Promise};
use log::warn;
use std::ops::Deref;
#[derive(Debug)]
pub struct CloseOnDrop<T: __private::Closable>(Option<T>);
impl<T: __private::Closable> CloseOnDrop<T> {
pub fn new(inner: T) -> Self {
Self(Some(inner))
}
pub fn into_inner(mut self) -> T {
let inner = self
.0
.take()
.expect("inner should only be None once consumed or dropped");
std::mem::forget(self);
inner
}
pub fn close(self, reply_code: ShortUInt, reply_text: &str) -> Promise<()> {
self.into_inner().close(reply_code, reply_text)
}
}
impl<T: __private::Closable> Deref for CloseOnDrop<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
self.0
.as_ref()
.expect("inner should only be None once consumed or dropped")
}
}
impl<T: __private::Closable> Drop for CloseOnDrop<T> {
fn drop(&mut self) {
if let Some(inner) = self.0.as_ref() {
if let Err(err) = inner
.close(protocol::constants::REPLY_SUCCESS.into(), "OK")
.wait()
{
warn!("Failed to close on drop: {:?}", err);
}
}
}
}
pub(crate) mod __private {
use super::*;
pub trait Closable {
fn close(&self, reply_code: ShortUInt, reply_text: &str) -> Promise<()>;
}
}