Asynchronous Programming in Rust

Book description

Get a fundamental understanding of asynchronous programming and Rust's futures by working through examples that show you how everything really works

Key Features

  • Master asynchronous Rust through examples focusing on key concepts
  • Build a solid understanding of concepts such as coroutines, fibers, futures, and callbacks
  • Explore Rust's futures, craft your own runtime, and excel in handling stacks, ABIs, syscalls, and inline assembly
  • Purchase of the print or Kindle book includes a free PDF eBook

Book Description

Step into the world of asynchronous programming with confidence by conquering the challenges of unclear concepts with this hands-on guide. Using functional examples, this book simplifies the trickiest concepts, exploring goroutines, fibers, futures, and callbacks to help you navigate the vast Rust async ecosystem with ease.

You’ll start by building a solid foundation in asynchronous programming and explore diverse strategies for modeling program flow. The book then guides you through concepts like epoll, coroutines, green threads, and callbacks using practical examples. The final section focuses on Rust, examining futures, generators, and the reactor-executor pattern. You’ll apply your knowledge to create your own runtime, solidifying expertise in this dynamic domain. Throughout the book, you’ll not only gain proficiency in Rust's async features but also see how Rust models asynchronous program flow.

By the end of the book, you'll possess the knowledge and practical skills needed to actively contribute to the Rust async ecosystem.

What you will learn

  • Explore the essence of asynchronous program flow and its significance
  • Understand the difference between concurrency and parallelism
  • Gain insights into how computers and operating systems handle concurrent tasks
  • Uncover the mechanics of async/await
  • Understand Rust’s futures by implementing them yourself
  • Implement green threads from scratch to thoroughly understand them

Who this book is for

This book is for programmers who want to enhance their understanding of asynchronous programming, especially those experienced in VM’ed or interpreted languages like C#, Java, Python, JavaScript, and Go. If you work with C or C++ but have had limited exposure to asynchronous programming, this book serves as a resource to broaden your knowledge in this area. Although the examples are predominantly in Rust, the intricacies of Rust’s futures are covered in detail. So, anyone with a keen interest in learning Rust or with working knowledge of Rust will be able to get the most out of this book.

