use std::fmt::{Debug, Formatter};
use std::ops::{Deref, DerefMut};
pub struct TcpStream {
f: Option<Box<dyn FnOnce() + Send + 'static>>,
stream: tokio::net::TcpStream,
}
impl TcpStream {
pub(crate) fn new<F: FnOnce() + Send + 'static>(f: F, stream: tokio::net::TcpStream) -> Self {
TcpStream {
f: Some(Box::new(f)),
stream,
}
}
}
impl Debug for TcpStream {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
self.stream.fmt(f)
}
}
impl Drop for TcpStream {
fn drop(&mut self) {
self.f.take().unwrap()()
}
}
impl Deref for TcpStream {
type Target = tokio::net::TcpStream;
fn deref(&self) -> &Self::Target {
&self.stream
}
}
impl DerefMut for TcpStream {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.stream
}
}