Hasty Briefsbeta

Bilingual

Async from scratch 3: Pinned against the wall

a year ago
  • #Async
  • #Rust
  • #Pinning
  • The article discusses the concept of pinning in Rust, particularly in the context of asynchronous programming and futures.
  • It explains the difference between associated types and generics in traits, highlighting how associated types help with type inference and provide a single output type for a given base type.
  • The article delves into the challenges of self-referential structs in Rust, demonstrating how raw pointers can lead to issues like dangling references and memory unsafety.
  • It introduces the `Pin` type as a solution to safely handle self-referential data by restricting mutable access to prevent moving pinned data.
  • The article explains the `Unpin` marker trait, which allows types to opt out of pinning constraints, and how Rust defaults to implementing `Unpin` for most types.
  • It covers projection techniques for accessing fields of pinned structs safely, including manual projection methods and the use of the `pin-project` crate.
  • The article provides a practical example of implementing a pinned future for a trick-or-treat scenario, showing how to manage self-referential data safely.
  • It concludes by acknowledging the complexity of self-referential types and hints at future topics on running multiple futures concurrently.