when two tasks is given the same id. The explicit close call is not needed. We can do that in the `Drop` implementation instead. It's better to have accounting tasks like this only one place and it didn't add anything to the example. There is a subtle bug which occurs if two tasks are given the same Id. I mentioned this explicitly since it's such an easy thing to do. I added the fix as a reader excercise since for the examples sake I think it's better to pass them in explicitly so that we don't "pollute" the example with more code than strictly needed to get an understanding of futures.
62 lines
2.8 KiB
Markdown
62 lines
2.8 KiB
Markdown

|
|
|
|
# Futures Explained in 200 Lines of Rust
|
|
|
|
The rendered version is found at: [https://cfsamson.github.io/books-futures-explained/](https://cfsamson.github.io/books-futures-explained/)
|
|
|
|
You can find the main example in the repository [examples-futures](https://github.com/cfsamson/examples-futures).
|
|
|
|
This book aims to explain `Futures` in Rust using an example driven approach,
|
|
exploring why they're designed the way they are, and how they work. We'll also
|
|
take a look at some of the alternatives we have when dealing with concurrency
|
|
in programming.
|
|
|
|
Going into the level of detail I do in this book is not needed to use futures
|
|
or async/await in Rust. It's for the curious out there that want to know _how_
|
|
it all works.
|
|
|
|
## What this book covers
|
|
|
|
This book will try to explain everything you might wonder about up until the
|
|
topic of different types of executors and runtimes. We'll just implement a very
|
|
simple runtime in this book introducing some concepts but it's enough to get
|
|
started.
|
|
|
|
[Stjepan Glavina](https://github.com/stjepang) has made an excellent series of
|
|
articles about async runtimes and executors, and if the rumors are right there
|
|
is more to come from him in the near future.
|
|
|
|
The way you should go about it is to read this book first, then continue
|
|
reading the [articles from stejpang](https://stjepang.github.io/) to learn more
|
|
about runtimes and how they work, especially:
|
|
|
|
1. [Build your own block_on()](https://stjepang.github.io/2020/01/25/build-your-own-block-on.html)
|
|
2. [Build your own executor](https://stjepang.github.io/2020/01/31/build-your-own-executor.html)
|
|
|
|
## Contributing
|
|
|
|
All kinds of contributions are welcome. Spelling, wording or clarifications are
|
|
very welcome as well as adding or suggesting changes to the content. I'd appreciate
|
|
if you contribute through a PR.
|
|
|
|
Feedback, questions or discussion is welcome in the issue tracker.
|
|
|
|
## Changelog
|
|
|
|
**2020-04-06:** Final draft finished
|
|
|
|
**2020-04-10:** Rather substantial rewrite of the `Reactor` to better the
|
|
readability and make it easier to reason about. In addition I fixed a mistake
|
|
in the `Poll` method and a possible race condition. See #2 for more details.
|
|
|
|
**2020-04-13:** Added a "bonus section" to the [Implementing Futures chapter](https://cfsamson.github.io/books-futures-explained/6_future_example.html) where we avoid using `thread::park` and instead show how we
|
|
can use a `Condvar` and a `Mutex` to create a proper `Parker`. Updated the [Finished Example](https://cfsamson.github.io/books-futures-explained/8_finished_example.html) to reflect these changes. Unfortunately, this led us
|
|
a few lines over my initial promis of keeping the example below 200 LOC but the I think the inclusion
|
|
is worth it.
|
|
|
|
## License
|
|
|
|
This book is MIT licensed.
|
|
|
|
[rendered]: https://cfsamson.github.io/books-futures-explained/
|