ntex_compio/
lib.rs

1#[cfg(feature = "compio")]
2mod io;
3
4#[cfg(feature = "compio")]
5pub(crate) mod compat {
6    use std::{io::Result, net, net::SocketAddr};
7
8    use ntex_io::Io;
9    use ntex_service::cfg::SharedCfg;
10
11    /// Tcp stream wrapper for compio TcpStream
12    pub(crate) struct TcpStream(pub(crate) compio_net::TcpStream);
13
14    #[cfg(unix)]
15    /// Tcp stream wrapper for compio UnixStream
16    pub(crate) struct UnixStream(pub(crate) compio_net::UnixStream);
17
18    /// Opens a TCP connection to a remote host.
19    pub async fn tcp_connect(addr: SocketAddr, cfg: SharedCfg) -> Result<Io> {
20        let sock = compio_net::TcpStream::connect(addr).await?;
21        Ok(Io::new(TcpStream(sock), cfg))
22    }
23
24    #[cfg(unix)]
25    /// Opens a unix stream connection.
26    pub async fn unix_connect<'a, P>(addr: P, cfg: SharedCfg) -> Result<Io>
27    where
28        P: AsRef<std::path::Path> + 'a,
29    {
30        let sock = compio_net::UnixStream::connect(addr).await?;
31        Ok(Io::new(UnixStream(sock), cfg))
32    }
33
34    /// Convert std TcpStream to tokio's TcpStream
35    pub fn from_tcp_stream(stream: net::TcpStream, cfg: SharedCfg) -> Result<Io> {
36        stream.set_nodelay(true)?;
37        Ok(Io::new(
38            TcpStream(compio_net::TcpStream::from_std(stream)?),
39            cfg,
40        ))
41    }
42
43    #[cfg(unix)]
44    /// Convert std UnixStream to tokio's UnixStream
45    pub fn from_unix_stream(
46        stream: std::os::unix::net::UnixStream,
47        cfg: SharedCfg,
48    ) -> Result<Io> {
49        Ok(Io::new(
50            UnixStream(compio_net::UnixStream::from_std(stream)?),
51            cfg,
52        ))
53    }
54}
55
56#[cfg(feature = "compio")]
57pub use self::compat::*;