Skip to content

Commit

Permalink
meta: combine net and dns, use parking_lot (#2951)
Browse files Browse the repository at this point in the history
This combines the `dns` and `net` feature flags. Previously, `dns` was
included as part of `net`. Given that is is rare that one would want
`dns` without `net`, DNS is now entirely gated w/ `net`.

The `parking_lot` feature is included as part of `full`.

Some misc docs are tweaked to reflect feature flag changes.
  • Loading branch information
carllerche committed Oct 12, 2020
1 parent c90681b commit 1923350
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 117 deletions.
4 changes: 1 addition & 3 deletions tokio/Cargo.toml
Expand Up @@ -30,12 +30,12 @@ default = []

# enable everything
full = [
"dns",
"fs",
"io-util",
"io-std",
"macros",
"net",
"parking_lot",
"process",
"rt",
"rt-multi-thread",
Expand All @@ -45,14 +45,12 @@ full = [
"time",
]

dns = []
fs = []
io-util = ["memchr"]
# stdin, stdout, stderr
io-std = []
macros = ["tokio-macros"]
net = [
"dns",
"lazy_static",
"libc",
"mio/os-poll",
Expand Down
94 changes: 46 additions & 48 deletions tokio/src/lib.rs
Expand Up @@ -17,7 +17,7 @@
))]
#![cfg_attr(docsrs, feature(doc_cfg))]

//! A runtime for writing reliable, asynchronous, and slim applications.
//! A runtime for writing reliable network applications without compromising speed.
//!
//! Tokio is an event-driven, non-blocking I/O platform for writing asynchronous
//! applications with the Rust programming language. At a high level, it
Expand Down Expand Up @@ -60,52 +60,6 @@
//! tokio = { version = "0.2", features = ["full"] }
//! ```
//!
//! ## Feature flags
//!
//! Tokio uses a set of [feature flags] to reduce the amount of compiled code. It
//! is possible to just enable certain features over others. By default, Tokio
//! does not enable any features but allows one to enable a subset for their use
//! case. Below is a list of the available feature flags. You may also notice
//! above each function, struct and trait there is listed one or more feature flags
//! that are required for that item to be used. If you are new to Tokio it is
//! recommended that you use the `full` feature flag which will enable all public APIs.
//! Beware though that this will pull in many extra dependencies that you may not
//! need.
//!
//! - `full`: Enables all Tokio public API features listed below.
//! - `rt-core`: Enables `tokio::spawn`, the basic (current thread) scheduler,
//! and non-scheduler utilities.
//! - `rt-multi-thread`: Enables the heavier, multi-threaded, work-stealing scheduler.
//! - `io-util`: Enables the IO based `Ext` traits.
//! - `io-std`: Enable `Stdout`, `Stdin` and `Stderr` types.
//! - `net`: Enables `tokio::net` types such as `TcpStream`, `UnixStream` and `UdpSocket`.
//! - `time`: Enables `tokio::time` types and allows the schedulers to enable
//! the built in timer.
//! - `process`: Enables `tokio::process` types.
//! - `macros`: Enables `#[tokio::main]` and `#[tokio::test]` macros.
//! - `sync`: Enables all `tokio::sync` types.
//! - `stream`: Enables optional `Stream` implementations for types within Tokio.
//! - `signal`: Enables all `tokio::signal` types.
//! - `fs`: Enables `tokio::fs` types.
//! - `dns`: Enables async `tokio::net::ToSocketAddrs`.
//! - `test-util`: Enables testing based infrastructure for the Tokio runtime.
//! - `blocking`: Enables `block_in_place` and `spawn_blocking`.
//!
//! _Note: `AsyncRead` and `AsyncWrite` traits do not require any features and are
//! always available._
//!
//! ### Internal features
//!
//! These features do not expose any new API, but influence internal
//! implementation aspects of Tokio, and can pull in additional
//! dependencies. They are not included in `full`:
//!
//! - `parking_lot`: As a potential optimization, use the _parking_lot_ crate's
//! synchronization primitives internally. MSRV may increase according to the
//! _parking_lot_ release in use.
//!
//! [feature flags]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-features-section
//!
//! ### Authoring applications
//!
//! Tokio is great for writing applications and most users in this case shouldn't
Expand Down Expand Up @@ -332,6 +286,50 @@
//! }
//! }
//! ```
//!
//! ## Feature flags
//!
//! Tokio uses a set of [feature flags] to reduce the amount of compiled code. It
//! is possible to just enable certain features over others. By default, Tokio
//! does not enable any features but allows one to enable a subset for their use
//! case. Below is a list of the available feature flags. You may also notice
//! above each function, struct and trait there is listed one or more feature flags
//! that are required for that item to be used. If you are new to Tokio it is
//! recommended that you use the `full` feature flag which will enable all public APIs.
//! Beware though that this will pull in many extra dependencies that you may not
//! need.
//!
//! - `full`: Enables all Tokio public API features listed below.
//! - `rt`: Enables `tokio::spawn`, the basic (current thread) scheduler,
//! and non-scheduler utilities.
//! - `rt-multi-thread`: Enables the heavier, multi-threaded, work-stealing scheduler.
//! - `io-util`: Enables the IO based `Ext` traits.
//! - `io-std`: Enable `Stdout`, `Stdin` and `Stderr` types.
//! - `net`: Enables `tokio::net` types such as `TcpStream`, `UnixStream` and `UdpSocket`.
//! - `time`: Enables `tokio::time` types and allows the schedulers to enable
//! the built in timer.
//! - `process`: Enables `tokio::process` types.
//! - `macros`: Enables `#[tokio::main]` and `#[tokio::test]` macros.
//! - `sync`: Enables all `tokio::sync` types.
//! - `stream`: Enables optional `Stream` implementations for types within Tokio.
//! - `signal`: Enables all `tokio::signal` types.
//! - `fs`: Enables `tokio::fs` types.
//! - `test-util`: Enables testing based infrastructure for the Tokio runtime.
//!
//! _Note: `AsyncRead` and `AsyncWrite` traits do not require any features and are
//! always available._
//!
//! ### Internal features
//!
//! These features do not expose any new API, but influence internal
//! implementation aspects of Tokio, and can pull in additional
//! dependencies.
//!
//! - `parking_lot`: As a potential optimization, use the _parking_lot_ crate's
//! synchronization primitives internally. MSRV may increase according to the
//! _parking_lot_ release in use.
//!
//! [feature flags]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-features-section

