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,
|
|
|
|
|
))]
|
|
|
|
|
|
2018-03-01 21:48:18 -08:00
|
|
|
//! A runtime for writing reliable, asynchronous, and slim applications.
|
2016-07-30 17:53:12 -07:00
|
|
|
//!
|
2018-03-01 21:48:18 -08: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
|
|
|
//!
|
2018-03-01 21:48:18 -08: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,
|
2018-03-01 21:48:18 -08:00
|
|
|
//! 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
|
|
|
//!
|
2018-03-01 21:48:18 -08:00
|
|
|
//! Guide level documentation is found on the [website].
|
2016-09-02 11:07:52 -07:00
|
|
|
//!
|
2018-03-01 21:48:18 -08: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
|
|
|
//!
|
2018-03-01 21:48:18 -08: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();
|
2017-12-12 18:32:50 -06:00
|
|
|
//! 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
|
2018-02-21 07:42:22 -08:00
|
|
|
//! let server = listener.incoming()
|
2018-03-01 21:48:18 -08:00
|
|
|
//! .map_err(|e| eprintln!("accept failed = {:?}", e))
|
2018-02-21 07:42:22 -08:00
|
|
|
//! .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
|
|
|
//! });
|
|
|
|
|
//!
|
2018-02-21 07:42:22 -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
|
|
|
|
2019-01-04 11:42:33 -08: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;
|
2019-01-04 11:42:33 -08:00
|
|
|
|
|
|
|
|
#[cfg(feature = "io")]
|
|
|
|
|
extern crate bytes;
|
|
|
|
|
#[cfg(feature = "reactor")]
|
2016-07-30 17:53:12 -07:00
|
|
|
extern crate mio;
|
2019-01-04 11:42:33 -08:00
|
|
|
#[cfg(feature = "rt-full")]
|
2018-10-03 03:19:27 +02:00
|
|
|
extern crate num_cpus;
|
2019-01-04 11:42:33 -08:00
|
|
|
#[cfg(feature = "rt-full")]
|
2018-06-12 19:26:03 +02:00
|
|
|
extern crate tokio_current_thread;
|
2019-01-04 11:42:33 -08:00
|
|
|
#[cfg(feature = "io")]
|
2017-02-05 17:06:57 -08:00
|
|
|
extern crate tokio_io;
|
2019-01-04 11:42:33 -08:00
|
|
|
#[cfg(feature = "codec")]
|
2018-08-14 21:18:54 +03:00
|
|
|
extern crate tokio_codec;
|
2019-01-04 11:42:33 -08:00
|
|
|
#[cfg(feature = "fs")]
|
2018-05-02 11:19:58 -07:00
|
|
|
extern crate tokio_fs;
|
2019-01-04 11:42:33 -08:00
|
|
|
#[cfg(feature = "reactor")]
|
2018-03-02 13:51:34 -08:00
|
|
|
extern crate tokio_reactor;
|
2019-01-04 11:42:33 -08:00
|
|
|
#[cfg(feature = "rt-full")]
|
2018-02-21 07:42:22 -08:00
|
|
|
extern crate tokio_threadpool;
|
2019-01-22 11:37:26 -08:00
|
|
|
#[cfg(feature = "sync")]
|
|
|
|
|
extern crate tokio_sync;
|
2019-01-04 11:42:33 -08:00
|
|
|
#[cfg(feature = "timer")]
|
2018-03-30 11:50:02 -07:00
|
|
|
extern crate tokio_timer;
|
2019-01-04 11:42:33 -08:00
|
|
|
#[cfg(feature = "tcp")]
|
2018-03-15 03:38:59 +11:00
|
|
|
extern crate tokio_tcp;
|
2019-01-04 11:42:33 -08:00
|
|
|
#[cfg(feature = "udp")]
|
2018-03-15 03:38:59 +11:00
|
|
|
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;
|
|
|
|
|
|
2019-01-04 11:42:33 -08:00
|
|
|
#[cfg(all(unix, feature = "uds"))]
|
2018-08-16 06:26:10 +02:00
|
|
|
extern crate tokio_uds;
|
|
|
|
|
|
2019-01-04 11:42:33 -08:00
|
|
|
#[cfg(feature = "timer")]
|
2018-06-06 16:04:39 -07:00
|
|
|
pub mod clock;
|
2019-01-04 11:42:33 -08:00
|
|
|
#[cfg(feature = "codec")]
|
2018-09-26 15:28:57 -07:00
|
|
|
pub mod codec;
|
2019-01-04 11:42:33 -08:00
|
|
|
#[cfg(feature = "fs")]
|
2018-05-02 11:19:58 -07:00
|
|
|
pub mod fs;
|
2019-01-04 11:42:33 -08:00
|
|
|
#[cfg(feature = "io")]
|
2018-09-26 15:28:57 -07:00
|
|
|
pub mod io;
|
2019-01-04 11:42:33 -08:00
|
|
|
#[cfg(any(feature = "tcp", feature = "udp", feature = "uds"))]
|
2016-09-02 11:07:52 -07:00
|
|
|
pub mod net;
|
2018-09-26 15:28:57 -07:00
|
|
|
pub mod prelude;
|
2019-01-04 11:42:33 -08:00
|
|
|
#[cfg(feature = "reactor")]
|
2016-09-02 11:07:52 -07:00
|
|
|
pub mod reactor;
|
2019-01-22 11:37:26 -08:00
|
|
|
#[cfg(feature = "sync")]
|
|
|
|
|
pub mod sync;
|
2019-01-04 11:42:33 -08:00
|
|
|
#[cfg(feature = "timer")]
|
2018-03-30 11:50:02 -07:00
|
|
|
pub mod timer;
|
|
|
|
|
pub mod util;
|
2018-02-21 07:42:22 -08:00
|
|
|
|
2019-01-04 11:42:33 -08:00
|
|
|
if_runtime! {
|
|
|
|
|
extern crate tokio_executor;
|
|
|
|
|
pub mod executor;
|
|
|
|
|
pub mod runtime;
|
|
|
|
|
|
|
|
|
|
pub use executor::spawn;
|
|
|
|
|
pub use runtime::run;
|
|
|
|
|
}
|
2018-02-28 09:03:13 -08:00
|
|
|
|
2018-09-26 15:28:57 -07:00
|
|
|
// ===== 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;
|