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/HEAD/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. [`channel`] contains the channel types for message passing.
110//!
111//! [files]: fs/struct.File.html
112//! [TCP]: net/struct.TcpStream.html
113//! [UDP]: net/struct.UdpSocket.html
114//! [`io`]: io/index.html
115//! [`sync`]: sync/index.html
116//! [`channel`]: channel/index.html
117//!
118//! ## Timeouts, intervals, and delays
119//!
120//! `async-std` provides several methods to manipulate time:
121//!
122//! * [`task::sleep`] to wait for a duration to pass without blocking.
123//! * [`stream::interval`] for emitting an event at a set interval.
124//! * [`future::timeout`] to time-out futures if they don't resolve within a
125//! set interval.
126//!
127//! [`task::sleep`]: task/fn.sleep.html
128//! [`stream::interval`]: stream/fn.interval.html
129//! [`future::timeout`]: future/fn.timeout.html
130//!
131//! # Examples
132//!
133//! All examples require the [`"attributes"` feature](#features) to be enabled.
134//! This feature is not enabled by default because it significantly impacts
135//! compile times. See [`task::block_on`] for an alternative way to start
136//! executing tasks.
137//!
138//! Call an async function from the main function:
139//!
140#![cfg_attr(feature = "attributes", doc = "```")]
141#![cfg_attr(not(feature = "attributes"), doc = "```ignore")]
142//! async fn say_hello() {
143//! println!("Hello, world!");
144//! }
145//!
146//! #[async_std::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#![cfg_attr(feature = "attributes", doc = "```")]
155#![cfg_attr(not(feature = "attributes"), doc = "```ignore")]
156//! use async_std::prelude::*;
157//!
158//! #[async_std::main]
159//! async fn main() {
160//! let a = async { 1u8 };
161//! let b = async { 2u8 };
162//! assert_eq!(a.join(b).await, (1u8, 2u8))
163//! }
164//! ```
165//!
166//! Create a UDP server that echoes back each received message to the sender:
167//!
168#![cfg_attr(feature = "attributes", doc = "```no_run")]
169#![cfg_attr(not(feature = "attributes"), doc = "```ignore")]
170//! use async_std::net::UdpSocket;
171//!
172//! #[async_std::main]
173//! async fn main() -> std::io::Result<()> {
174//! let socket = UdpSocket::bind("127.0.0.1:8080").await?;
175//! println!("Listening on {}", socket.local_addr()?);
176//!
177//! let mut buf = vec![0u8; 1024];
178//!
179//! loop {
180//! let (recv, peer) = socket.recv_from(&mut buf).await?;
181//! let sent = socket.send_to(&buf[..recv], &peer).await?;
182//! println!("Sent {} out of {} bytes to {}", sent, recv, peer);
183//! }
184//! }
185//! ```
186//! [`task::block_on`]: task/fn.block_on.html
187//!
188//! # Features
189//!
190//! Items marked with
191//! <span
192//! class="module-item stab portability"
193//! style="display: inline; border-radius: 3px; padding: 2px; font-size: 80%; line-height: 1.2;"
194//! ><code>unstable</code></span>
195//! are available only when the `unstable` Cargo feature is enabled:
196//!
197//! ```toml
198//! [dependencies.async-std]
199//! version = "1.7.0"
200//! features = ["unstable"]
201//! ```
202//!
203//! Items marked with
204//! <span
205//! class="module-item stab portability"
206//! style="display: inline; border-radius: 3px; padding: 2px; font-size: 80%; line-height: 1.2;"
207//! ><code>attributes</code></span>
208//! are available only when the `attributes` Cargo feature is enabled:
209//!
210//! ```toml
211//! [dependencies.async-std]
212//! version = "1.7.0"
213//! features = ["attributes"]
214//! ```
215//!
216//! Compatibility with the `tokio` 1.0 runtime is also simultaneously possible
217//! using the `tokio1` Cargo feature:
218//!
219//! ```toml
220//! [dependencies.async-std]
221//! version = "1.7.0"
222//! features = ["tokio1"]
223//! ```
224//!
225//! Compatibility with the `tokio` 0.2 runtime is possible using the `tokio02`
226//! Cargo feature:
227//!
228//! ```toml
229//! [dependencies.async-std]
230//! version = "1.7.0"
231//! features = ["tokio02"]
232//! ```
233//!
234//! Compatibility with the `tokio` 0.3 runtime is also simultaneously possible
235//! using the `tokio03` Cargo feature:
236//!
237//! ```toml
238//! [dependencies.async-std]
239//! version = "1.7.0"
240//! features = ["tokio03"]
241//! ```
242//!
243//! Additionally it's possible to only use the core traits and combinators by
244//! only enabling the `std` Cargo feature:
245//!
246//! ```toml
247//! [dependencies.async-std]
248//! version = "1.7.0"
249//! default-features = false
250//! features = ["std"]
251//! ```
252//!
253//! And to use async-std on `no_std` targets that only support `alloc` only
254//! enable the `alloc` Cargo feature:
255//!
256//! ```toml
257//! [dependencies.async-std]
258//! version = "1.7.0"
259//! default-features = false
260//! features = ["alloc"]
261//! ```
262//!
263//! # Runtime configuration
264//!
265//! Several environment variables are available to tune the async-std
266//! runtime:
267//!
268//! * `ASYNC_STD_THREAD_COUNT`: The number of threads that the
269//! async-std runtime will start. By default, this is one per logical
270//! cpu as determined by [async-global-executor](async_global_executor),
271//! which may be different than the number of physical cpus. Async-std
272//! _will panic_ if this is set to any value other than a positive
273//! integer.
274//! * `ASYNC_STD_THREAD_NAME`: The name that async-std's runtime
275//! threads report to the operating system. The default value is
276//! `"async-std/runtime"`.
277//!
278
279#![cfg_attr(not(feature = "std"), no_std)]
280#![cfg_attr(feature = "docs", feature(doc_cfg))]
281#![warn(missing_docs, missing_debug_implementations, rust_2018_idioms)]
282#![allow(clippy::mutex_atomic, clippy::module_inception)]
283#![doc(test(attr(deny(rust_2018_idioms, warnings))))]
284#![doc(test(attr(allow(unused_extern_crates, unused_variables))))]
285#![doc(html_logo_url = "https://async.rs/images/logo--hero.svg")]
286
287#[macro_use]
288mod utils;
289
290#[cfg(feature = "attributes")]
291#[cfg_attr(feature = "docs", doc(cfg(attributes)))]
292#[doc(inline)]
293pub use async_attributes::{main, test};
294
295#[cfg(feature = "std")]
296mod macros;
297
298cfg_alloc! {
299 pub mod task;
300 pub mod future;
301 pub mod stream;
302}
303
304cfg_std! {
305 pub mod io;
306 pub mod os;
307 pub mod prelude;
308 pub mod sync;
309 pub mod channel;
310}
311
312cfg_default! {
313 #[cfg(not(target_os = "unknown"))]
314 pub mod fs;
315 pub mod path;
316 pub mod net;
317 #[cfg(not(target_os = "unknown"))]
318 pub(crate) mod rt;
319}
320
321cfg_unstable! {
322 pub mod pin;
323 #[cfg(all(not(target_os = "unknown"), feature = "std"))]
324 pub mod process;
325
326 mod unit;
327 mod vec;
328 mod result;
329 mod option;
330 mod string;
331 mod collections;
332}
333
334cfg_unstable_default! {
335 #[doc(inline)]
336 pub use std::{write, writeln};
337}