Async Rust

Book description

Already popular among programmers for its memory safety and speed, the Rust programming language is also valuable for asynchrony. This practical book shows you how asynchronous Rust can help you solve problems that require multitasking. You'll learn how to apply async programming to solve problems with an async approach. You will also dive deeper into async runtimes, implementing your own ways in which async runtimes handle incoming tasks.

Authors Maxwell Flitton and Caroline Morton also show you how to implement the Tokio software library to help you with incoming traffic, communicate between threads with shared memory and channels, and design a range of complex solutions using actors. You'll also learn ways to use Rust in embedded systems, and perform unit and end-to-end tests on a Rust async system.

With this book, you'll learn:

  • How Rust approaches async programming
  • How coroutines relate to async Rust
  • Reactive programming and how to implement pub sub in async rust
  • How to solve problems using actors
  • How to customize Tokio to gain control over how tasks are processed
  • Async Rust design patterns
  • How to build an async TCP server just using the standard library
  • How to unit test async Rust

By the end of the book, you'll be able to implement your own async TCP server completely from the standard library with zero external dependencies, and unit test your async code.

Publisher resources

View/Submit Errata

Table of contents

  1. 1. Introduction to Async
    1. What is Async?
    2. Introduction to Processes
      1. Interacting With Processes
      2. Communicating With Processes
    3. What Are Threads?
    4. Where Can We Utilise Async?
      1. Using Async For File I/O
      2. Improving HTTP Request Performance With Async
    5. Summary
  2. 2. Basic Async Rust
    1. Understanding Tasks
    2. Futures
      1. Pinning in Futures
      2. Context in Futures
    3. Sharing data between Futures
      1. High-level data sharing between futures
      2. How are Futures processed?
    4. Putting it all together
      1. Summary
  3. 3. Building Our Own Async Runtime
    1. Building Our Own Async Queue
    2. Increasing Workers And Queues
    3. Passing Tasks To Different Queues
    4. Task Stealing
    5. Refactoring Our Spawn Task Function
    6. Creating Our Own Join Macro
    7. Configuring Our Runtime
    8. Running Background Processes
    9. Summary
  4. 4. Integrating Networking Into Our Own Async Runtime
    1. Building An Executor For Hyper
    2. Building An HTTP Connection
    3. Implementing The Tokio AsyncRead Trait
    4. Implementing The Tokio AsyncWrite Trait
    5. Connecting And Running Our Client
    6. Introduction To Mio
      1. Polling Sockets in Futures
      2. Sending Data Over Socket
    7. Summary
  5. 5. Coroutines
    1. Introduction to Coroutines
      1. What are Coroutines?
      2. Why use Coroutines?
    2. Generating with Coroutines
      1. Implementing a Simple Generator in Rust
      2. Stacking our Coroutines
      3. Calling a Coroutine from a Coroutine
    3. Mimicking Async Behaviour with Coroutines
    4. Controlling Coroutines
    5. Testing Coroutines
    6. Conclusion
  6. 6. Reactive Programming
    1. Building a Basic Reactive System
      1. Defining Our Subjects
      2. Building Our Display Observer
      3. Building Our Heater and HeatLoss Observer
      4. Getting User Input By Callbacks
    2. Enabling Broadcasting With An Event Bus
      1. Building Our Event Bus Struct
      2. Building Our Event Bus Handle
      3. Interacting With Our Event Bus With Async Tasks
    3. Conclusion
  7. 7. Customising Tokio
    1. Building a Runtime
    2. Processing Tasks With LocalPools
    3. Getting Unsafe With Thread Data
    4. Graceful Shutdowns
    5. Conclusion
  8. 8. The Actor Model
    1. Building a Basic Actor
    2. Actors Vs Mutexes
    3. Implementing the Router Pattern
    4. Actor State Recovery
    5. Actor Supervision
    6. Conclusion
  9. 9. Design Patterns
    1. Building an Isolated Module
    2. Waterfall Design pattern
    3. The Decorator Pattern
    4. The State Machine Pattern
    5. The Retry Pattern
    6. The Circuit Breaker Pattern
    7. Conclusion
  10. About the Authors

Product information

  • Title: Async Rust
  • Author(s): Maxwell Flitton, Caroline Morton
  • Release date: December 2024
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781098169480