From ec2a9936370120ba346f3c2b738648593547c3d4 Mon Sep 17 00:00:00 2001 From: Michael Buesch Date: Sun, 29 May 2022 15:31:28 +0200 Subject: [PATCH] Remove Send and Sync autotraits from the lock guard Having Send on the lock guard is unsound. Having Sync on the lock guard is unsound, unless T is Sync. Normally this should use negative trait impls to get rid of Send and Sync. However, negative trait impls are not fully stabilized, yet. Use a phantom pointer instead. That's a bit overly strict in that it also removes Sync, if T is Sync. --- src/lib.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 22e2e19..80a240e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -51,6 +51,7 @@ use core::cell::UnsafeCell; use core::fmt; use core::ops::{Deref, DerefMut}; use core::sync::atomic::{AtomicBool, Ordering}; +use core::marker::PhantomData; /// A light-weight lock guarded by an atomic boolean. /// @@ -167,6 +168,7 @@ impl TryLock { Some(Locked { lock: self, order: unlock_order, + _p: PhantomData, }) } else { None @@ -224,6 +226,8 @@ impl fmt::Debug for TryLock { pub struct Locked<'a, T: 'a> { lock: &'a TryLock, order: Ordering, + /// Suppresses Send and Sync autotraits for `struct Locked`. + _p: PhantomData<*mut T>, } impl<'a, T> Deref for Locked<'a, T> {