Skip to content

Commit 9b48a5f

Browse files
committed
Use PhantomPinned in RWLock to enforce Pin usage.
1 parent 9b2112a commit 9b48a5f

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

library/std/src/sys_common/rwlock.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::marker::PhantomPinned;
12
use crate::pin::Pin;
23
use crate::sys::rwlock as imp;
34

@@ -6,22 +7,25 @@ use crate::sys::rwlock as imp;
67
/// This structure is entirely unsafe and serves as the lowest layer of a
78
/// cross-platform binding of system rwlocks. It is recommended to use the
89
/// safer types at the top level of this crate instead of this type.
9-
pub struct RWLock(imp::RWLock);
10+
pub struct RWLock {
11+
inner: imp::RWLock,
12+
_pinned: PhantomPinned,
13+
}
1014

1115
impl RWLock {
1216
/// Creates a new reader-writer lock for use.
1317
///
1418
/// Behavior is undefined if the reader-writer lock is moved after it is
1519
/// first used with any of the functions below.
1620
pub const fn new() -> RWLock {
17-
RWLock(imp::RWLock::new())
21+
RWLock { inner: imp::RWLock::new(), _pinned: PhantomPinned }
1822
}
1923

2024
/// Acquires shared access to the underlying lock, blocking the current
2125
/// thread to do so.
2226
#[inline]
2327
pub fn read(self: Pin<&Self>) {
24-
unsafe { self.0.read() }
28+
unsafe { self.inner.read() }
2529
}
2630

2731
/// Attempts to acquire shared access to this lock, returning whether it
@@ -30,14 +34,14 @@ impl RWLock {
3034
/// This function does not block the current thread.
3135
#[inline]
3236
pub fn try_read(self: Pin<&Self>) -> bool {
33-
unsafe { self.0.try_read() }
37+
unsafe { self.inner.try_read() }
3438
}
3539

3640
/// Acquires write access to the underlying lock, blocking the current thread
3741
/// to do so.
3842
#[inline]
3943
pub fn write(self: Pin<&Self>) {
40-
unsafe { self.0.write() }
44+
unsafe { self.inner.write() }
4145
}
4246

4347
/// Attempts to acquire exclusive access to this lock, returning whether it
@@ -46,15 +50,15 @@ impl RWLock {
4650
/// This function does not block the current thread.
4751
#[inline]
4852
pub fn try_write(self: Pin<&Self>) -> bool {
49-
unsafe { self.0.try_write() }
53+
unsafe { self.inner.try_write() }
5054
}
5155

5256
/// Unlocks previously acquired shared access to this lock.
5357
///
5458
/// Behavior is undefined if the current thread does not have shared access.
5559
#[inline]
5660
pub unsafe fn read_unlock(self: Pin<&Self>) {
57-
self.0.read_unlock()
61+
self.inner.read_unlock()
5862
}
5963

6064
/// Unlocks previously acquired exclusive access to this lock.
@@ -63,7 +67,7 @@ impl RWLock {
6367
/// exclusive access.
6468
#[inline]
6569
pub unsafe fn write_unlock(self: Pin<&Self>) {
66-
self.0.write_unlock()
70+
self.inner.write_unlock()
6771
}
6872
}
6973

@@ -72,6 +76,6 @@ impl Drop for RWLock {
7276
fn drop(&mut self) {
7377
// SAFETY: The rwlock wasn't moved since using any of its
7478
// functions, because they all require a Pin.
75-
unsafe { self.0.destroy() }
79+
unsafe { self.inner.destroy() }
7680
}
7781
}

0 commit comments

Comments
 (0)