Table of contents

  1. Asynchronous Programming in Rust
  2. Contributors
  3. About the author
  4. About the reviewer
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Conventions used
    6. Get in touch
    7. Share your thoughts
    8. Download a free PDF copy of this book
  6. Part 1:Asynchronous Programming Fundamentals
  7. Chapter 1: Concurrency and Asynchronous Programming: a Detailed Overview
    1. Technical requirements
    2. An evolutionary journey of multitasking
      1. Non-preemptive multitasking
      2. Preemptive multitasking
      3. Hyper-threading
      4. Multicore processors
      5. Do you really write synchronous code?
    3. Concurrency versus parallelism
      1. The mental model I use
      2. Let’s draw some parallels to process economics
      3. Concurrency and its relation to I/O
      4. What about threads provided by the operating system?
      5. Choosing the right reference frame
      6. Asynchronous versus concurrent
    4. The role of the operating system
      1. Concurrency from the operating system’s perspective
      2. Teaming up with the operating system
      3. Communicating with the operating system
    5. The CPU and the operating system
      1. Down the rabbit hole
      2. How does the CPU prevent us from accessing memory we’re not supposed to access?
      3. But can’t we just change the page table in the CPU?
    6. Interrupts, firmware, and I/O
      1. A simplified overview
      2. Interrupts
      3. Firmware
    7. Summary
  8. Chapter 2: How Programming Languages Model Asynchronous Program Flow
    1. Definitions
      1. Threads
    2. Threads provided by the operating system
      1. Creating new threads takes time
      2. Each thread has its own stack
      3. Context switching
      4. Scheduling
      5. The advantage of decoupling asynchronous operations from OS threads
      6. Example
    3. Fibers and green threads
      1. Each stack has a fixed space
      2. Context switching
      3. Scheduling
      4. FFI
    4. Callback based approaches
    5. Coroutines: promises and futures
      1. Coroutines and async/await
    6. Summary
  9. Chapter 3: Understanding OS-Backed Event Queues, System Calls, and Cross-Platform Abstractions
    1. Technical requirements
      1. Running the Linux examples
    2. Why use an OS-backed event queue?
      1. Blocking I/O
      2. Non-blocking I/O
      3. Event queuing via epoll/kqueue and IOCP
    3. Readiness-based event queues
    4. Completion-based event queues
    5. epoll, kqueue, and IOCP
    6. Cross-platform event queues
    7. System calls, FFI, and cross-platform abstractions
      1. The lowest level of abstraction
      2. The next level of abstraction
      3. The highest level of abstraction
    8. Summary
  10. Part 2:Event Queues and Green Threads
  11. Chapter 4: Create Your Own Event Queue
    1. Technical requirements
    2. Design and introduction to epoll
      1. Is all I/O blocking?
    3. The ffi module
      1. Bitflags and bitmasks
      2. Level-triggered versus edge-triggered events
    4. The Poll module
    5. The main program
    6. Summary
  12. Chapter 5: Creating Our Own Fibers
    1. Technical requirements
    2. How to use the repository alongside the book
    3. Background information
      1. Instruction sets, hardware architectures, and ABIs
      2. The System V ABI for x86-64
      3. A quick introduction to Assembly language
    4. An example we can build upon
      1. Setting up our project
      2. An introduction to Rust inline assembly macro
      3. Running our example
    5. The stack
      1. What does the stack look like?
      2. Stack sizes
    6. Implementing our own fibers
      1. Implementing the runtime
      2. Guard, skip, and switch functions
    7. Finishing thoughts
    8. Summary
  13. Part 3:Futures and async/await in Rust
  14. Chapter 6: Futures in Rust
    1. What is a future?
    2. Leaf futures
    3. Non-leaf futures
    4. A mental model of an async runtime
    5. What the Rust language and standard library take care of
    6. I/O vs CPU-intensive tasks
    7. Summary
  15. Chapter 7: Coroutines and async/await
    1. Technical requirements
    2. Introduction to stackless coroutines
    3. An example of hand-written coroutines
      1. Futures module
      2. HTTP module
      3. Do all futures have to be lazy?
      4. Creating coroutines
    4. async/await
      1. coroutine/wait
      2. corofy—the coroutine preprocessor
      3. b-async-await—an example of a coroutine/wait transformation
    5. c-async-await—concurrent futures
    6. Final thoughts
    7. Summary
  16. Chapter 8: Runtimes, Wakers, and the Reactor-Executor Pattern
    1. Technical requirements
    2. Introduction to runtimes and why we need them
      1. Reactors and executors
    3. Improving our base example
      1. Design
      2. Changing the current implementation
    4. Creating a proper runtime
    5. Step 1 – Improving our runtime design by adding a Reactor and a Waker
      1. Creating a Waker
      2. Changing the Future definition
    6. Step 2 – Implementing a proper Executor
    7. Step 3 – Implementing a proper Reactor
    8. Experimenting with our new runtime
      1. An example using concurrency
      2. Running multiple futures concurrently and in parallel
    9. Summary
  17. Chapter 9: Coroutines, Self-Referential Structs, and Pinning
    1. Technical requirements
    2. Improving our example 1 – variables
      1. Setting up the base example
      2. Improving our base example
    3. Improving our example 2 – references
    4. Improving our example 3 – this is… not… good…
    5. Discovering self-referential structs
      1. What is a move?
    6. Pinning in Rust
      1. Pinning in theory
      2. Definitions
      3. Pinning to the heap
      4. Pinning to the stack
      5. Pin projections and structural pinning
    7. Improving our example 4 – pinning to the rescue
      1. future.rs
      2. http.rs
      3. Main.rs
      4. executor.rs
    8. Summary
  18. Chapter 10: Creating Your Own Runtime
    1. Technical requirements
      1. Setting up our example
      2. main.rs
      3. future.rs
      4. http.rs
      5. executor.rs
      6. reactor.rs
    2. Experimenting with our runtime
    3. Challenges with asynchronous Rust
      1. Explicit versus implicit reactor instantiation
      2. Ergonomics versus efficiency and flexibility
      3. Common traits that everyone agrees about
      4. Async drop
    4. The future of asynchronous Rust
    5. Summary
    6. Epilogue
  19. Index
    1. Why subscribe?
  20. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share your thoughts
    3. Download a free PDF copy of this book

Product information

  • Title: Asynchronous Programming in Rust
  • Author(s): Carl Fredrik Samson
  • Release date: February 2024
  • Publisher(s): Packt Publishing
  • ISBN: 9781805128137