diff --git a/docs/.gitignore b/docs/.gitignore new file mode 100644 index 00000000..7585238e --- /dev/null +++ b/docs/.gitignore @@ -0,0 +1 @@ +book diff --git a/docs/book.toml b/docs/book.toml new file mode 100644 index 00000000..b080e779 --- /dev/null +++ b/docs/book.toml @@ -0,0 +1,6 @@ +[book] +authors = ["The async-std maintainers"] +language = "en" +multilingual = false +src = "src" +title = "Async programming in Rust with async-std" diff --git a/docs/src/SUMMARY.md b/docs/src/SUMMARY.md new file mode 100644 index 00000000..d800c707 --- /dev/null +++ b/docs/src/SUMMARY.md @@ -0,0 +1,23 @@ +# Summary + +- [Overview](./overview.md) + - [`async-std`](./overview/async-std.md) + - [`std::future` and `futures-rs`](./overview/std-and-library-futures.md) + - [Stability guarantees](./overview/stability-guarantees.md) +- [Async concepts using async-std](./concepts.md) + - [Futures](./concepts/futures.md) + - [Tasks](./concepts/tasks.md) + - [Async read/write](./concepts/async-read-write.md) + - [Streams and Channels](./concepts/streams.md) +- [Tutorials](./tutorials/index.md) + - [Integrating std::thread](./tutorials/integrating-std-thread.md) +- [Async Patterns](./patterns.md) + - [Fork/Join](./patterns/fork-join.md) + - [Accepting requests](./patterns/accepting-concurrent-requests.md) + - [Proper Shutdown](./patterns/proper-shutdown.md) + - [Background Tasks](./patterns/background-tasks.md) + - [Testing](./patterns/testing.md) + - [Collected Small Patterns](./patterns/small-patterns.md) +- [Security practices](./security/index.md) + - [Security disclosures and policy](./security/policy.md) +- [Glossary](./glossary.md) diff --git a/docs/src/concepts.md b/docs/src/concepts.md new file mode 100644 index 00000000..27fef8cc --- /dev/null +++ b/docs/src/concepts.md @@ -0,0 +1,15 @@ +# Async concepts using async-std + +[Rust Futures][futures] have the reputation of being hard. We don't think this is the case. They are, in our opinion, one of the easiest concurrency concepts around and have an intuitive explanation. + +However, there are good reasons for that perception. Futures have three concepts at their base that seem to be a constant source of confusion: deferred computation, asynchronicity and independence of execution strategy. + +These concepts are not hard, but something many people are not used to. This base confusion is amplified by many implementations oriented on details and hard to understand. Most explanations of these implementations also target advanced users. We both try to provide easy to understand primitives and approachable overviews of the concepts. + +Futures are a concept that abstracts over how code is run. By themselves, they do nothing. This is a weird concept in an imperative language, where usually one thing happens after the other - right now. + +So how do Futures run? You decide! Futures do nothing without the piece of code _executing_ them. This part is called an _executor_. An _executor_ decides _when_ and _how_ to execute your futures. The `async-std::task` module provides you with and interface to such an executor. + +Let's start with a little bit of motivation, though. + +[futures]: https://en.wikipedia.org/wiki/Futures_and_promises diff --git a/docs/src/concepts/async-read-write.md b/docs/src/concepts/async-read-write.md new file mode 100644 index 00000000..79354298 --- /dev/null +++ b/docs/src/concepts/async-read-write.md @@ -0,0 +1 @@ +# Async read/write diff --git a/docs/src/concepts/futures.md b/docs/src/concepts/futures.md new file mode 100644 index 00000000..be04cfe9 --- /dev/null +++ b/docs/src/concepts/futures.md @@ -0,0 +1,118 @@ +# Futures + +A notable point about Rust is [*fearless concurrency*](https://blog.rust-lang.org/2015/04/10/Fearless-Concurrency.html). That is the notion that you should be empowered to do concurrent things, without giving up safety. Also, Rust being a low-level language, it's about fearless concurrency *without picking a specific implementation strategy*. This means we *must* abstract over the strategy, to allow choice *later*, if we want to have any way to share code between users of different strategies. + +Futures abstract over *computation*. They describe the "what", independent of the "where" and the "when". For that, they aim to break code into small, composable actions that can then be executed by a part of our system. Let's take a tour through what it means to compute things to find where we can abstract. + +## Send and Sync + +Luckily, concurrent Rust already has two well-known and effective concepts abstracting over sharing between Rust concurrent parts of a program: Send and Sync. Notably, both the Send and Sync traits abstract over *strategies* of concurrent work, compose neatly, and don't prescribe an implementation. + +As a quick summary, `Send` abstracts over passing data in a computation over to another concurrent computation (let's call it the receiver), losing access to it on the sender side. In many programming languages, this strategy is commonly implemented, but missing support from the language side expects you to keep up this behaviour yourself. This is a regular source of bugs: senders keeping handles to sent things around and maybe even working with them after sending. Rust mitigates this problem by making this behaviour known. Types can be `Send` or not (by implementing the appropriate marker trait), allowing or disallowing sending them around. + +Note how we avoided any word like *"thread"*, but instead opted for "computation". The full power of `Send` (and subsequently also `Sync`) is that they relieve you of the burden of knowing *what* shares. At the point of implementation, you only need to know which method of sharing is appropriate for the type at hand. This keeps reasoning local and is not influenced by whatever implementation the user of that type later uses. +`Sync` is about sharing data between two concurrent parts of a program. This is another common pattern: as writing to a memory location or reading while another party is writing is inherently unsafe, this access needs to be moderated through synchronisation.[^1] There are many common ways of two parties to agree on not using the same part in memory at the same time, for example mutexes and spinlocks. Again, Rust gives you the option of (safely!) not caring. Rust gives you the ability to express that something *needs* synchronisation while not being specific about the *how*. + +`Send` and `Sync` can be composed in interesting fashions, but that's beyond the scope here. You can find examples in the [Rust Book][rust-book-sync]. + +To sum up: Rust gives us the ability to safely abstract over important properties of concurrent programs: their data sharing. It does so in a very lightweight fashion: the language itself only knows about the two markers `Send` and `Sync` and helps us a little by deriving them itself, when possible. The rest is a library concern. + +## An easy view of computation + +While computation is a subject to write a whole [book](https://computationbook.com/) about, a very simplified view of them suffices for us: + +- computation is a sequence of composable operations +- they can branch based on a decision +- they either run to succession and yield a result or they can yield an error +## Deferring computation + +As mentioned above `Send` and `Sync` are about data. But programs are not only about data, they also talk about *computing* the data. And that's what \[Futures\][futures] do. We are going to have a close look at how that works in the next chapter. Let's look at what Futures allow us to express, in English. Futures go from this plan: + +- Do X +- If X succeeds, do Y + +towards + +- Start doing X +- Once X succeeds, start doing Y + +Remember the talk about "deferred computation" in the intro? That's all it is. Instead of telling the computer what to execute and decide upon *now*, you tell it what to start doing and how to react on potential events the... well... `Future`. + +## Orienting towards the beginning + +Let's have a look at a simple function, specifically the return value: + + fn read_file(path: &str) -> Result { + let mut file = File.open(path)?; + let mut contents = String::new(); + file.read_to_string(&mut contents)?; + contents + } + +You can call that at any time, so you are in full control on when you call it. But here's the problem: the moment you call it, you transfer control to the called function. It returns a value. +Note that this return value talks about the past. The past has a drawback: all decisions have been made. It has an advantage: the outcome is visible. We can unwrap the presents of program past and then decide what to do with it. + +But here's a problem: we wanted to abstract over *computation* to be allowed to let someone else choose how to run it. That's fundamentally incompatible with looking at the results of previous computation all the time. So, let's find a type that describes a computation without running it. Let's look at the function again: + + fn read_file(path: &str) -> Result { + let mut file = File.open(path)?; + let mut contents = String::new(); + file.read_to_string(&mut contents)?; + contents + } + +Speaking in terms of time, we can only take action *before* calling the function or *after* the function returned. This is not desirable, as it takes from us the ability to do something *while* it runs. When working with parallel code, this would take from us the ability to start a parallel task while the first runs (because we gave away control). + +This is the moment where we could reach for [threads](https://en.wikipedia.org/wiki/Thread_). But threads are a very specific concurrency primitive and we said that we are searching for an abstraction. +What we are searching is something that represents ongoing work towards a result in the future. Whenever we say `something` in Rust, we almost always mean a trait. Let's start with an incomplete definition of the `Future` trait: + + trait Future { + type Output; + + fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll; + } + +Ignore `Pin` and `Context` for now, you don't need them for high-level understanding. Looking at it closely, we see the following: it is generic over the `Output`. It provides a function called `poll`, which allows us to check on the state of the current computation. +Every call to `poll()` can result in one of these two cases: + +1. The future is done, `poll` will return `[Poll::Ready](https://doc.rust-lang.org/std/task/enum.Poll.html#variant.Ready)` +2. The future has not finished executing, it will return `[Poll::Pending](https://doc.rust-lang.org/std/task/enum.Poll.html#variant.Pending)` + +This allows us to externally check if a `Future` has finished doing its work, or is finally done and can give us the value. The most simple way (but not efficient) would be to just constantly poll futures in a loop. There's optimistions here, and this is what a good runtime is does for you. +Note that calling `poll` after case 1 happened may result in confusing behaviour. See the [futures-docs](https://doc.rust-lang.org/std/future/trait.Future.html) for details. + +## Async + +While the `Future` trait has existed in Rust for a while, it was inconvenient to build and describe them. For this, Rust now has a special syntax: `async`. The example from above, implemented in `async-std`, would look like this: + + + use async_std::fs::File; + + async fn read_file(path: &str) -> Result { + let mut file = File.open(path).await?; + let mut contents = String::new(); + file.read_to_string(&mut contents).await?; + contents + } + +Amazingly little difference, right? All we did is label the function `async` and insert 2 special commands: `.await`. + +This function sets up a deferred computation. When this function is called, it will produce a `Future` instead of immediately returning a String. (Or, more precisely, generate a type for you that implements `Future`.) + +## What does `.await` do? + +The `.await` postfix does exactly what it says on the tin: the moment you use it, the code will wait until the requested action (e.g. opening a file or reading all data in it) is finished. `.await?` is not special, it’s just the application of the `?` operator to the result of `.await`. So, what is gained over the initial code example? We’re getting futures and then immediately waiting for them? + +The `.await` points act as a marker. Here, the code will wait for a `Future` to produce its value. How will a future finish? You don’t need to care! The marker allows the code later *executing* this piece of code (usually called the “runtime”) when it can take some time to care about all the other things it has to do. It will come back to this point when the operation you are doing in the background is done. This is why this style of programming is also called *evented programming*. We are waiting for *things to happen* (e.g. a file to be opened) and then react (by starting to read). + +When executing 2 or more of these functions at the same time, our runtime system is then able to fill the wait time with handling *all the other events* currently going on. + +## Conclusion + +Working from values, we searched for something that expresses *working towards a value available sometime later*. From there, we talked about the concept of polling. + +A `Future` is any data type that does not represent a value, but the ability to *produce a value at some point in the future*. Implementations of this are very varied and detailled depending on use-case, but the interface is simple. + +Next, we will introduce you to `tasks`, which we need to actually *run* Futures. + +[^1]: Two parties reading while it is guaranteed that no one is writing is always safe. diff --git a/docs/src/concepts/streams.md b/docs/src/concepts/streams.md new file mode 100644 index 00000000..80eeccb1 --- /dev/null +++ b/docs/src/concepts/streams.md @@ -0,0 +1 @@ +# Streams diff --git a/docs/src/concepts/tasks.md b/docs/src/concepts/tasks.md new file mode 100644 index 00000000..a56cff50 --- /dev/null +++ b/docs/src/concepts/tasks.md @@ -0,0 +1,83 @@ +# Tasks +Now that we know what Futures are, we now want to run them! + +In `async-std`, the `tasks` (TODO: link) module is responsible for this. The simplest way is using the `block_on` function: + +```rust +use async_std::fs::File; +use async_std::task; + +async fn read_file(path: &str) -> Result { + let mut file = File.open(path).await?; + let mut contents = String::new(); + file.read_to_string(&mut contents).await?; + contents +} + +fn main() { + let task = task::spawn(async { + let result = read_file("data.csv"); + match result { + Ok(s) => println!("{}", s), + Err(e) => println!("Error reading file: {:?}", e) + } + }); + println!("Started task!"); + task::block_on(task); + println!("Stopped task!"); +} +``` + +This asks the runtime baked into `async_std` to execute the code that reads a file. Let’s go one by one, though, inside to outside. + +```rust +async { + let result = read_file("data.csv"); + match result { + Ok(s) => println!("{}", s), + Err(e) => println!("Error reading file: {:?}", e) + } +} +``` + +This is an `async` *block*. Async blocks are necessary to call `async` functions, and will instruct the compiler to include all the relevant instructions to do so. In Rust, all blocks return a value and `async` blocks happen to return a value of the kind `Future`. + +But let’s get to the interesting part: + +```rust + +task::spawn(async { }) + +``` + +`spawn` takes a Future and starts running it on a `Task`. It returns a `JoinHandle`. Futures in Rust are sometimes called *cold* Futures. You need something that starts running them. To run a Future, there may be some additional bookkeeping required, e.g. if it’s running or finished, where it is being placed in memory and what the current state is. This bookkeeping part is abstracted away in a `Task`. A `Task` is similar to a `Thread`, with some minor differences: it will be scheduled by the program instead of the operating system kernel and if it encounters a point where it needs to wait, the program itself responsible for waking it up again. We’ll talk a little bit about that later. An `async_std` task can also has a name and an ID, just like a thread. + +For now, it is enough to know that once you `spawn`ed a task, it will continue running in the background. The `JoinHandle` in itself is a future that will finish once the `Task` ran to conclusion. Much like with `threads` and the `join` function, we can now call `block_on` on the handle to *block* the program (or the calling thread, to be specific) to wait for it to finish. + + +## Tasks in `async_std` + +Tasks in `async_std` are one of the core abstractions. Much like Rust’s `thread`s, they provide some practical functionality over the raw concept. `Tasks` have a relationship to the runtime, but they are in themselves separate. `async_std` tasks have a number of desirable properties: + + +- They are single-allocated +- All tasks have a *backchannel*, which allows them to propagate results and errors to the spawning task through the `JoinHandle` +- The carry desirable metadata for debugging +- They support task local storage + +`async_std` s task api handles setup and teardown of a backing runtime for you and doesn’t rely on a runtime being started. + +## Blocking + +TODO: fill me in + +## Errors and panics + +TODO: fill me in + + +## Conclusion + +`async_std` comes with a useful `Task` type that works with an API similar to `std::thread`. It covers error and panic behaviour in a structured and defined way. + +Tasks are separate concurrent units and sometimes they need to communicate. That’s where `Stream`s come in. diff --git a/docs/src/glossary.md b/docs/src/glossary.md new file mode 100644 index 00000000..caff7e7e --- /dev/null +++ b/docs/src/glossary.md @@ -0,0 +1,7 @@ +# Glossary + +### blocking + +"blocked" generally refers to conditions that keep a task from doing its work. For example, it might need data to be sent by a client before continuing. When tasks becomes blocked, usually, other tasks are scheduled. + +Sometimes you hear that you should never call "blocking functions" in an async context. What this refers to is functions that block the current thread and do not yield control back. This keeps the executor from using this thread to schedule another task. \ No newline at end of file diff --git a/docs/src/images/horizontal_color.svg b/docs/src/images/horizontal_color.svg new file mode 100644 index 00000000..88bed32c --- /dev/null +++ b/docs/src/images/horizontal_color.svg @@ -0,0 +1,78 @@ + + + + + + image/svg+xml + + + + + + + + + + + + + + diff --git a/docs/src/images/icon_color.svg b/docs/src/images/icon_color.svg new file mode 100644 index 00000000..90dccdee --- /dev/null +++ b/docs/src/images/icon_color.svg @@ -0,0 +1,80 @@ + + + + + + image/svg+xml + + + + + + + + + + + + + + diff --git a/docs/src/images/vertical_color.svg b/docs/src/images/vertical_color.svg new file mode 100644 index 00000000..14bd065c --- /dev/null +++ b/docs/src/images/vertical_color.svg @@ -0,0 +1,85 @@ + + + + + + image/svg+xml + + + + + + + + + + + + + + + diff --git a/docs/src/overview.md b/docs/src/overview.md new file mode 100644 index 00000000..b40799d4 --- /dev/null +++ b/docs/src/overview.md @@ -0,0 +1,10 @@ +# Overview + +![async-std logo](./images/horizontal_color.svg) + +`async-std` along with its [supporting libraries][organization] is a library making your life in async programming easier. It provides provide fundamental implementations for downstream libraries and applications alike. The name reflects the approach of this library: it is a closely modeled to the Rust main standard library as possible, replacing all components by async counterparts. + +`async-std` provides an interface to all important primitives: filesystem operations, network operations and concurrency basics like timers. It also exposes an `task` in a model similar to the `thread` module found in the Rust standard lib. But it does not only include io primitives, but also `async/await` compatible versions of primitives like `Mutex`. You can read more about `async-std` in [the overview chapter][overview-std]. + +[organization]: https://github.com/async-std/async-std +[overview-std]: overview/async-std/ diff --git a/docs/src/overview/async-std.md b/docs/src/overview/async-std.md new file mode 100644 index 00000000..5b8c6ccf --- /dev/null +++ b/docs/src/overview/async-std.md @@ -0,0 +1 @@ +# async-std diff --git a/docs/src/overview/stability-guarantees.md b/docs/src/overview/stability-guarantees.md new file mode 100644 index 00000000..e5aa9067 --- /dev/null +++ b/docs/src/overview/stability-guarantees.md @@ -0,0 +1,40 @@ +# Stability and SemVer + +`async-std` follows https://semver.org/. + +In short: we are versioning our software as `MAJOR.MINOR.PATCH`. We increase the: + +* MAJOR version when there are incompatible API changes, +* MINOR version when we introducece functionality in a backwards-compatible manner +* PATCH version when we make backwards-compatible bug fixes + +We will provide migration documentation between major versions. + +## Future expectations + +`async-std` uses its own implementations of the following concepts: + +* `Read` +* `Write` +* `Seek` +* `BufRead` +* `Stream` + +For integration with the ecosystem, all types implementing these traits also have an implementation of the corresponding interfaces in the `futures-rs` library. +Please note that our SemVer guarantees don't extend to usage of those interfaces. We expect those to be conservatively updated and in lockstep. + +## Minimum version policy + +The current tentative policy is that the minimum Rust version required to use this crate can be increased in minor version updates. For example, if `async-std` 1.0 requires Rust 1.37.0, then `async-std` 1.0.z for all values of z will also require Rust 1.37.0 or newer. However, `async-std` 1.y for y > 0 may require a newer minimum version of Rust. + +In general, this crate will be conservative with respect to the minimum supported version of Rust. With `async/await` being a new feature though, we will track changes in a measured pace initially. + +## Security fixes + +Security fixes will be applied to _all_ minor branches of this library in all _supported_ major revisions. This policy might change in the future, in which case we give at least _3 month_ of ahead notice. + +## Credits + +This policy is based on [burntsushis regex crate][regex-policy]. + +[regex-policy]: https://github.com/rust-lang/regex#minimum-rust-version-policy diff --git a/docs/src/overview/std-and-library-futures.md b/docs/src/overview/std-and-library-futures.md new file mode 100644 index 00000000..98cdfb44 --- /dev/null +++ b/docs/src/overview/std-and-library-futures.md @@ -0,0 +1,27 @@ +# `std::future` and `futures-rs` + +Rust has two kinds of types commonly referred to as `Future`: + + +- the first is `std::future::Future` from Rust’s [standard library](https://doc.rust-lang.org/std/future/trait.Future.html). +- the second is `futures::future::Future` from the [futures-rs crate](https://docs.rs/futures-preview/0.3.0-alpha.17/futures/prelude/trait.Future.html), currently released as `futures-preview`. + +The future defined in the [futures-rs](https://docs.rs/futures-preview/0.3.0-alpha.17/futures/prelude/trait.Future.html) crate was the original implementation of the type. To enable the `async/await` syntax, the core Future trait was moved into Rust’s standard library and became `std::future::Future`. In some sense, the `std``::future::Future` can be seen as a minimal subset of `futures::future::Future`. + +It is critical to understand the difference between `std::future::Future` and `futures::future::Future`, and the approach that `async-std` takes towards them. In itself, `std::future::Future` is not something you want to interact with as a user—except by calling `.await` on it. The inner workings of `std::future::Future` are mostly of interest to people implementing `Future`. Make no mistake—this is very useful! Most of the functionality that used to be defined on `Future` itself has been moved to an extension trait called `[FuturesExt](https://docs.rs/futures-preview/0.3.0-alpha.17/futures/future/trait.FutureExt.html)`. From this information, you might be able to infer that the `futures` library serves as an extension to the core Rust async features. + +In the same tradition as `futures`, `async-std` re-exports the core `std::future::``Future` type. You can get actively opt into the extensions provided by the `futures-preview` crate by adding it your `Cargo.toml` and importing `FuturesExt`. + +## Interfaces and Stability + + `async-std` aims to be a stable and reliable library, at the level of the Rust standard library. This also means that we don't rely on the `futures` library for our interface. Yet, we appreciate that many users have come to like the conveniences that `futures-rs` brings. For that reason, `async-std` implements all `futures` traits for its types. + + Luckily, the approach from above gives you full flexibility. If you care about stability a lot, you can just use `async-std` as is. If you prefer the `futures` library interfaces, you link those in.. Both uses are first class. + +## `async_std::future` + +There’s some support functions that we see as important for working with futures of any kind. These can be found in the `async_std::future` module and are covered by our stability guarantees. + +## Streams and Read/Write/Seek/BufRead traits + +Due to limitations of the Rust compiler, those are currently implemented in `async_std`, but cannot be implemented by users themselves. \ No newline at end of file diff --git a/docs/src/patterns.md b/docs/src/patterns.md new file mode 100644 index 00000000..a19b81b4 --- /dev/null +++ b/docs/src/patterns.md @@ -0,0 +1,5 @@ +# Patterns + +This section documents small, useful patterns. + +It is intended to be read at a glance, allowing you to get back when you have a problem. \ No newline at end of file diff --git a/docs/src/patterns/accepting-concurrent-requests.md b/docs/src/patterns/accepting-concurrent-requests.md new file mode 100644 index 00000000..b984183c --- /dev/null +++ b/docs/src/patterns/accepting-concurrent-requests.md @@ -0,0 +1 @@ +# Accepting requests diff --git a/docs/src/patterns/async-read-write.md b/docs/src/patterns/async-read-write.md new file mode 100644 index 00000000..79354298 --- /dev/null +++ b/docs/src/patterns/async-read-write.md @@ -0,0 +1 @@ +# Async read/write diff --git a/docs/src/patterns/background-tasks.md b/docs/src/patterns/background-tasks.md new file mode 100644 index 00000000..02571b26 --- /dev/null +++ b/docs/src/patterns/background-tasks.md @@ -0,0 +1 @@ +# Background Tasks diff --git a/docs/src/patterns/fork-join.md b/docs/src/patterns/fork-join.md new file mode 100644 index 00000000..4709bb20 --- /dev/null +++ b/docs/src/patterns/fork-join.md @@ -0,0 +1 @@ +# Fork/Join diff --git a/docs/src/patterns/proper-shutdown.md b/docs/src/patterns/proper-shutdown.md new file mode 100644 index 00000000..49ba1f43 --- /dev/null +++ b/docs/src/patterns/proper-shutdown.md @@ -0,0 +1 @@ +# Proper Shutdown diff --git a/docs/src/patterns/small-patterns.md b/docs/src/patterns/small-patterns.md new file mode 100644 index 00000000..2250d196 --- /dev/null +++ b/docs/src/patterns/small-patterns.md @@ -0,0 +1,16 @@ +# Small Patterns + +A collection of small, useful patterns. + +## Splitting streams + +`async-std` doesn't provide a `split()` method on `io` handles. Instead, splitting a stream into a read and write half can be done like this: + +```rust +use async_std::io; + +async fn echo(stream: io::TcpStream) { + let (reader, writer) = &mut (&stream, &stream); + io::copy(reader, writer).await?; +} +``` \ No newline at end of file diff --git a/docs/src/patterns/testing.md b/docs/src/patterns/testing.md new file mode 100644 index 00000000..f00b526a --- /dev/null +++ b/docs/src/patterns/testing.md @@ -0,0 +1 @@ +# Testing diff --git a/docs/src/security/index.md b/docs/src/security/index.md new file mode 100644 index 00000000..ab625a13 --- /dev/null +++ b/docs/src/security/index.md @@ -0,0 +1,12 @@ +# Security + +Writing a highly perfomant async core library is a task involving some instances of unsafe code. + +We take great care in vetting all unsafe code included in `async-std` and do follow generally accepted practices. + +In the case that you find a security-related bug in our library, please get in touch with our [security contact][security-policy]. + +Patches improving the resilience of the library or the testing setup are happily accepted on our [github org][github]. + +[security-policies]: /security/policy +[github]: https://github.com/async-std/ \ No newline at end of file diff --git a/docs/src/security/policy.md b/docs/src/security/policy.md new file mode 100644 index 00000000..d6285d0e --- /dev/null +++ b/docs/src/security/policy.md @@ -0,0 +1,37 @@ +# Policy + +Safety is one of the core principles of what we do, and to that end, we would like to ensure that async-std has a secure implementation. Thank you for taking the time to responsibly disclose any issues you find. + +All security bugs in async-std distribution should be reported by email to security@ferrous-systems.com. This list is delivered to a small security team. Your email will be acknowledged within 24 hours, and you’ll receive a more detailed response to your email within 48 hours indicating the next steps in handling your report. If you would like, you can encrypt your report using our public key. This key is also On MIT’s keyserver and reproduced below. + +Be sure to use a descriptive subject line to avoid having your report be missed. After the initial reply to your report, the security team will endeavor to keep you informed of the progress being made towards a fix and full announcement. As recommended by [RFPolicy][rf-policy], these updates will be sent at least every five days. In reality, this is more likely to be every 24-48 hours. + +If you have not received a reply to your email within 48 hours, or have not heard from the security team for the past five days, there are a few steps you can take (in order): + +* Contact the current security coordinator TODO directly. +* Contact the back-up contact TODO directly. +* Post on our Community forums + +Please note that the discussion forums are public areas. When escalating in these venues, please do not discuss your issue. Simply say that you’re trying to get a hold of someone from the security team. + +[rf-policy]: https://en.wikipedia.org/wiki/RFPolicy + +## Disclosure policy + +The async-std project has a 5 step disclosure process. + +* The security report is received and is assigned a primary handler. This person will coordinate the fix and release process. +* The problem is confirmed and a list of all affected versions is determined. +* Code is audited to find any potential similar problems. +* Fixes are prepared for all releases which are still under maintenance. These fixes are not committed to the public repository but rather held locally pending the announcement. +* On the embargo date, the changes are pushed to the public repository and new builds are deployed to crates.io. Within 6 hours, a copy of the advisory will be published on the the async.rs blog. + +This process can take some time, especially when coordination is required with maintainers of other projects. Every effort will be made to handle the bug in as timely a manner as possible, however it’s important that we follow the release process above to ensure that the disclosure is handled in a consistent manner. + +## Credits + +This policy is adapted from the [Rust project](https://www.rust-lang.org/policies/security) security policy. + +## PGP Key + +TODO \ No newline at end of file diff --git a/docs/src/tutorials/index.md b/docs/src/tutorials/index.md new file mode 100644 index 00000000..81c8590f --- /dev/null +++ b/docs/src/tutorials/index.md @@ -0,0 +1 @@ +# Tutorials diff --git a/docs/src/tutorials/integrating-std-thread.md b/docs/src/tutorials/integrating-std-thread.md new file mode 100644 index 00000000..c5aa5e1a --- /dev/null +++ b/docs/src/tutorials/integrating-std-thread.md @@ -0,0 +1,43 @@ +# Exercise: Waiting for `std::thread` + +Parallel processing is usually done via [threads]. +In `async-std`, we have similar concept, called a [`task`]. +These two worlds seem different - and in some regards, they are - though they +are easy to connect. +In this exercise, you will learn how to connect to concurrent/parallel components easily, by connecting a thread to a task. + +## Understanding the problem + +The standard thread API in Rust is `std::thread`. Specifically, it contains the [`spawn`] function, which allows us to start a thread: + +```rust +std::thread::spawn(|| { + println!("in child thread"); +}) +println!("in parent thread"); +``` + +This creates a thread, _immediately_ [schedules] it to run, and continues. This is crucial: once the thread is spawned, it is independent of its _parent thread_. If you want to wait for the thread to end, you need to capture its [`JoinHandle`] and join it with your current thread: + +```rust +let thread = std::thread::spawn(|| { + println!("in child thread"); +}) +thread.join(); +println!("in parent thread"); +``` + +This comes at a cost though: the waiting thread will [block] until the child is done. Wouldn't it be nice if we could just use the `.await` syntax here and leave the opportunity for another task to be scheduled while waiting? + +## Backchannels + + + + + +[threads]: TODO: wikipedia +[`task`]: TODO: docs link +[`spawn`]: TODO: docs link +[`JoinHandle`]: TODO: docs link +[schedules]: TODO: Glossary link +[block]: TODO: Link to blocking \ No newline at end of file