UnpinCell

2024年10月16日

AI 摘要 AI 生成,可能有误

作者提出了一种新的 Rust 设计 UnpinCell,作为其先前提出的固定位置(pinned places)提案的一个变体。先前的设计依赖于“固定字段”(pinned fields)这一不寻常的概念来支持固定投影,而这种新方法旨在提供类似的特性,同时保持与 Rust 标准的字段投影行为一致,而无需引入特殊的字段标记。

A variation on my previous design for pinned places has occurred to me that would be more consistent with Rust’s existing feature set.

The most outlandish aspect of the previous design was the notion of “pinned fields,” which support pinned projection. This is quite different from how field projection normally works in Rust: if you have a mutable reference to a struct, you can get a mutable reference to its field, period. (I know Niko Matsakis has recently explored ideas that would change this; this post won’t go into any deep consideration of that proposal.) I’ve come up with a design which would have similar properties, instead of introducing a kind of field marker.

继续阅读全文

为了尊重作者,这里只展示开头几段。你可以去原文阅读,或者在 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.

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.

Pin

AI 摘要The article discusses Pin, a foundational but widely misunderstood part of Rust's async ecosystem that supports self-referential types by preventing them from moving in memory. The author, who originally proposed adding Pin to Rust, acknowledges critiques (such as one from the makers of Mojo) that interacting with Pin creates a 'complexity cliff' for users, and aims to explain what Pin achieves, how it came to be, and why it is hard to use. The author promises to present ideas for resolving this complexity in a follow-up post.

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.