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