arta/
net.rs

1//! Networking primitives for TCP/UDP communication.
2
3mod tcp_listener;
4mod tcp_stream;
5mod to_socket_addrs;
6mod udp_socket;
7
8pub use tcp_listener::*;
9pub use tcp_stream::*;
10pub use to_socket_addrs::*;
11pub use udp_socket::*;
12
13use cfg_if::cfg_if;
14
15cfg_if! {
16    if #[cfg(windows)] {
17        /// Represents a socket that implements OS specific methods.
18        pub trait OsSocket: std::os::windows::io::AsRawSocket + std::os::windows::io::AsSocket + From<std::os::windows::io::OwnedSocket> {}
19        impl<T> OsSocket for T where T: std::os::windows::io::AsRawSocket + std::os::windows::io::AsSocket {}
20    } else if #[cfg(any(unix, target_os = "wasi"))]{
21        /// Represents a socket that implements OS specific methods.
22        pub trait OsSocket: std::os::fd::AsRawFd + std::os::fd::AsFd + From<std::os::fd::OwnedFd> {}
23        impl<T> OsSocket for T where T: std::os::fd::AsRawFd + std::os::fd::AsFd + From<std::os::fd::OwnedFd> {}
24    } else {
25        /// Represents a socket that implements OS specific methods.
26        pub trait OsSocket {}
27        impl<T> OsSocket for T {}
28    }
29}
30
31/// Represents an async runtime that supports asynchronous networking.
32pub trait NetRuntime: Send + Sync {
33    /// Runtime's tcp listener.
34    type TcpListener: RuntimeTcpListener<Runtime = Self>;
35    /// Runtime's tcp stream.
36    type TcpStream: RuntimeTcpStream<Runtime = Self>;
37    /// Runtime's udp socket.
38    type UdpSocket: RuntimeUdpSocket<Runtime = Self>;
39}