Skip to content

Commit

Permalink
Remove Send and Sync autotraits from the lock guard (#8)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mbuesch committed Jan 6, 2023
1 parent 643b0ec commit 531bfce
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/lib.rs
Expand Up @@ -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.
///
Expand Down Expand Up @@ -167,6 +168,7 @@ impl<T> TryLock<T> {
Some(Locked {
lock: self,
order: unlock_order,
_p: PhantomData,
})
} else {
None
Expand Down Expand Up @@ -224,6 +226,8 @@ impl<T: fmt::Debug> fmt::Debug for TryLock<T> {
pub struct Locked<'a, T: 'a> {
lock: &'a TryLock<T>,
order: Ordering,
/// Suppresses Send and Sync autotraits for `struct Locked`.
_p: PhantomData<*mut T>,
}

impl<'a, T> Deref for Locked<'a, T> {
Expand Down

0 comments on commit 531bfce

Please sign in to comment.