Pin + Arc + Mutex

We could provide a type Mutex2 (bikeshed name)

struct Mutex2<T: ?Sized> {
    notpin: std::marker::PhantomPinned,
    inner: sys::Mutex,
    poison: poison::Flag,
    data: UnsafeCell<T>,
}

impl<T> Mutex2<T> {
    pub fn new(value: T) -> Self { ... }
}

impl<T: ?Sized> Mutex2<T> {
    pub fn lock(self: Pin<&Self>) -> Result<Mutex2Guard<'_, T>, PoisonError<T>> { ... }
    pub fn try_lock(self: Pin<&Self>) -> Result<Mutex2Guard<'_, T>, TryLockError<T>> { ... }
}

Then you can seemless use Pin<Arc<Mutex2<T>>> as a single allocation, or even with stack pinning at zero-allocations!

2 Likes