Generators with UnpinCell

2024年10月25日

AI 摘要 AI 生成,可能有误

作者认为,新提出的 UnpinCell 包装类型可以将固定(pinned)可变引用转换为普通可变引用,从而解决 Rust 生成器最大的悬而未决的问题:Iterator 接口不允许自引用的值。由于 Pin 被设计成一种向后兼容、可选择加入的契约,像 Iterator 这样预先存在的 trait——它接受的是普通可变引用而非固定引用——无法由自引用类型来实现。因此,生成器无法像异步函数那样支持自引用值,除非 Iterator 接口采用固定引用。

In July, I described a way to make pinning more ergonomic by integrating it more fully into the language. Last week, I develoepd that idea further with the notion of UnpinCell: a wrapper type that lets a user take an &pin mut UnpinCell<T> and produce an &mut T, similar to how other cells let a user take a shared reference to the cell and produce a mutable reference to its contents. I believe that this notion can also solve the biggest outstanding issues facing generators: the fact that the Iterator interface does not permit self-referential values.

As I wrote in my explanation of Pin’s design, the biggest advantage that Pin had over other design ideas was that it was a trivially backward compatible way of introducing a contract that an object will never be moved. But this meant that a trait could only opt into that contract using the new interface; traits that existed before Pin and don’t opt into that contract cannot be implemented by types that have self-referential values. The most problematic trait here is Iterator, because generators (functions that evaluate to iterators in the same way async functions evaluate to futures) would ideally support self-referential values just like async functions do. So long as the interface for Iterator takes a mutable reference and not a pinned mutable reference, implementers must assume the iterator can be moved around and therefore can’t be self-referential.

继续阅读全文

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

更多来自 Without Boats

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.

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.