|
1 | 1 | # Futures
|
2 | 2 |
|
3 |
| -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. |
| 3 | +A notable point about Rust is [*fearless concurrency*](https://blog.rust-lang.org/2015/04/10/Fearless-Concurrency.html). |
| 4 | +That is the notion that you should be empowered to do concurrent things, without giving up safety. Also, Rust being a |
| 5 | +low-level language, it's about fearless concurrency *without picking a specific implementation strategy*. This means we |
| 6 | +*must* abstract over the strategy, to allow choice *later*, if we want to have any way to share code between users of |
| 7 | +different strategies. |
4 | 8 |
|
5 |
| -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. |
| 9 | +Futures abstract over *computation*. They describe the "what", independent of the "where" and the "when". For that, |
| 10 | +they aim to break code into small, composable actions that can then be executed by a part of our system. Let's take a |
| 11 | +tour through what it means to compute things to find where we can abstract. |
6 | 12 |
|
7 | 13 | ## Send and Sync
|
8 | 14 |
|
9 |
| -Luckily, concurrent Rust already has two well-known and effective concepts abstracting over sharing between 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. |
| 15 | +Luckily, concurrent Rust already has two well-known and effective concepts abstracting over sharing between concurrent |
| 16 | +parts of a program: `Send` and `Sync`. Notably, both the `Send` and `Sync` traits abstract over *strategies* of |
| 17 | +concurrent work, compose neatly, and don't prescribe an implementation. |
10 | 18 |
|
11 | 19 | As a quick summary:
|
12 | 20 |
|
13 |
| -- `Send` abstracts over *passing data* in a computation 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, and expects you to enforce the "losing access" 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, and the ownership and borrowing rules prevent subsequent access. |
14 |
| - |
15 |
| -- `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 for 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*. |
16 |
| - |
17 |
| -Note how we avoided any word like *"thread"*, but instead opted for "computation". The full power of `Send` and `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. |
| 21 | +- `Send` abstracts over *passing data* in a computation to another concurrent computation (let's call it the receiver), |
| 22 | + losing access to it on the sender side. In many programming languages, this strategy is commonly implemented, but |
| 23 | + missing support from the language side, and expects you to enforce the "losing access" behaviour yourself. |
| 24 | + This is a regular source of bugs: senders keeping handles to sent things around and maybe even working with them |
| 25 | + after sending. Rust mitigates this problem by making this behaviour known. Types can be `Send` or not |
| 26 | + (by implementing the appropriate marker trait), allowing or disallowing sending them around, and the ownership and |
| 27 | + borrowing rules prevent subsequent access. |
| 28 | + |
| 29 | +- `Sync` is about *sharing data* between two concurrent parts of a program. This is another common pattern: as writing |
| 30 | + to a memory location or reading while another party is writing is inherently unsafe, this access needs to be |
| 31 | + moderated through synchronisation.[^1] There are many common ways for two parties to agree on not using the same part |
| 32 | + in memory at the same time, for example mutexes and spinlocks. Again, Rust gives you the option of (safely!) not |
| 33 | + caring. Rust gives you the ability to express that something *needs* synchronisation while not being specific about |
| 34 | + the *how*. |
| 35 | + |
| 36 | +Note how we avoided any word like *"thread"*, but instead opted for "computation". The full power of `Send` and `Sync` |
| 37 | +is that they relieve you of the burden of knowing *what* shares. At the point of implementation, you only need to know |
| 38 | +which method of sharing is appropriate for the type at hand. This keeps reasoning local and is not influenced by |
| 39 | +whatever implementation the user of that type later uses. |
18 | 40 |
|
19 | 41 | `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].
|
20 | 42 |
|
21 | 43 | [rust-book-sync]: https://doc.rust-lang.org/stable/book/ch16-04-extensible-concurrency-sync-and-send.html
|
22 | 44 |
|
23 |
| -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. |
| 45 | +To sum up: Rust gives us the ability to safely abstract over important properties of concurrent programs, their data |
| 46 | +sharing. It does so in a very lightweight fashion; the language itself only knows about the two markers `Send` and |
| 47 | +`Sync` and helps us a little by deriving them itself, when possible. The rest is a library concern. |
24 | 48 |
|
25 | 49 | ## An easy view of computation
|
26 | 50 |
|
27 | 51 | While computation is a subject to write a whole [book](https://computationbook.com/) about, a very simplified view suffices for us: A sequence of composable operations which can branch based on a decision, run to succession and yield a result or yield an error
|
28 | 52 |
|
29 | 53 | ## Deferring computation
|
30 | 54 |
|
31 |
| -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: |
| 55 | +As mentioned above, `Send` and `Sync` are about data. But programs are not only about data, they also talk about *computing* |
| 56 | +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. |
| 57 | +Let's look at what Futures allow us to express, in English. Futures go from this plan: |
32 | 58 |
|
33 | 59 | - Do X
|
34 | 60 | - If X succeeded, do Y
|
@@ -73,7 +99,9 @@ fn read_file(path: &str) -> io::Result<String> {
|
73 | 99 | }
|
74 | 100 | ```
|
75 | 101 |
|
76 |
| -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). |
| 102 | +Speaking in terms of time, we can only take action *before* calling the function or *after* the function returned. |
| 103 | +This is not desirable, as it takes from us the ability to do something *while* it runs. When working with parallel |
| 104 | +code, this would take from us the ability to start a parallel task while the first runs (because we gave away control). |
77 | 105 |
|
78 | 106 | 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.
|
79 | 107 |
|
@@ -124,9 +152,17 @@ This `async` function sets up a deferred computation. When this function is call
|
124 | 152 |
|
125 | 153 | ## What does `.await` do?
|
126 | 154 |
|
127 |
| -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. The `.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? |
128 |
| - |
129 |
| -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 component (usually called the “runtime”) in charge of *executing* this piece of code to take care of all the other things it has to do while the computation finishes. 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). |
| 155 | +The `.await` postfix does exactly what it says on the tin: the moment you use it, the code will wait until the |
| 156 | +requested action (e.g. opening a file or reading all data in it) is finished. The `.await?` is not special, it's just |
| 157 | +the application of the `?` operator to the result of `.await`. So, what is gained over the initial code example? We're |
| 158 | +getting futures and then immediately waiting for them? |
| 159 | + |
| 160 | +The `.await` points act as a marker. Here, the code will wait for a `Future` to produce its value. How will a future |
| 161 | +finish? You don't need to care! The marker allows the component (usually called the “runtime”) in charge of *executing* |
| 162 | +this piece of code to take care of all the other things it has to do while the computation finishes. It will come back |
| 163 | +to this point when the operation you are doing in the background is done. This is why this style of programming is also |
| 164 | +called *evented programming*. We are waiting for *things to happen* (e.g. a file to be opened) and then react |
| 165 | +(by starting to read). |
130 | 166 |
|
131 | 167 | 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.
|
132 | 168 |
|
|
0 commit comments