Skip to content

Commit d7797fb

Browse files
committed
Add comment about Mapped(Mutex|RwLockWrite)Guard variance.
1 parent ca2a39a commit d7797fb

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

library/std/src/sync/mutex.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@ unsafe impl<T: ?Sized + Sync> Sync for MutexGuard<'_, T> {}
245245
#[unstable(feature = "mapped_lock_guards", issue = "none")]
246246
#[clippy::has_significant_drop]
247247
pub struct MappedMutexGuard<'a, T: ?Sized + 'a> {
248+
// NB: we use a pointer instead of `&'a mut T` to avoid `noalias` violations, because a
249+
// `MappedMutexGuard` argument doesn't hold uniqueness for its whole scope, only until it drops.
250+
// `NonNull` is covariant over `T`, so we add a `PhantomData<&'a mut T>` field
251+
// below for the correct variance over `T` (invariance).
248252
data: NonNull<T>,
249253
inner_state: &'a MutexState,
250254
poison: poison::Guard,

library/std/src/sync/rwlock.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ unsafe impl<T: ?Sized + Sync> Sync for MappedRwLockReadGuard<'_, T> {}
194194
#[unstable(feature = "mapped_lock_guards", issue = "none")]
195195
#[clippy::has_significant_drop]
196196
pub struct MappedRwLockWriteGuard<'a, T: ?Sized + 'a> {
197+
// NB: we use a pointer instead of `&'a mut T` to avoid `noalias` violations, because a
198+
// `MappedRwLockWriteGuard` argument doesn't hold uniqueness for its whole scope, only until it drops.
199+
// `NonNull` is covariant over `T`, so we add a `PhantomData<&'a mut T>` field
200+
// below for the correct variance over `T` (invariance).
197201
data: NonNull<T>,
198202
inner_state: &'a RwLockState,
199203
poison: poison::Guard,

0 commit comments

Comments
 (0)