Files
tokio/src/lib.rs
T

156 lines
4.2 KiB
Rust
Raw Normal View History

2019-01-25 10:20:09 -08:00
#![doc(html_root_url = "https://docs.rs/tokio/0.1.15")]
2018-09-26 22:32:51 -07:00
#![deny(missing_docs, warnings, missing_debug_implementations)]
#![cfg_attr(feature = "async-await-preview", feature(
async_await,
await_macro,
futures_api,
))]
//! A runtime for writing reliable, asynchronous, and slim applications.
2016-07-30 17:53:12 -07:00
//!
//! Tokio is an event-driven, non-blocking I/O platform for writing asynchronous
//! applications with the Rust programming language. At a high level, it
//! provides a few major components:
2017-12-05 16:55:25 +01:00
//!
//! * A multi threaded, work-stealing based task [scheduler][runtime].
2018-07-22 13:35:30 -07:00
//! * A [reactor] backed by the operating system's event queue (epoll, kqueue,
//! IOCP, etc...).
//! * Asynchronous [TCP and UDP][net] sockets.
2018-05-02 11:19:58 -07:00
//! * Asynchronous [filesystem][fs] operations.
2018-03-30 11:50:02 -07:00
//! * [Timer][timer] API for scheduling work in the future.
2016-09-02 11:07:52 -07:00
//!
2018-03-30 11:50:02 -07:00
//! Tokio is built using [futures] as the abstraction for managing the
//! complexity of asynchronous programming.
2016-09-02 11:07:52 -07:00
//!
//! Guide level documentation is found on the [website].
2016-09-02 11:07:52 -07:00
//!
//! [website]: https://tokio.rs/docs/getting-started/hello-world/
2018-07-22 13:35:30 -07:00
//! [futures]: http://docs.rs/futures/0.1
2017-01-11 09:14:50 -08:00
//!
2016-09-02 11:07:52 -07:00
//! # Examples
//!
//! A simple TCP echo server:
//!
//! ```no_run
2017-10-24 16:30:16 -07:00
//! extern crate tokio;
2016-09-02 11:07:52 -07:00
//!
//! use tokio::prelude::*;
//! use tokio::io::copy;
2017-10-24 16:30:16 -07:00
//! use tokio::net::TcpListener;
2016-09-02 11:07:52 -07:00
//!
//! fn main() {
2017-12-05 16:55:25 +01:00
//! // Bind the server's socket.
2017-01-10 10:02:15 -08:00
//! let addr = "127.0.0.1:12345".parse().unwrap();
//! let listener = TcpListener::bind(&addr)
2017-12-05 16:55:25 +01:00
//! .expect("unable to bind TCP listener");
2017-01-10 10:02:15 -08:00
//!
//! // Pull out a stream of sockets for incoming connections
//! let server = listener.incoming()
//! .map_err(|e| eprintln!("accept failed = {:?}", e))
//! .for_each(|sock| {
//! // Split up the reading and writing parts of the
//! // socket.
//! let (reader, writer) = sock.split();
//!
//! // A future that echos the data and returns how
//! // many bytes were copied...
//! let bytes_copied = copy(reader, writer);
//!
//! // ... after which we'll print what happened.
//! let handle_conn = bytes_copied.map(|amt| {
//! println!("wrote {:?} bytes", amt)
//! }).map_err(|err| {
//! eprintln!("IO error {:?}", err)
//! });
//!
//! // Spawn the future as a concurrent task.
//! tokio::spawn(handle_conn)
2017-01-10 10:02:15 -08:00
//! });
//!
//! // Start the Tokio runtime
//! tokio::run(server);
2016-09-02 11:07:52 -07:00
//! }
//! ```
2016-07-30 17:53:12 -07:00
macro_rules! if_runtime {
($($i:item)*) => ($(
#[cfg(any(feature = "rt-full"))]
$i
)*)
}
2019-01-24 21:50:34 +02:00
#[macro_use]
2016-07-30 17:53:12 -07:00
extern crate futures;
#[cfg(feature = "io")]
extern crate bytes;
#[cfg(feature = "reactor")]
2016-07-30 17:53:12 -07:00
extern crate mio;
#[cfg(feature = "rt-full")]
2018-10-03 03:19:27 +02:00
extern crate num_cpus;
#[cfg(feature = "rt-full")]
2018-06-12 19:26:03 +02:00
extern crate tokio_current_thread;
#[cfg(feature = "io")]
2017-02-05 17:06:57 -08:00
extern crate tokio_io;
#[cfg(feature = "codec")]
extern crate tokio_codec;
#[cfg(feature = "fs")]
2018-05-02 11:19:58 -07:00
extern crate tokio_fs;
#[cfg(feature = "reactor")]
extern crate tokio_reactor;
#[cfg(feature = "rt-full")]
extern crate tokio_threadpool;
#[cfg(feature = "sync")]
extern crate tokio_sync;
#[cfg(feature = "timer")]
2018-03-30 11:50:02 -07:00
extern crate tokio_timer;
#[cfg(feature = "tcp")]
extern crate tokio_tcp;
#[cfg(feature = "udp")]
extern crate tokio_udp;
2016-07-30 17:53:12 -07:00
2018-09-26 10:10:47 -07:00
#[cfg(feature = "async-await-preview")]
extern crate tokio_async_await;
#[cfg(all(unix, feature = "uds"))]
2018-08-16 06:26:10 +02:00
extern crate tokio_uds;
#[cfg(feature = "timer")]
pub mod clock;
#[cfg(feature = "codec")]
pub mod codec;
#[cfg(feature = "fs")]
2018-05-02 11:19:58 -07:00
pub mod fs;
#[cfg(feature = "io")]
pub mod io;
#[cfg(any(feature = "tcp", feature = "udp", feature = "uds"))]
2016-09-02 11:07:52 -07:00
pub mod net;
pub mod prelude;
#[cfg(feature = "reactor")]
2016-09-02 11:07:52 -07:00
pub mod reactor;
#[cfg(feature = "sync")]
pub mod sync;
#[cfg(feature = "timer")]
2018-03-30 11:50:02 -07:00
pub mod timer;
pub mod util;
if_runtime! {
extern crate tokio_executor;
pub mod executor;
pub mod runtime;
pub use executor::spawn;
pub use runtime::run;
}
// ===== Experimental async/await support =====
2018-09-26 10:10:47 -07:00
#[cfg(feature = "async-await-preview")]
mod async_await;
#[cfg(feature = "async-await-preview")]
pub use async_await::{run_async, spawn_async};
#[cfg(feature = "async-await-preview")]
pub use tokio_async_await::await;