// Includes re-exports used by macros.
//
Expand Down Expand Up @@ -359,7 +357,7 @@ cfg_process! {
pub mod process;
}

#[cfg(any(feature = "dns", feature = "fs", feature = "io-std"))]
#[cfg(any(feature = "net", feature = "fs", feature = "io-std"))]
mod blocking;

cfg_rt! {
Expand Down
13 changes: 1 addition & 12 deletions tokio/src/macros/cfg.rs
Expand Up @@ -6,7 +6,7 @@ macro_rules! cfg_block_on {
$(
#[cfg(any(
feature = "fs",
feature = "dns",
feature = "net",
feature = "io-std",
feature = "rt",
))]
Expand All @@ -32,16 +32,6 @@ macro_rules! cfg_atomic_waker_impl {
}
}

macro_rules! cfg_dns {
($($item:item)*) => {
$(
#[cfg(feature = "dns")]
#[cfg_attr(docsrs, doc(cfg(feature = "dns")))]
$item
)*
}
}

macro_rules! cfg_fs {
($($item:item)*) => {
$(
Expand Down Expand Up @@ -338,7 +328,6 @@ macro_rules! cfg_coop {
($($item:item)*) => {
$(
#[cfg(any(
feature = "dns",
feature = "fs",
feature = "io-std",
feature = "net",
Expand Down
19 changes: 10 additions & 9 deletions tokio/src/net/addr.rs
Expand Up @@ -9,7 +9,7 @@ use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV
///
/// Implementations of `ToSocketAddrs` for string types require a DNS lookup.
/// These implementations are only provided when Tokio is used with the
/// **`dns`** feature flag.
/// **`net`** feature flag.
///
/// # Calling
///
Expand All @@ -23,12 +23,13 @@ pub trait ToSocketAddrs: sealed::ToSocketAddrsPriv {}

type ReadyFuture<T> = future::Ready<io::Result<T>>;

#[cfg(any(feature = "dns", feature = "net"))]
pub(crate) fn to_socket_addrs<T>(arg: T) -> T::Future
where
T: ToSocketAddrs,
{
arg.to_socket_addrs(sealed::Internal)
cfg_net! {
pub(crate) fn to_socket_addrs<T>(arg: T) -> T::Future
where
T: ToSocketAddrs,
{
arg.to_socket_addrs(sealed::Internal)
}
}

// ===== impl &impl ToSocketAddrs =====
Expand Down Expand Up @@ -143,7 +144,7 @@ impl sealed::ToSocketAddrsPriv for &[SocketAddr] {
}
}

cfg_dns! {
cfg_net! {
// ===== impl str =====

impl ToSocketAddrs for str {}
Expand Down Expand Up @@ -256,7 +257,7 @@ pub(crate) mod sealed {
#[allow(missing_debug_implementations)]
pub struct Internal;

cfg_dns! {
cfg_net! {
use crate::blocking::JoinHandle;

use std::option;
Expand Down
2 changes: 1 addition & 1 deletion tokio/src/net/lookup_host.rs
@@ -1,4 +1,4 @@
cfg_dns! {
cfg_net! {
use crate::net::addr::{self, ToSocketAddrs};

use std::io;
Expand Down
4 changes: 1 addition & 3 deletions tokio/src/net/mod.rs
Expand Up @@ -27,12 +27,10 @@ mod addr;
pub(crate) use addr::to_socket_addrs;
pub use addr::ToSocketAddrs;

cfg_dns! {
cfg_net! {
mod lookup_host;
pub use lookup_host::lookup_host;
}

cfg_net! {
pub mod tcp;
pub use tcp::listener::TcpListener;
pub use tcp::socket::TcpSocket;
Expand Down
21 changes: 1 addition & 20 deletions tokio/src/net/tcp/listener.rs
Expand Up @@ -80,7 +80,7 @@ impl TcpListener {
/// method.
///
/// The address type can be any implementor of the [`ToSocketAddrs`] trait.
/// Note that strings only implement this trait when the **`dns`** feature
/// Note that strings only implement this trait when the **`net`** feature
/// is enabled, as strings may contain domain names that need to be resolved.
///
/// If `addr` yields multiple addresses, bind will be attempted with each of
Expand Down Expand Up @@ -109,25 +109,6 @@ impl TcpListener {
/// Ok(())
/// }
/// ```
///
/// Without the `dns` feature:
///
/// ```no_run
/// use tokio::net::TcpListener;
/// use std::net::Ipv4Addr;
///
/// use std::io;
///
/// #[tokio::main]
/// async fn main() -> io::Result<()> {
/// let listener = TcpListener::bind((Ipv4Addr::new(127, 0, 0, 1), 2345)).await?;
///
/// // use the listener
///
/// # let _ = listener;
/// Ok(())
/// }
/// ```
pub async fn bind<A: ToSocketAddrs>(addr: A) -> io::Result<TcpListener> {
let addrs = to_socket_addrs(addr).await?;

Expand Down
22 changes: 1 addition & 21 deletions tokio/src/net/tcp/stream.rs
Expand Up @@ -61,7 +61,7 @@ impl TcpStream {
///
/// `addr` is an address of the remote host. Anything which implements the
/// [`ToSocketAddrs`] trait can be supplied as the address. Note that
/// strings only implement this trait when the **`dns`** feature is enabled,
/// strings only implement this trait when the **`net`** feature is enabled,
/// as strings may contain domain names that need to be resolved.
///
/// If `addr` yields multiple addresses, connect will be attempted with each
Expand Down Expand Up @@ -90,26 +90,6 @@ impl TcpStream {
/// }
/// ```
///
/// Without the `dns` feature:
///
/// ```no_run
/// use tokio::net::TcpStream;
/// use tokio::prelude::*;
/// use std::error::Error;
/// use std::net::Ipv4Addr;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn Error>> {
/// // Connect to a peer
/// let mut stream = TcpStream::connect((Ipv4Addr::new(127, 0, 0, 1), 8080)).await?;
///
/// // Write some data.
/// stream.write_all(b"hello world!").await?;
///
/// Ok(())
/// }
/// ```
///
/// The [`write_all`] method is defined on the [`AsyncWriteExt`] trait.
///
/// [`write_all`]: fn@crate::io::AsyncWriteExt::write_all
Expand Down

0 comments on commit 1923350

Please sign in to comment.