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 to 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. Preface
    1. What Is Async Rust?
    2. Who Is This Book For?
    3. Overview of the Chapters
    4. Conventions Used in This Book
    5. O’Reilly Online Learning
    6. How to Contact Us
    7. Acknowledgments
      1. Maxwell
      2. Caroline
  2. 1. Introduction to Async
    1. What Is Async?
    2. Introduction to Processes
    3. What Are Threads?
    4. Where Can We Utilize Async?
      1. Using Async for File I/O
      2. Improving HTTP Request Performance with Async
    5. Summary
  3. 2. Basic Async Rust
    1. Understanding Tasks
    2. Futures
      1. Pinning in Futures
      2. Context in Futures
    3. Waking Futures Remotely
    4. Sharing Data Between Futures
      1. High-Level Data Sharing Between Futures
      2. How Are Futures Processed?
    5. Putting It All Together
    6. Summary
  4. 3. Building Our Own Async Queues
    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
  5. 4. Integrating Networking into Our Own Async Runtime
    1. Understanding Executors and Connectors
    2. Integrating hyper into Our Async Runtime
    3. Building an HTTP Connection
    4. Implementing the Tokio AsyncRead Trait
    5. Implementing the Tokio AsyncWrite Trait
    6. Connecting and Running Our Client
    7. Introducing mio
      1. Polling Sockets in Futures
      2. Sending Data over the Socket
    8. Summary
  6. 5. Coroutines
    1. Introducing 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 Behavior with Coroutines
    4. Controlling Coroutines
    5. Testing Coroutines
    6. Summary
  7. 6. Reactive Programming
    1. Building a Basic Reactive System
      1. Defining Our Subjects
      2. Building Our Display Observer
      3. Building Our Heater and Heat-Loss Observer
      4. Getting User Input via 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 via Async Tasks
    3. Summary
  8. 7. Customizing Tokio
    1. Building a Runtime
    2. Processing Tasks with Local Pools
    3. Getting Unsafe with Thread Data
    4. Graceful Shutdowns
    5. Summary
  9. 8. The Actor Model
    1. Building a Basic Actor
    2. Working with Actors Versus Mutexes
    3. Implementing the Router Pattern
    4. Implementing State Recovery for Actors
    5. Creating Actor Supervision
    6. Summary
  10. 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. Summary
  11. 10. Building an Async Server with No Dependencies
    1. Setting Up the Basics
    2. Building Our std Async Runtime
      1. Building Our Waker
      2. Building Our Executor
      3. Running Our Executor
      4. Building Our Sender
      5. Building Our Receiver
      6. Building Our Sleep
    3. Building Our Server
      1. Accepting Requests
      2. Handling Requests
    4. Building Our Async Client
    5. Summary
  12. 11. Testing
    1. Performing Basic Sync Testing
    2. Mocking Async Code
    3. Testing For Deadlocks
    4. Testing for Race Conditions
    5. Testing Channel Capacity
    6. Testing Network Interactions
    7. Fine-Grained Future Testing
    8. Summary
  13. Index
  14. About the Authors

Product information

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