#shared #pinned #mutable #access #data #mutex #pin

pinarcmutex

Provides a PinArcMutex type, for shared mutable access to pinned data

2 releases

0.1.1 Jan 29, 2023
0.1.0 Jan 29, 2023

#1449 in Asynchronous

MIT/Apache

8KB

Provides a PinArcMutex<T> type. This type provides a safe API for getting shared mutable access to pinned data.


lib.rs:

This crate provides the PinArcMutex type, which offers shared mutable access to pinned state.

Problem

Rust already provides good solutions to the problem of shared mutable state - often, this looks like Arc<Mutex<T>>. However, sometimes we additionally need that shared mutable state to be pinned. One example of this is wanting to poll a Stream from multiple threads.

It turns out that there are no great ways to do this. The fundamental problem is that Mutex types in std and tokio have no structural pinning. Unfortunately, that cannot be solved by designing a better PinnedMutex type either - the real limitation is actually in [Pin] itself. Specifically, because of methods like get_ref, the Pin API makes it impossible to assert pinned-ness of a T without creating a &mut T, which is prohibitive for cases like mutexes.

Alternatives

If your T is Unpin, you can use a Arc<tokio::Mutex<T>> directly and do not need this crate.

If you do not mind an extra allocation, you can also get a similar API without an extra dependency via Arc<tokio::Mutex<Pin<Box<T>>>>.

MSRV

This crate has the same MSRV as its only dependency, tokio.

Dependencies

~2–7.5MB
~47K SLoC