Skip to content

Commit

Permalink
Restrict mapping function bounds to prevent possible UB until it has …
Browse files Browse the repository at this point in the history
…been proven safe
  • Loading branch information
eira-fransham committed Aug 25, 2021
1 parent a89fb5c commit de6920c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
5 changes: 2 additions & 3 deletions lock_api/src/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,8 +509,7 @@ impl<'a, R: RawMutex + 'a, T: ?Sized + 'a> MutexGuard<'a, R, T> {
#[inline]
pub fn map<U, F>(s: Self, f: F) -> MappedMutexGuard<'a, R, U>
where
F: FnOnce(&'a mut T) -> U,
U: 'a,
F: FnOnce(&mut T) -> U,
{
let raw = &s.mutex.raw;
let data = unsafe { &mut *s.mutex.data.get() };
Expand All @@ -536,7 +535,7 @@ impl<'a, R: RawMutex + 'a, T: ?Sized + 'a> MutexGuard<'a, R, T> {
#[inline]
pub fn try_map<U, F>(s: Self, f: F) -> Result<MappedMutexGuard<'a, R, U>, Self>
where
F: FnOnce(&'a mut T) -> Option<U>,
F: FnOnce(&mut T) -> Option<U>,
{
let raw = &s.mutex.raw;
let data = unsafe { &mut *s.mutex.data.get() };
Expand Down
8 changes: 4 additions & 4 deletions lock_api/src/rwlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,7 @@ impl<'a, R: RawRwLock + 'a, T: ?Sized + 'a> RwLockReadGuard<'a, R, T> {
#[inline]
pub fn map<U, F>(s: Self, f: F) -> MappedRwLockReadGuard<'a, R, U>
where
F: FnOnce(&'a T) -> U,
F: FnOnce(&T) -> U,
{
let raw = &s.rwlock.raw;
let data = f(unsafe { &*s.rwlock.data.get() });
Expand All @@ -1210,7 +1210,7 @@ impl<'a, R: RawRwLock + 'a, T: ?Sized + 'a> RwLockReadGuard<'a, R, T> {
#[inline]
pub fn try_map<U, F>(s: Self, f: F) -> Result<MappedRwLockReadGuard<'a, R, U>, Self>
where
F: FnOnce(&'a T) -> Option<U>,
F: FnOnce(&T) -> Option<U>,
{
let raw = &s.rwlock.raw;
let data = match f(unsafe { &*s.rwlock.data.get() }) {
Expand Down Expand Up @@ -1474,7 +1474,7 @@ impl<'a, R: RawRwLock + 'a, T: ?Sized + 'a> RwLockWriteGuard<'a, R, T> {
#[inline]
pub fn map<U, F>(s: Self, f: F) -> MappedRwLockWriteGuard<'a, R, U>
where
F: FnOnce(&'a mut T) -> U,
F: FnOnce(&mut T) -> U,
{
let raw = &s.rwlock.raw;
let data = f(unsafe { &mut *s.rwlock.data.get() });
Expand All @@ -1498,7 +1498,7 @@ impl<'a, R: RawRwLock + 'a, T: ?Sized + 'a> RwLockWriteGuard<'a, R, T> {
#[inline]
pub fn try_map<U, F>(s: Self, f: F) -> Result<MappedRwLockWriteGuard<'a, R, U>, Self>
where
F: FnOnce(&'a mut T) -> Option<U>,
F: FnOnce(&mut T) -> Option<U>,
{
let raw = &s.rwlock.raw;
let data = match f(unsafe { &mut *s.rwlock.data.get() }) {
Expand Down

0 comments on commit de6920c

Please sign in to comment.