use std::marker::PhantomData; use futures::Poll; use futures::future::Future; use tokio::sync::lock::Lock as TokioLock; pub use tokio::sync::lock::LockGuard as AsyncLockGuard; pub struct AsyncMutex(TokioLock); unsafe impl Sync for AsyncMutex {} impl AsyncMutex { pub fn new(value: T) -> Self { Self(TokioLock::new(value)) } // to allow any error type (we never error, so we have no error type of our own) pub fn lock(&self) -> LockFuture { LockFuture { lock: self.0.clone(), _error: PhantomData, } } } /// Represents a lock to be held in the future: pub struct LockFuture { lock: TokioLock, // We can't error and we don't want to enforce a specific error type either _error: PhantomData, } impl Future for LockFuture { type Item = AsyncLockGuard; type Error = E; fn poll(&mut self) -> Poll, E> { Ok(self.lock.poll_lock()) } }