summaryrefslogtreecommitdiffstats
path: root/tokio/src/lib.rs
blob: f42a8a1e98c541d40d1ffba0024fa5740e8833ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#![doc(html_root_url = "https://docs.rs/tokio/0.2.0-alpha.6")]
#![warn(
    missing_debug_implementations,
    missing_docs,
    rust_2018_idioms,
    unreachable_pub
)]
#![deny(intra_doc_link_resolution_failure)]
#![doc(test(
    no_crate_inject,
    attr(deny(warnings, rust_2018_idioms), allow(dead_code, unused_variables))
))]

//! A runtime for writing reliable, asynchronous, and slim applications.
//!
//! 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:
//!
//! * A multi threaded, work-stealing based task [scheduler][runtime].
//! * A [driver] backed by the operating system's event queue (epoll, kqueue,
//!   IOCP, etc...).
//! * Asynchronous [TCP and UDP][net] sockets.
//! * Asynchronous [filesystem][fs] operations.
//! * [Timer][timer] API for scheduling work in the future.
//!
//! Guide level documentation is found on the [website].
//!
//! [driver]: tokio::net::driver
//! [website]: https://tokio.rs/docs/
//!
//! # Examples
//!
//! A simple TCP echo server:
//!
//! ```no_run
//! use tokio::net::TcpListener;
//! use tokio::prelude::*;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     let mut listener = TcpListener::bind("127.0.0.1:8080").await?;
//!
//!     loop {
//!         let (mut socket, _) = listener.accept().await?;
//!
//!         tokio::spawn(async move {
//!             let mut buf = [0; 1024];
//!
//!             // In a loop, read data from the socket and write the data back.
//!             loop {
//!                 let n = match socket.read(&mut buf).await {
//!                     // socket closed
//!                     Ok(n) if n == 0 => return,
//!                     Ok(n) => n,
//!                     Err(e) => {
//!                         println!("failed to read from socket; err = {:?}", e);
//!                         return;
//!                     }
//!                 };
//!
//!                 // Write the data back
//!                 if let Err(e) = socket.write_all(&buf[0..n]).await {
//!                     println!("failed to write to socket; err = {:?}", e);
//!                     return;
//!                 }
//!             }
//!         });
//!     }
//! }
//! ```
macro_rules! if_runtime {
    ($($i:item)*) => ($(
        #[cfg(any(
            feature = "rt-full",
            feature = "rt-current-thread",
        ))]
        $i
    )*)
}

#[cfg(all(loom, test))]
macro_rules! thread_local {
    ($($tts:tt)+) => { loom::thread_local!{ $($tts)+ } }
}

#[cfg(feature = "timer")]
pub mod clock;

#[cfg(feature = "codec")]
pub mod codec;

#[cfg(feature = "fs")]
pub mod fs;

pub mod future;

#[cfg(feature = "io-traits")]
pub mod io;

#[cfg(feature = "net-driver")]
pub mod net;

#[cfg(feature = "net-driver")]
mod loom;

pub mod prelude;

#[cfg(all(feature = "process", not(loom)))]
pub mod process;

#[cfg(feature = "signal")]
#[cfg(not(loom))]
pub mod signal;

pub mod stream;

#[cfg(feature = "sync")]
pub mod sync;

#[cfg(feature = "timer")]
pub mod timer;

#[cfg(feature = "executor-core")]
pub mod executor;

if_runtime! {
    pub mod runtime;

    #[doc(inline)]
    pub use crate::executor::spawn;

    #[cfg(not(test))] // Work around for rust-lang/rust#62127
    #[cfg(feature = "macros")]
    #[doc(inline)]
    pub use tokio_macros::main;

    #[cfg(feature = "macros")]
    #[doc(inline)]
    pub use tokio_macros::test;
}

#[cfg(feature = "io-util")]
#[cfg(test)]
fn is_unpin<T: Unpin>() {}