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