Files
tokio/src/lib.rs
T

113 lines
3.5 KiB
Rust
Raw Normal View History

2016-09-02 11:07:52 -07:00
//! `Future`-powered I/O at the core of Tokio
2016-07-30 17:53:12 -07:00
//!
2017-12-05 16:55:25 +01:00
//! This crate uses the [`futures`] crate to provide an event loop ("reactor
2017-10-24 17:20:46 -07:00
//! core") which can be used to drive I/O like TCP and UDP. All asynchronous I/O
2017-12-05 16:55:25 +01:00
//! is powered by the [`mio`] crate.
//!
//! [`futures`]: ../futures/index.html
//! [`mio`]: ../mio/index.html
2016-09-02 11:07:52 -07:00
//!
//! The concrete types provided in this crate are relatively bare bones but are
//! intended to be the essential foundation for further projects needing an
//! event loop. In this crate you'll find:
//!
2017-12-05 16:55:25 +01:00
//! * TCP, both streams and listeners.
//! * UDP sockets.
//! * An event loop to run futures.
2016-09-02 11:07:52 -07:00
//!
//! More functionality is likely to be added over time, but otherwise the crate
2017-12-05 16:55:25 +01:00
//! is intended to be flexible, with the [`PollEvented`] type accepting any
//! type that implements [`mio::Evented`]. For example, the [`tokio-uds`] crate
//! uses [`PollEvented`] to provide support for Unix domain sockets.
//!
//! [`PollEvented`]: ./reactor/struct.PollEvented.html
//! [`mio::Evented`]: ../mio/event/trait.Evented.html
//! [`tokio-uds`]: https://crates.io/crates/tokio-uds
2016-09-02 11:07:52 -07:00
//!
//! Some other important tasks covered by this crate are:
//!
//! * All I/O is futures-aware. If any action in this crate returns "not ready"
//! or "would block", then the current future task is scheduled to receive a
//! notification when it would otherwise make progress.
//!
2017-01-11 09:14:50 -08:00
//! You can find more extensive documentation in terms of tutorials at
//! [https://tokio.rs](https://tokio.rs).
//!
2016-09-02 11:07:52 -07:00
//! # Examples
//!
//! A simple TCP echo server:
//!
//! ```no_run
//! extern crate futures;
2017-10-25 10:54:54 -07:00
//! extern crate futures_cpupool;
2017-10-24 16:30:16 -07:00
//! extern crate tokio;
2017-08-16 19:17:12 +08:00
//! extern crate tokio_io;
2016-09-02 11:07:52 -07:00
//!
2017-01-10 10:02:15 -08:00
//! use futures::{Future, Stream};
2017-10-25 10:54:54 -07:00
//! use futures::future::Executor;
//! use futures_cpupool::CpuPool;
2017-08-16 19:17:12 +08:00
//! use tokio_io::AsyncRead;
//! use tokio_io::io::copy;
2017-10-24 16:30:16 -07:00
//! use tokio::net::TcpListener;
2017-12-05 09:02:07 -08:00
//! use tokio::reactor::Reactor;
2016-09-02 11:07:52 -07:00
//!
//! fn main() {
2017-12-05 16:55:25 +01:00
//! // Create the event loop that will drive this server.
2017-12-05 09:02:07 -08:00
//! let mut core = Reactor::new().unwrap();
2017-01-10 10:02:15 -08:00
//! let handle = core.handle();
//!
2017-10-25 10:54:54 -07:00
//! let pool = CpuPool::new_num_cpus();
//!
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-05 16:55:25 +01:00
//! let listener = TcpListener::bind(&addr, &handle)
//! .expect("unable to bind TCP listener");
2017-01-10 10:02:15 -08:00
//!
//! // Pull out a stream of sockets for incoming connections
2017-02-08 20:23:54 +01:00
//! let server = listener.incoming().for_each(|(sock, _)| {
2017-01-10 10:02:15 -08:00
//! // Split up the reading and writing parts of the
2017-12-05 16:55:25 +01:00
//! // socket.
2017-01-10 10:02:15 -08:00
//! let (reader, writer) = sock.split();
//!
//! // A future that echos the data and returns how
//! // many bytes were copied...
//! let bytes_copied = copy(reader, writer);
//!
2017-12-05 16:55:25 +01:00
//! // ... after which we'll print what happened.
2017-01-10 10:02:15 -08:00
//! let handle_conn = bytes_copied.map(|amt| {
2017-08-16 19:17:12 +08:00
//! println!("wrote {:?} bytes", amt)
2017-01-10 10:02:15 -08:00
//! }).map_err(|err| {
2017-12-05 16:55:25 +01:00
//! eprintln!("IO error {:?}", err)
2017-01-10 10:02:15 -08:00
//! });
//!
2017-12-05 16:55:25 +01:00
//! // Spawn the future as a concurrent task.
2017-10-25 10:54:54 -07:00
//! pool.execute(handle_conn).unwrap();
2016-09-07 16:11:19 -07:00
//!
//! Ok(())
2016-09-02 11:07:52 -07:00
//! });
//!
2017-12-05 16:55:25 +01:00
//! // Spin up the server on the event loop.
2017-01-10 10:02:15 -08:00
//! core.run(server).unwrap();
2016-09-02 11:07:52 -07:00
//! }
//! ```
2016-07-30 17:53:12 -07:00
2016-12-19 22:41:31 -08:00
#![doc(html_root_url = "https://docs.rs/tokio-core/0.1")]
2016-07-30 17:53:12 -07:00
#![deny(missing_docs)]
2017-10-25 17:27:24 -07:00
#![deny(warnings)]
2017-12-06 17:19:21 +01:00
#![warn(missing_debug_implementations)]
2016-07-30 17:53:12 -07:00
2017-02-05 17:06:57 -08:00
extern crate bytes;
#[macro_use]
2016-07-30 17:53:12 -07:00
extern crate futures;
2017-02-05 17:06:57 -08:00
extern crate iovec;
2016-07-30 17:53:12 -07:00
extern crate mio;
extern crate slab;
2017-10-24 16:41:39 -07:00
#[macro_use]
2017-02-05 17:06:57 -08:00
extern crate tokio_io;
2016-07-30 17:53:12 -07:00
#[macro_use]
extern crate log;
2016-09-02 11:07:52 -07:00
pub mod net;
pub mod reactor;