Pin

2024年7月19日

AI 摘要 AI 生成,可能有误

本文讨论了 Pin——Rust 异步生态系统中一个基础性却被广泛误解的组成部分,它通过防止自引用类型在内存中移动来支持这类类型。作者是最初提议将 Pin 加入 Rust 的人,他承认了一些批评意见(例如来自 Mojo 开发者的批评),即与 Pin 交互会给用户带来“复杂度悬崖”,并旨在解释 Pin 实现了什么、它是如何产生的,以及它为何难以使用。作者承诺将在后续文章中提出解决这一复杂性的思路。

The Pin type (and the concept of pinning in general) is a foundational building block on which the rest of the the Rust async ecosystem stands. Unfortunately, it has also been one of the least accessible and most misunderstood elements of async Rust. This post is meant to explain what Pin achieves, how it came to be, and what the current problem with Pin is.

There was an interesting post a few months ago on the blog of the company Modular, which is developing a new language called Mojo. In a brief section discussing Pin in Rust, I found that it very succinctly captured the zeitgeist of the public discussion of the subject:

In Rust, there is no concept of value identity. For a self-referential struct pointing to its own member, that data can become invalid if the object moves, as it’ll be pointing to the old location in memory. This creates a complexity spike, particularly in parts of async Rust where futures need to be self-referential and store state, so you must wrap Self with Pin to guarantee it’s not going to move. In Mojo, objects have an identity so referring to self.foo will always return the correct location in memory, without any additional complexity required for the programmer.

继续阅读全文

为了尊重作者,这里只展示开头几段。你可以去原文阅读,或者在 HorizonReader 里读:划线做笔记、让 AI 帮忙。

更多来自 Without Boats

Generators with UnpinCell

AI 摘要The author argues that the newly proposed UnpinCell wrapper type, which converts a pinned mutable reference into a plain mutable reference, could solve the biggest outstanding problem with Rust generators: the Iterator interface does not permit self-referential values. Because Pin was designed as a backward-compatible, opt-in contract, pre-existing traits like Iterator—which takes a plain mutable reference rather than a pinned one—cannot be implemented by self-referential types. As a result, generators cannot support self-referential values the way async functions do, unless the Iterator interface adopts pinned references.

UnpinCell

AI 摘要The author proposes a new Rust design, UnpinCell, as a variation on their earlier pinned places proposal. Whereas the previous design relied on the unusual concept of 'pinned fields' to support pinned projection, this new approach aims to offer similar properties while remaining consistent with Rust's standard field projection behavior, without introducing a special field marker.

Pinned places

AI 摘要The article explains that Rust's Pin type was designed in 2018 as a minimal, library-only solution to enable shipping async/await quickly, but this choice created a significant complexity cliff for users who must interact with pinned references, especially when implementing Future by hand. The author argues that these difficulties stem not from the inherent complexity of pinning as a concept, but from Pin being a pure library type lacking language support. The post proposes a set of fully backward-compatible language changes to make working with pinned references feel much closer to working with ordinary references.

Ownership

AI 摘要This article explains how substructural type theory applies to programming language design, arguing that designers should use accessible metaphors like 'ownership' to help ordinary programmers understand concepts such as constructors, destructors, and preventing aliasing of objects that represent external resources. Using Rust's ownership model as the primary example, the author aims to demystify these ideas so they become more common knowledge among programmers. While the post contains nothing groundbreaking for experts, it includes notes critiquing certain aspects of Rust's implementation that the author believes are incorrect.

References are like jumps

AI 摘要The article revisits C.A.R. Hoare's 1974 critique of references and pointers in high-level languages, in which he argued they reintroduce machine-code problems—such as address/content confusion, unconstrained mutation through aliasing, lack of independent meaning, and performance overhead—calling them 'a step backward.' The author contends that while Hoare's famous 'billion dollar mistake' was the null pointer, the industry's broader disregard of his warnings about pointers in general was a 'trillion dollar mistake,' because mutable, aliased state destroys the ability to reason locally about a program's behavior. Preventing accidental mutation of aliased state, the author concludes, is essential to building correctly functioning software.