Rust Concepts

Advanced Rust from the type system to unsafe code, async and production, measured on Rust 1.100 and sourced from the official docs, books and papers, one concept at a time.

Sort

Threads & Atomics

Async

1

Futures, Wakers and the Poll Model

The Future trait with Poll, Context and Waker, the enum state machine the compiler generates for an async fn (measured with size_of_val and -Zprint-type-sizes), why future size grows with what is held across .await and where Box::pin helps, a minimal std-only block_on executor built on Wake and thread park, and the silent hang of a Pending without a wake, including how it disguises itself as latency under tokio::time::timeout.

2

Pin and Self-Referential Futures

Why an async fn state machine can point into itself (measured in memory), the Pin guarantee and the Unpin auto trait, the real compiler errors when polling an unpinned future, pin! versus Box::pin, structural pinning and projection with pin-project-lite (including its Drop ban and PinnedDrop), and what happens natively and under Miri when unsafe code moves a pinned future, with the RFC 2349 rationale.

3

Tokio Runtime Internals

The current_thread and multi_thread schedulers and the drivers a hand-built Builder leaves off, worker local queues, the global queue and work stealing measured with tokio_unstable steal counters, the LIFO slot measured on a ping-pong, the 128-unit cooperative budget with consume_budget and yield_now, the mio I/O driver over kqueue and epoll and the millisecond timer wheel, and a measured 300 ms stall from std::thread::sleep inside a task fixed with spawn_blocking or block_in_place, on tokio 1.54.1.

4

Async Traits and Async Closures

async fn in traits and RPITIT since 1.75, the future cannot be sent between threads safely error when spawning generically and its fixes with an explicit -> impl Future + Send desugaring or trait_variant::make, return type notation still unstable in 1.100, why traits with async methods are not dyn compatible and what boxing with async-trait or Pin<Box<dyn Future>> costs in allocations per call, and async closures with AsyncFn, AsyncFnMut and AsyncFnOnce (1.85) versus closures returning futures, including the Send gap of AsyncFn.

5

Cancellation Safety and Graceful Shutdown

Dropping a future as cancellation, what tokio::select! does to the losing branches, JoinHandle detach versus abort, the Tokio cancel-safety lists, a measured data loss with read_exact in a select! loop fixed with a persistent buffer and read_buf, JoinSet, CancellationToken and child tokens, and a graceful shutdown with signal handling, broadcast cancellation, TaskTracker and a deadline, including the SIGTERM that ctrl_c alone does not catch.