diff --git a/Cargo.toml b/Cargo.toml index 7a6210e91..d9cf89130 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,17 +25,22 @@ include = [ "examples/**/*.rs", ] +# For documentation of features see the `mio::features` module. [features] +# By default Mio only provides a shell implementation. default = [] + os-poll = [] os-util = [] pipe = ["os-poll"] -tcp = [] -udp = [] -uds = [] +# Enables `mio::net` module containing networking primitives. +net = [] # Deprecated features, will be removed in a future version. extra-docs = [] # Docs are now always present. +tcp = ["net"] # Replaced with "net" feature. +udp = ["net"] # Replaced with "net" feature. +uds = ["net"] # Replaced with "net" feature. [dependencies] log = "0.4.8" diff --git a/src/lib.rs b/src/lib.rs index c5679dfe2..f30b3a5d6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -127,21 +127,10 @@ pub mod features { //! The `pipe` feature adds `unix::pipe`, and related types, a non-blocking //! wrapper around the `pipe(2)` system call. //! - //! ## Network types + #![cfg_attr(feature = "net", doc = "## Network types (enabled)")] + #![cfg_attr(not(feature = "net"), doc = "## Network types (disabled)")] //! - //! Mio provide three features to enable network types: - //! - #![cfg_attr(feature = "tcp", doc = "* `tcp` (enabled)")] - #![cfg_attr(not(feature = "tcp"), doc = "* `tcp` (disabled)")] - //! : includes `TcpStream` and `TcpListener`, - #![cfg_attr(feature = "udp", doc = "* `udp` (enabled)")] - #![cfg_attr(not(feature = "udp"), doc = "* `udp` (disabled)")] - //! : includes `UdpSocket`, and - #![cfg_attr(feature = "uds", doc = "* `uds` (enabled)")] - #![cfg_attr(not(feature = "uds"), doc = "* `uds` (disabled)")] - //! : includes `UnixDatagram`, `UnixListener`, `UnixStream` and `SocketAddr`. - //! - //! All types can be found in the `net` module. + //! The `net` feature enables networking primitives in the `net` module. } pub mod guide { diff --git a/src/macros/mod.rs b/src/macros/mod.rs index 2275ed9fa..04193b1cf 100644 --- a/src/macros/mod.rs +++ b/src/macros/mod.rs @@ -24,71 +24,24 @@ macro_rules! cfg_not_os_poll { } } -/// One of the `tcp`, `udp`, `uds` features enabled. -#[cfg(unix)] +/// The `net` feature is enabled. macro_rules! cfg_net { ($($item:item)*) => { $( - #[cfg(any(feature = "tcp", feature = "udp", feature = "uds"))] - #[cfg_attr(docsrs, doc(cfg(any(feature = "tcp", feature = "udp", feature = "uds"))))] + #[cfg(feature = "net")] + #[cfg_attr(docsrs, doc(cfg(feature = "net")))] $item )* } } -/// One of the features enabled that needs `IoSource`. That is `tcp`, or `udp`, -/// or on Unix `uds` or `pipe`. +/// One of the features enabled that needs `IoSource`. That is `net` or `pipe` +/// (on Unix). macro_rules! cfg_io_source { ($($item:item)*) => { $( - #[cfg(any(feature = "tcp", feature = "udp", all(unix, any(feature = "uds", feature = "pipe"))))] - #[cfg_attr(docsrs, doc(any(feature = "tcp", feature = "udp", all(unix, any(feature = "uds", feature = "pipe")))))] - $item - )* - } -} - -/// One of the `tcp`, `udp` features enabled. -#[cfg(windows)] -macro_rules! cfg_net { - ($($item:item)*) => { - $( - #[cfg(any(feature = "tcp", feature = "udp"))] - #[cfg_attr(docsrs, doc(cfg(any(feature = "tcp", feature = "udp"))))] - $item - )* - } -} - -/// Feature `tcp` enabled. -macro_rules! cfg_tcp { - ($($item:item)*) => { - $( - #[cfg(feature = "tcp")] - #[cfg_attr(docsrs, doc(cfg(feature = "tcp")))] - $item - )* - } -} - -/// Feature `udp` enabled. -macro_rules! cfg_udp { - ($($item:item)*) => { - $( - #[cfg(feature = "udp")] - #[cfg_attr(docsrs, doc(cfg(feature = "udp")))] - $item - )* - } -} - -/// Feature `uds` enabled. -#[cfg(unix)] -macro_rules! cfg_uds { - ($($item:item)*) => { - $( - #[cfg(feature = "uds")] - #[cfg_attr(docsrs, doc(cfg(feature = "uds")))] + #[cfg(any(feature = "net", all(unix, feature = "pipe")))] + #[cfg_attr(docsrs, doc(any(feature = "net", all(unix, feature = "pipe"))))] $item )* } @@ -107,24 +60,11 @@ macro_rules! cfg_pipe { } /// Feature `os-util` enabled, or one of the features that need `os-util`. -#[cfg(unix)] -macro_rules! cfg_any_os_util { - ($($item:item)*) => { - $( - #[cfg(any(feature = "os-util", feature = "tcp", feature = "udp", feature = "uds", feature = "pipe"))] - #[cfg_attr(docsrs, doc(cfg(any(feature = "os-util", feature = "tcp", feature = "udp", feature = "uds", feature = "pipe"))))] - $item - )* - } -} - -/// Feature `os-util` enabled, or one of the features that need `os-util`. -#[cfg(windows)] macro_rules! cfg_any_os_util { ($($item:item)*) => { $( - #[cfg(any(feature = "os-util", feature = "tcp", feature = "udp", feature = "pipe"))] - #[cfg_attr(docsrs, doc(cfg(any(feature = "os-util", feature = "tcp", feature = "udp", feature = "pipe"))))] + #[cfg(any(feature = "os-util", feature = "net", all(unix, feature = "pipe")))] + #[cfg_attr(docsrs, doc(cfg(any(feature = "os-util", feature = "net", all(unix, feature = "pipe")))))] $item )* } diff --git a/src/net/mod.rs b/src/net/mod.rs index 91804ec2e..de1be0d0e 100644 --- a/src/net/mod.rs +++ b/src/net/mod.rs @@ -1,4 +1,4 @@ -//! Networking primitives +//! Networking primitives. //! //! The types provided in this module are non-blocking by default and are //! designed to be portable across all supported Mio platforms. As long as the @@ -7,18 +7,13 @@ //! //! [portability guidelines]: ../struct.Poll.html#portability -cfg_tcp! { - mod tcp; - pub use self::tcp::{TcpListener, TcpSocket, TcpStream}; -} +mod tcp; +pub use self::tcp::{TcpListener, TcpSocket, TcpStream}; -cfg_udp! { - mod udp; - pub use self::udp::UdpSocket; -} +mod udp; +pub use self::udp::UdpSocket; #[cfg(unix)] -cfg_uds! { - mod uds; - pub use self::uds::{SocketAddr, UnixDatagram, UnixListener, UnixStream}; -} +mod uds; +#[cfg(unix)] +pub use self::uds::{SocketAddr, UnixDatagram, UnixListener, UnixStream}; diff --git a/src/sys/mod.rs b/src/sys/mod.rs index 08bd271b7..c7b630cf1 100644 --- a/src/sys/mod.rs +++ b/src/sys/mod.rs @@ -54,31 +54,7 @@ cfg_os_poll! { #[cfg(unix)] cfg_os_poll! { mod unix; - pub use self::unix::SourceFd; - - pub(crate) use self::unix::{event, Event, Events, Selector, Waker}; - - cfg_tcp! { - pub(crate) use self::unix::tcp; - } - - cfg_udp! { - pub(crate) use self::unix::udp; - } - - cfg_uds! { - pub use self::unix::SocketAddr; - - pub(crate) use self::unix::uds; - } - - cfg_pipe! { - pub(crate) use self::unix::pipe; - } - - cfg_io_source! { - pub(crate) use self::unix::IoSourceState; - } + pub use self::unix::*; } #[cfg(windows)] @@ -98,7 +74,7 @@ cfg_not_os_poll! { } #[cfg(unix)] - cfg_uds! { + cfg_net! { pub use self::unix::SocketAddr; } } diff --git a/src/sys/shell/mod.rs b/src/sys/shell/mod.rs index a63760a5a..7e1533f45 100644 --- a/src/sys/shell/mod.rs +++ b/src/sys/shell/mod.rs @@ -10,16 +10,10 @@ pub(crate) use self::selector::{event, Event, Events, Selector}; mod waker; pub(crate) use self::waker::Waker; -cfg_tcp! { +cfg_net! { pub(crate) mod tcp; -} - -cfg_udp! { pub(crate) mod udp; -} - -#[cfg(unix)] -cfg_uds! { + #[cfg(unix)] pub(crate) mod uds; } diff --git a/src/sys/unix/mod.rs b/src/sys/unix/mod.rs index f045fb511..6cd68d531 100644 --- a/src/sys/unix/mod.rs +++ b/src/sys/unix/mod.rs @@ -14,8 +14,6 @@ macro_rules! syscall { } cfg_os_poll! { - mod net; - mod selector; pub(crate) use self::selector::{event, Event, Events, Selector}; @@ -25,15 +23,11 @@ cfg_os_poll! { mod waker; pub(crate) use self::waker::Waker; - cfg_tcp! { - pub(crate) mod tcp; - } + cfg_net! { + mod net; - cfg_udp! { + pub(crate) mod tcp; pub(crate) mod udp; - } - - cfg_uds! { pub(crate) mod uds; pub use self::uds::SocketAddr; } @@ -66,7 +60,7 @@ cfg_os_poll! { } cfg_not_os_poll! { - cfg_uds! { + cfg_net! { mod uds; pub use self::uds::SocketAddr; } diff --git a/src/sys/unix/net.rs b/src/sys/unix/net.rs index 2671b42c5..e7fc6fc5f 100644 --- a/src/sys/unix/net.rs +++ b/src/sys/unix/net.rs @@ -1,7 +1,5 @@ -#[cfg(all(feature = "os-poll", any(feature = "tcp", feature = "udp")))] use std::net::SocketAddr; -#[cfg(all(feature = "os-poll", any(feature = "udp")))] pub(crate) fn new_ip_socket( addr: SocketAddr, socket_type: libc::c_int, @@ -15,10 +13,6 @@ pub(crate) fn new_ip_socket( } /// Create a new non-blocking socket. -#[cfg(all( - feature = "os-poll", - any(feature = "tcp", feature = "udp", feature = "uds") -))] pub(crate) fn new_socket( domain: libc::c_int, socket_type: libc::c_int, @@ -70,7 +64,6 @@ pub(crate) fn new_socket( socket } -#[cfg(all(feature = "os-poll", any(feature = "tcp", feature = "udp")))] pub(crate) fn socket_addr(addr: &SocketAddr) -> (*const libc::sockaddr, libc::socklen_t) { use std::mem::size_of_val; @@ -87,7 +80,6 @@ pub(crate) fn socket_addr(addr: &SocketAddr) -> (*const libc::sockaddr, libc::so } /// `storage` must be initialised to `sockaddr_in` or `sockaddr_in6`. -#[cfg(all(feature = "os-poll", feature = "tcp"))] pub(crate) unsafe fn to_socket_addr( storage: *const libc::sockaddr_storage, ) -> std::io::Result { diff --git a/src/sys/windows/mod.rs b/src/sys/windows/mod.rs index 25590c262..c45680d88 100644 --- a/src/sys/windows/mod.rs +++ b/src/sys/windows/mod.rs @@ -25,13 +25,10 @@ cfg_net! { } }}; } -} -cfg_tcp! { - pub(crate) mod tcp; -} + mod net; -cfg_udp! { + pub(crate) mod tcp; pub(crate) mod udp; } @@ -41,10 +38,6 @@ pub(crate) mod named_pipe; mod waker; pub(crate) use waker::Waker; -cfg_net! { - mod net; -} - cfg_io_source! { use std::io; use std::os::windows::io::RawSocket; diff --git a/src/sys/windows/net.rs b/src/sys/windows/net.rs index f825ee3ed..1eb9fc24e 100644 --- a/src/sys/windows/net.rs +++ b/src/sys/windows/net.rs @@ -5,9 +5,7 @@ use std::sync::Once; use winapi::ctypes::c_int; use winapi::shared::ws2def::SOCKADDR; -use winapi::um::winsock2::{ - ioctlsocket, socket, FIONBIO, INVALID_SOCKET, SOCKET, -}; +use winapi::um::winsock2::{ioctlsocket, socket, FIONBIO, INVALID_SOCKET, SOCKET}; /// Initialise the network stack for Windows. pub(crate) fn init() { @@ -21,7 +19,6 @@ pub(crate) fn init() { } /// Create a new non-blocking socket. -#[cfg(feature = "udp")] pub(crate) fn new_ip_socket(addr: SocketAddr, socket_type: c_int) -> io::Result { use winapi::um::winsock2::{PF_INET, PF_INET6};