1use std::io::{Read, Write};
2use std::net::{Shutdown, TcpStream};
3#[cfg(unix)]
4use std::os::unix::io::AsRawFd;
5#[cfg(unix)]
6use std::os::unix::net::UnixStream;
7#[cfg(windows)]
8use std::os::windows::io::AsRawSocket;
9
10#[cfg(windows)]
11use uds_windows::UnixStream;
12
13use crate::error::*;
14
15#[cfg(unix)]
16pub trait Stream: Read + Write + Send + Sync + AsRawFd {
17 fn split(&mut self) -> Result<(Box<dyn Read + Send + Sync>, Box<dyn Write + Send + Sync>)>;
18 fn shutdown(&mut self) -> Result<()>;
19 fn try_clone(&mut self) -> ::std::io::Result<Box<dyn Stream>>;
20 fn set_nonblocking(&mut self, b: bool) -> Result<()>;
21}
22
23#[cfg(windows)]
24pub trait Stream: Read + Write + Send + Sync + AsRawSocket {
25 fn split(&mut self) -> Result<(Box<dyn Read + Send + Sync>, Box<dyn Write + Send + Sync>)>;
26 fn shutdown(&mut self) -> Result<()>;
27 fn try_clone(&mut self) -> ::std::io::Result<Box<dyn Stream>>;
28 fn set_nonblocking(&mut self, b: bool) -> Result<()>;
29}
30
31impl Stream for TcpStream {
32 #[inline]
33 fn split(&mut self) -> Result<(Box<dyn Read + Send + Sync>, Box<dyn Write + Send + Sync>)> {
34 Ok((
35 Box::new(TcpStream::try_clone(self).map_err(map_context!())?),
36 Box::new(TcpStream::try_clone(self).map_err(map_context!())?),
37 ))
38 }
39
40 #[inline]
41 fn shutdown(&mut self) -> Result<()> {
42 TcpStream::shutdown(self, Shutdown::Both).map_err(map_context!())?;
43 Ok(())
44 }
45
46 #[inline]
47 fn try_clone(&mut self) -> ::std::io::Result<Box<dyn Stream>> {
48 Ok(Box::new(TcpStream::try_clone(self)?))
49 }
50
51 #[inline]
52 fn set_nonblocking(&mut self, b: bool) -> Result<()> {
53 TcpStream::set_nonblocking(self, b).map_err(map_context!())?;
54 Ok(())
55 }
56}
57
58impl Stream for UnixStream {
59 #[inline]
60 fn split(&mut self) -> Result<(Box<dyn Read + Send + Sync>, Box<dyn Write + Send + Sync>)> {
61 Ok((
62 Box::new(UnixStream::try_clone(self).map_err(map_context!())?),
63 Box::new(UnixStream::try_clone(self).map_err(map_context!())?),
64 ))
65 }
66
67 #[inline]
68 fn shutdown(&mut self) -> Result<()> {
69 UnixStream::shutdown(self, Shutdown::Both).map_err(map_context!())?;
70 Ok(())
71 }
72
73 #[inline]
74 fn try_clone(&mut self) -> ::std::io::Result<Box<dyn Stream>> {
75 Ok(Box::new(UnixStream::try_clone(self)?))
76 }
77
78 #[inline]
79 fn set_nonblocking(&mut self, b: bool) -> Result<()> {
80 UnixStream::set_nonblocking(self, b).map_err(map_context!())?;
81 Ok(())
82 }
83}