tcp_pool/
stream.rs

1use std::fmt::{Debug, Formatter};
2use std::ops::{Deref, DerefMut};
3
4/// tcp流
5pub struct TcpStream {
6    f: Option<Box<dyn FnOnce() + Send + 'static>>,
7    stream: tokio::net::TcpStream,
8}
9
10impl TcpStream {
11    pub(crate) fn new<F: FnOnce() + Send + 'static>(f: F, stream: tokio::net::TcpStream) -> Self {
12        TcpStream {
13            f: Some(Box::new(f)),
14            stream,
15        }
16    }
17}
18
19impl Debug for TcpStream {
20    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
21        self.stream.fmt(f)
22    }
23}
24
25impl Drop for TcpStream {
26    fn drop(&mut self) {
27        self.f.take().unwrap()()
28    }
29}
30
31impl Deref for TcpStream {
32    type Target = tokio::net::TcpStream;
33
34    fn deref(&self) -> &Self::Target {
35        &self.stream
36    }
37}
38
39impl DerefMut for TcpStream {
40    fn deref_mut(&mut self) -> &mut Self::Target {
41        &mut self.stream
42    }
43}