async_std/
lib.rs

1//! # Async version of the Rust standard library
2//!
3//! `async-std` is a foundation of portable Rust software, a set of minimal and battle-tested
4//! shared abstractions for the [broader Rust ecosystem][crates.io]. It offers std types, like
5//! [`Future`] and [`Stream`], library-defined [operations on language primitives](#primitives),
6//! [standard macros](#macros), [I/O] and [multithreading], among [many other things][other].
7//!
8//! `async-std` is available from [crates.io]. Once included, `async-std` can be accessed
9//! in [`use`] statements through the path `async_std`, as in [`use async_std::future`].
10//!
11//! [I/O]: io/index.html
12//! [multithreading]: task/index.html
13//! [other]: #what-is-in-the-standard-library-documentation
14//! [`use`]: https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html
15//! [`use async_std::future`]: future/index.html
16//! [crates.io]: https://crates.io
17//! [`Future`]: future/trait.Future.html
18//! [`Stream`]: stream/trait.Stream.html
19//!
20//! # How to read this documentation
21//!
22//! If you already know the name of what you are looking for, the fastest way to
23//! find it is to use the <a href="#" onclick="focusSearchBar();">search
24//! bar</a> at the top of the page.
25//!
26//! Otherwise, you may want to jump to one of these useful sections:
27//!
28//! * [`async_std::*` modules](#modules)
29//! * [Async macros](#macros)
30//! * [The Async Prelude](prelude/index.html)
31//! * [Cargo.toml feature flags](#features)
32//! * [Examples](#examples)
33//!
34//! If this is your first time, the documentation for `async-std` is
35//! written to be casually perused. Clicking on interesting things should
36//! generally lead you to interesting places. Still, there are important bits
37//! you don't want to miss, so read on for a tour of the `async-std` and
38//! its documentation!
39//!
40//! Once you are familiar with the contents of `async-std` you may
41//! begin to find the verbosity of the prose distracting. At this stage in your
42//! development you may want to press the `[-]` button near the top of the
43//! page to collapse it into a more skimmable view.
44//!
45//! While you are looking at that `[-]` button also notice the `[src]`
46//! button. Rust's API documentation comes with the source code and you are
47//! encouraged to read it. The `async-std` source is generally high
48//! quality and a peek behind the curtains is often enlightening.
49//!
50//! Modules in this crate are organized in the same way as in `std`, except blocking
51//! functions have been replaced with async functions and threads have been replaced with
52//! lightweight tasks.
53//!
54//! You can find more information, reading materials, and other resources here:
55//!
56//! * [The async-std website](https://async.rs/)
57//! * [The async-std book](https://book.async.rs)
58//! * [GitHub repository](https://github.com/async-rs/async-std)
59//! * [List of code examples](https://github.com/async-rs/async-std/tree/master/examples)
60//! * [Discord chat](https://discord.gg/JvZeVNe)
61//!
62//! # What is in the `async-std` documentation?
63//!
64//! First, `async-std` is divided into a number of focused
65//! modules, [all listed further down this page](#modules). These modules are
66//! the bedrock upon which async Rust is forged, and they have mighty names
67//! like [`async_std::os`] and [`async_std::task`]. Modules' documentation
68//! typically includes an overview of the module along with examples, and are
69//! a smart place to start familiarizing yourself with the library.
70//!
71//! Second, `async-std` defines [The Async Prelude], a small collection
72//! of items - mostly traits - that should be imported into every module of
73//! every async crate. The traits in the prelude are pervasive, making the
74//! prelude documentation a good entry point to learning about the library.
75//!
76//! [The Async Prelude]: prelude/index.html
77//! [`async_std::os`]: os/index.html
78//! [`async_std::task`]: task/index.html
79//!
80//! And finally, `async-std` exports a number of async macros, and
81//! [lists them on this page](#macros).
82//!
83//! # Contributing changes to the documentation
84//!
85//! Check out `async-std`'s contribution guidelines [here](https://async.rs/contribute).
86//! The source for this documentation can be found on [GitHub](https://github.com/async-rs).
87//! To contribute changes, make sure you read the guidelines first, then submit
88//! pull requests for your suggested changes.
89//!
90//! Contributions are appreciated! If you see a part of the docs that can be
91//! improved, submit a PR, or chat with us first on
92//! [Discord](https://discord.gg/JvZeVNe).
93//!
94//! # A tour of `async-std`
95//!
96//! The rest of this crate documentation is dedicated to pointing out notable
97//! features of `async-std`.
98//!
99//! ## Platform abstractions and I/O
100//!
101//! Besides basic data types, `async-std` is largely concerned with
102//! abstracting over differences in common platforms, most notably Windows and
103//! Unix derivatives.
104//!
105//! Common types of I/O, including [files], [TCP], [UDP], are defined in the
106//! [`io`], [`fs`], and [`net`] modules.
107//!
108//! The [`task`] module contains `async-std`'s task abstractions. [`sync`]
109//! contains further primitive shared memory types, including [`channel`],
110//! which contains the channel types for message passing.
111//!
112//! [files]: fs/struct.File.html
113//! [TCP]: net/struct.TcpStream.html
114//! [UDP]: net/struct.UdpSocket.html
115//! [`io`]: fs/struct.File.html
116//! [`sync`]: sync/index.html
117//! [`channel`]: sync/fn.channel.html
118//!
119//! ## Timeouts, intervals, and delays
120//!
121//! `async-std` provides several methods to manipulate time:
122//!
123//! * [`task::sleep`] to wait for a duration to pass without blocking.
124//! * [`stream::interval`] for emitting an event at a set interval.
125//! * [`future::timeout`] to time-out futures if they don't resolve within a
126//!   set interval.
127//!
128//! [`task::sleep`]: task/fn.sleep.html
129//! [`stream::interval`]: stream/fn.interval.html
130//! [`future::timeout`]: future/fn.timeout.html
131//!
132//! # Examples
133//!
134//! All examples require the [`"attributes"` feature](#features) to be enabled.
135//! This feature is not enabled by default because it significantly impacts
136//! compile times. See [`task::block_on`] for an alternative way to start
137//! executing tasks.
138//!
139//! Call an async function from the main function:
140//!
141//! ```
142//! async fn say_hello() {
143//!     println!("Hello, world!");
144//! }
145//!
146//! #[tokio::main]
147//! async fn main() {
148//!     say_hello().await;
149//! }
150//! ```
151//!
152//! Await two futures concurrently, and return a tuple of their output:
153//!
154//! ```ignore
155//! use async_std::prelude::*;
156//!
157//! #[tokio::main]
158//! async fn main() {
159//!     let a = async { 1u8 };
160//!     let b = async { 2u8 };
161//!     assert_eq!(a.join(b).await, (1u8, 2u8))
162//! }
163//! ```
164//!
165//! Create a UDP server that echoes back each received message to the sender:
166//!
167//! ```no_run
168//! use async_std::net::UdpSocket;
169//!
170//! #[tokio::main]
171//! async fn main() -> std::io::Result<()> {
172//!     let socket = UdpSocket::bind("127.0.0.1:8080").await?;
173//!     println!("Listening on {}", socket.local_addr()?);
174//!
175//!     let mut buf = vec![0u8; 1024];
176//!
177//!     loop {
178//!         let (recv, peer) = socket.recv_from(&mut buf).await?;
179//!         let sent = socket.send_to(&buf[..recv], &peer).await?;
180//!         println!("Sent {} out of {} bytes to {}", sent, recv, peer);
181//!     }
182//! }
183//! ```
184//! [`task::block_on`]: task/fn.block_on.html
185//!
186//! # Features
187//!
188//! Items marked with
189//! <span
190//!   class="module-item stab portability"
191//!   style="display: inline; border-radius: 3px; padding: 2px; font-size: 80%; line-height: 1.2;"
192//! ><code>unstable</code></span>
193//! are available only when the `unstable` Cargo feature is enabled:
194//!
195//! ```toml
196//! [dependencies.async-std]
197//! version = "1.0.0"
198//! features = ["unstable"]
199//! ```
200//!
201//! Items marked with
202//! <span
203//!   class="module-item stab portability"
204//!   style="display: inline; border-radius: 3px; padding: 2px; font-size: 80%; line-height: 1.2;"
205//! ><code>attributes</code></span>
206//! are available only when the `attributes` Cargo feature is enabled:
207//!
208//! ```toml
209//! [dependencies.async-std]
210//! version = "1.0.0"
211//! features = ["attributes"]
212//! ```
213//!
214//! Additionally it's possible to only use the core traits and combinators by
215//! only enabling the `std` Cargo feature:
216//!
217//! ```toml
218//! [dependencies.async-std]
219//! version = "1.0.0"
220//! default-features = false
221//! features = ["std"]
222//! ```
223
224#![cfg_attr(feature = "docs", feature(doc_cfg))]
225#![warn(missing_docs, missing_debug_implementations, rust_2018_idioms)]
226#![allow(clippy::mutex_atomic, clippy::module_inception)]
227#![doc(test(attr(deny(rust_2018_idioms, warnings))))]
228#![doc(test(attr(allow(unused_extern_crates, unused_variables))))]
229#![doc(html_logo_url = "https://async.rs/images/logo--hero.svg")]
230#![recursion_limit = "2048"]
231
232#[macro_use]
233mod utils;
234
235#[cfg(feature = "attributes")]
236#[cfg_attr(feature = "docs", doc(cfg(attributes)))]
237#[doc(inline)]
238pub use tokio_async_attributes::{main, test};
239
240#[cfg(feature = "std")]
241mod macros;
242#[cfg(feature = "std")]
243pub use tokio::task_local;
244
245cfg_std! {
246    pub mod future;
247    pub mod io;
248    pub mod os;
249    pub mod prelude;
250    pub mod stream;
251    pub mod sync;
252    pub mod task;
253    pub use tokio::runtime;
254}
255
256cfg_default! {
257    pub mod fs;
258    pub mod path;
259    pub mod net;
260}
261
262cfg_unstable! {
263    pub mod pin;
264    pub mod process;
265
266    mod unit;
267    mod vec;
268    mod result;
269    mod option;
270    mod string;
271    mod collections;
272}
273
274cfg_unstable_default! {
275    #[doc(inline)]
276    pub use std::{write, writeln};
277}