如何从 Pin<Arc<Mutex<T>>> 中获取 Pin<&mut T>

How to get Pin<&mut T> out of Pin<Arc<Mutex<T>>>

我正在编写一个需要共享流所有权的流适配器。

将流(私下)存储在 Pin<Arc<Mutex<impl Stream>>> 中是否正确?

如何在直播中呼叫 poll_next(self: Pin<&mut Stream>

Mutex 没有所谓的 structural pinning,在 std::pin 模块文档中有描述:

It turns out that it is actually up to the author of the data structure to decide whether the pinned projection for a particular field turns Pin<&mut Struct> into Pin<&mut Field> or &mut Field. [...] As the author of a data structure you get to decide for each field whether pinning "propagates" to this field or not. Pinning that propagates is also called "structural", because it follows the structure of the type.

Mutex<T> 没有结构固定,因为结构的固定性(Mutex)不会传播到字段(T)——它 可以安全地从 Pin<&mut Mutex<T>> 中获取未固定的 &mut T(即,通过使用 Deref 而不是 DerefMut 并锁定互斥量),即使如果 TMutex<T>!Unpin

为了把这个&mut T变成一个Pin<&mut T>,你必须再做一层unchecked guarantee,用unsafePin::new_unchecked来证明T本身从未动过。这似乎违背了 "outer" Pin.

的目的

根据您的数据结构允许的内容,您可以考虑以下选项之一:

  • Arc<Mutex> 编写一个包装器,当通过 Pin 访问时固定 Mutex 的内容(您无法实现 Deref,但您可以编写返回 MutexGuards 并在内部进行 Pin 包装的方法)
  • 如果您的数据结构允许,只需将共享可变性推入实现 Stream 的类型。