forked from mirror/async-std
merge
This commit is contained in:
commit
54252b39e9
4 changed files with 16 additions and 10 deletions
|
@ -16,8 +16,12 @@ Note how we avoided any word like *"thread"*, but instead opted for "computation
|
|||
|
||||
`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].
|
||||
|
||||
[rust-book-sync]: https://doc.rust-lang.org/stable/book/ch16-04-extensible-concurrency-sync-and-send.html
|
||||
|
||||
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.
|
||||
|
||||
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 suffices for us:
|
||||
|
@ -28,7 +32,7 @@ While computation is a subject to write a whole [book](https://computationbook.c
|
|||
|
||||
## 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:
|
||||
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
|
||||
|
@ -40,6 +44,8 @@ towards
|
|||
|
||||
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`.
|
||||
|
||||
[futures]: https://doc.rust-lang.org/std/future/trait.Future.html
|
||||
|
||||
## Orienting towards the beginning
|
||||
|
||||
Let's have a look at a simple function, specifically the return value:
|
||||
|
@ -77,8 +83,8 @@ What we are searching is something that represents ongoing work towards a result
|
|||
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)`
|
||||
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 optimisations 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.
|
||||
|
|
|
@ -16,7 +16,7 @@ async fn read_file(path: &str) -> Result<String, io::Error> {
|
|||
}
|
||||
|
||||
fn main() {
|
||||
let task = task::spawn(async {
|
||||
let reader_task = task::spawn(async {
|
||||
let result = read_file("data.csv");
|
||||
match result {
|
||||
Ok(s) => println!("{}", s),
|
||||
|
@ -24,7 +24,7 @@ fn main() {
|
|||
}
|
||||
});
|
||||
println!("Started task!");
|
||||
task::block_on(task);
|
||||
task::block_on(reader_task);
|
||||
println!("Stopped task!");
|
||||
}
|
||||
```
|
||||
|
|
|
@ -6,11 +6,11 @@ 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`.
|
||||
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.
|
||||
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`.
|
||||
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
|
||||
|
||||
|
@ -24,4 +24,4 @@ There’s some support functions that we see as important for working with futur
|
|||
|
||||
## 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.
|
||||
Due to limitations of the Rust compiler, those are currently implemented in `async_std`, but cannot be implemented by users themselves.
|
||||
|
|
|
@ -89,4 +89,4 @@ where
|
|||
}
|
||||
})
|
||||
}
|
||||
```s
|
||||
```
|
||||
|
|
Loading…
Reference in a new issue