Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 61eb52c

Browse files
Merge pull request #625 from nbdd0121/sync_unsized
Use ?Sized in Mutex and RwLock
2 parents 6f4dcad + 732ef10 commit 61eb52c

File tree

2 files changed

+48
-44
lines changed

2 files changed

+48
-44
lines changed

‎src/sync/mutex.rs‎

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ use crate::task::{Context, Poll};
3939
/// #
4040
/// # })
4141
/// ```
42-
pub struct Mutex<T> {
42+
pub struct Mutex<T: ?Sized> {
4343
locked: AtomicBool,
4444
wakers: WakerSet,
4545
value: UnsafeCell<T>,
4646
}
4747

48-
unsafe impl<T: Send> Send for Mutex<T> {}
49-
unsafe impl<T: Send> Sync for Mutex<T> {}
48+
unsafe impl<T: ?Sized + Send> Send for Mutex<T> {}
49+
unsafe impl<T: ?Sized + Send> Sync for Mutex<T> {}
5050

5151
impl<T> Mutex<T> {
5252
/// Creates a new mutex.
@@ -65,7 +65,9 @@ impl<T> Mutex<T> {
6565
value: UnsafeCell::new(t),
6666
}
6767
}
68+
}
6869

70+
impl<T: ?Sized> Mutex<T> {
6971
/// Acquires the lock.
7072
///
7173
/// Returns a guard that releases the lock when dropped.
@@ -91,12 +93,12 @@ impl<T> Mutex<T> {
9193
/// # })
9294
/// ```
9395
pub async fn lock(&self) -> MutexGuard<'_, T> {
94-
pub struct LockFuture<'a, T> {
96+
pub struct LockFuture<'a, T: ?Sized> {
9597
mutex: &'a Mutex<T>,
9698
opt_key: Option<usize>,
9799
}
98100

99-
impl<'a, T> Future for LockFuture<'a, T> {
101+
impl<'a, T: ?Sized> Future for LockFuture<'a, T> {
100102
type Output = MutexGuard<'a, T>;
101103

102104
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
@@ -123,7 +125,7 @@ impl<T> Mutex<T> {
123125
}
124126
}
125127

126-
impl<T> Drop for LockFuture<'_, T> {
128+
impl<T: ?Sized> Drop for LockFuture<'_, T> {
127129
fn drop(&mut self) {
128130
// If the current task is still in the set, that means it is being cancelled now.
129131
if let Some(key) = self.opt_key {
@@ -189,7 +191,7 @@ impl<T> Mutex<T> {
189191
/// let mutex = Mutex::new(10);
190192
/// assert_eq!(mutex.into_inner(), 10);
191193
/// ```
192-
pub fn into_inner(self) -> T {
194+
pub fn into_inner(self) -> T whereT:Sized{
193195
self.value.into_inner()
194196
}
195197

@@ -216,7 +218,7 @@ impl<T> Mutex<T> {
216218
}
217219
}
218220

219-
impl<T: fmt::Debug> fmt::Debug for Mutex<T> {
221+
impl<T: ?Sized + fmt::Debug> fmt::Debug for Mutex<T> {
220222
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
221223
struct Locked;
222224
impl fmt::Debug for Locked {
@@ -238,19 +240,19 @@ impl<T> From<T> for Mutex<T> {
238240
}
239241
}
240242

241-
impl<T: Default> Default for Mutex<T> {
243+
impl<T: ?Sized + Default> Default for Mutex<T> {
242244
fn default() -> Mutex<T> {
243245
Mutex::new(Default::default())
244246
}
245247
}
246248

247249
/// A guard that releases the lock when dropped.
248-
pub struct MutexGuard<'a, T>(&'a Mutex<T>);
250+
pub struct MutexGuard<'a, T: ?Sized>(&'a Mutex<T>);
249251

250-
unsafe impl<T: Send> Send for MutexGuard<'_, T> {}
251-
unsafe impl<T: Sync> Sync for MutexGuard<'_, T> {}
252+
unsafe impl<T: ?Sized + Send> Send for MutexGuard<'_, T> {}
253+
unsafe impl<T: ?Sized + Sync> Sync for MutexGuard<'_, T> {}
252254

253-
impl<T> Drop for MutexGuard<'_, T> {
255+
impl<T: ?Sized> Drop for MutexGuard<'_, T> {
254256
fn drop(&mut self) {
255257
// Use `SeqCst` ordering to synchronize with `WakerSet::insert()` and `WakerSet::update()`.
256258
self.0.locked.store(false, Ordering::SeqCst);
@@ -260,27 +262,27 @@ impl<T> Drop for MutexGuard<'_, T> {
260262
}
261263
}
262264

263-
impl<T: fmt::Debug> fmt::Debug for MutexGuard<'_, T> {
265+
impl<T: ?Sized +fmt::Debug> fmt::Debug for MutexGuard<'_, T> {
264266
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
265267
fmt::Debug::fmt(&**self, f)
266268
}
267269
}
268270

269-
impl<T: fmt::Display> fmt::Display for MutexGuard<'_, T> {
271+
impl<T: ?Sized + fmt::Display> fmt::Display for MutexGuard<'_, T> {
270272
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
271273
(**self).fmt(f)
272274
}
273275
}
274276

275-
impl<T> Deref for MutexGuard<'_, T> {
277+
impl<T: ?Sized> Deref for MutexGuard<'_, T> {
276278
type Target = T;
277279

278280
fn deref(&self) -> &T {
279281
unsafe { &*self.0.value.get() }
280282
}
281283
}
282284

283-
impl<T> DerefMut for MutexGuard<'_, T> {
285+
impl<T: ?Sized> DerefMut for MutexGuard<'_, T> {
284286
fn deref_mut(&mut self) -> &mut T {
285287
unsafe { &mut *self.0.value.get() }
286288
}

‎src/sync/rwlock.rs‎

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ const READ_COUNT_MASK: usize = !(ONE_READ - 1);
4949
/// #
5050
/// # })
5151
/// ```
52-
pub struct RwLock<T> {
52+
pub struct RwLock<T: ?Sized> {
5353
state: AtomicUsize,
5454
read_wakers: WakerSet,
5555
write_wakers: WakerSet,
5656
value: UnsafeCell<T>,
5757
}
5858

59-
unsafe impl<T: Send> Send for RwLock<T> {}
60-
unsafe impl<T: Send + Sync> Sync for RwLock<T> {}
59+
unsafe impl<T: ?Sized + Send> Send for RwLock<T> {}
60+
unsafe impl<T: ?Sized + Send + Sync> Sync for RwLock<T> {}
6161

6262
impl<T> RwLock<T> {
6363
/// Creates a new reader-writer lock.
@@ -77,7 +77,9 @@ impl<T> RwLock<T> {
7777
value: UnsafeCell::new(t),
7878
}
7979
}
80+
}
8081

82+
impl<T: ?Sized> RwLock<T> {
8183
/// Acquires a read lock.
8284
///
8385
/// Returns a guard that releases the lock when dropped.
@@ -99,12 +101,12 @@ impl<T> RwLock<T> {
99101
/// # })
100102
/// ```
101103
pub async fn read(&self) -> RwLockReadGuard<'_, T> {
102-
pub struct ReadFuture<'a, T> {
104+
pub struct ReadFuture<'a, T: ?Sized> {
103105
lock: &'a RwLock<T>,
104106
opt_key: Option<usize>,
105107
}
106108

107-
impl<'a, T> Future for ReadFuture<'a, T> {
109+
impl<'a, T: ?Sized> Future for ReadFuture<'a, T> {
108110
type Output = RwLockReadGuard<'a, T>;
109111

110112
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
@@ -131,7 +133,7 @@ impl<T> RwLock<T> {
131133
}
132134
}
133135

134-
impl<T> Drop for ReadFuture<'_, T> {
136+
impl<T: ?Sized> Drop for ReadFuture<'_, T> {
135137
fn drop(&mut self) {
136138
// If the current task is still in the set, that means it is being cancelled now.
137139
if let Some(key) = self.opt_key {
@@ -224,12 +226,12 @@ impl<T> RwLock<T> {
224226
/// # })
225227
/// ```
226228
pub async fn write(&self) -> RwLockWriteGuard<'_, T> {
227-
pub struct WriteFuture<'a, T> {
229+
pub struct WriteFuture<'a, T: ?Sized> {
228230
lock: &'a RwLock<T>,
229231
opt_key: Option<usize>,
230232
}
231233

232-
impl<'a, T> Future for WriteFuture<'a, T> {
234+
impl<'a, T: ?Sized> Future for WriteFuture<'a, T> {
233235
type Output = RwLockWriteGuard<'a, T>;
234236

235237
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
@@ -256,7 +258,7 @@ impl<T> RwLock<T> {
256258
}
257259
}
258260

259-
impl<T> Drop for WriteFuture<'_, T> {
261+
impl<T: ?Sized> Drop for WriteFuture<'_, T> {
260262
fn drop(&mut self) {
261263
// If the current task is still in the set, that means it is being cancelled now.
262264
if let Some(key) = self.opt_key {
@@ -316,7 +318,7 @@ impl<T> RwLock<T> {
316318
/// let lock = RwLock::new(10);
317319
/// assert_eq!(lock.into_inner(), 10);
318320
/// ```
319-
pub fn into_inner(self) -> T {
321+
pub fn into_inner(self) -> T whereT:Sized{
320322
self.value.into_inner()
321323
}
322324

@@ -343,7 +345,7 @@ impl<T> RwLock<T> {
343345
}
344346
}
345347

346-
impl<T: fmt::Debug> fmt::Debug for RwLock<T> {
348+
impl<T: ?Sized + fmt::Debug> fmt::Debug for RwLock<T> {
347349
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
348350
struct Locked;
349351
impl fmt::Debug for Locked {
@@ -365,19 +367,19 @@ impl<T> From<T> for RwLock<T> {
365367
}
366368
}
367369

368-
impl<T: Default> Default for RwLock<T> {
370+
impl<T: ?Sized + Default> Default for RwLock<T> {
369371
fn default() -> RwLock<T> {
370372
RwLock::new(Default::default())
371373
}
372374
}
373375

374376
/// A guard that releases the read lock when dropped.
375-
pub struct RwLockReadGuard<'a, T>(&'a RwLock<T>);
377+
pub struct RwLockReadGuard<'a, T: ?Sized>(&'a RwLock<T>);
376378

377-
unsafe impl<T: Send> Send for RwLockReadGuard<'_, T> {}
378-
unsafe impl<T: Sync> Sync for RwLockReadGuard<'_, T> {}
379+
unsafe impl<T: ?Sized + Send> Send for RwLockReadGuard<'_, T> {}
380+
unsafe impl<T: ?Sized + Sync> Sync for RwLockReadGuard<'_, T> {}
379381

380-
impl<T> Drop for RwLockReadGuard<'_, T> {
382+
impl<T: ?Sized> Drop for RwLockReadGuard<'_, T> {
381383
fn drop(&mut self) {
382384
let state = self.0.state.fetch_sub(ONE_READ, Ordering::SeqCst);
383385

@@ -388,19 +390,19 @@ impl<T> Drop for RwLockReadGuard<'_, T> {
388390
}
389391
}
390392

391-
impl<T: fmt::Debug> fmt::Debug for RwLockReadGuard<'_, T> {
393+
impl<T: ?Sized + fmt::Debug> fmt::Debug for RwLockReadGuard<'_, T> {
392394
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
393395
fmt::Debug::fmt(&**self, f)
394396
}
395397
}
396398

397-
impl<T: fmt::Display> fmt::Display for RwLockReadGuard<'_, T> {
399+
impl<T: ?Sized + fmt::Display> fmt::Display for RwLockReadGuard<'_, T> {
398400
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
399401
(**self).fmt(f)
400402
}
401403
}
402404

403-
impl<T> Deref for RwLockReadGuard<'_, T> {
405+
impl<T: ?Sized> Deref for RwLockReadGuard<'_, T> {
404406
type Target = T;
405407

406408
fn deref(&self) -> &T {
@@ -409,12 +411,12 @@ impl<T> Deref for RwLockReadGuard<'_, T> {
409411
}
410412

411413
/// A guard that releases the write lock when dropped.
412-
pub struct RwLockWriteGuard<'a, T>(&'a RwLock<T>);
414+
pub struct RwLockWriteGuard<'a, T: ?Sized>(&'a RwLock<T>);
413415

414-
unsafe impl<T: Send> Send for RwLockWriteGuard<'_, T> {}
415-
unsafe impl<T: Sync> Sync for RwLockWriteGuard<'_, T> {}
416+
unsafe impl<T: ?Sized + Send> Send for RwLockWriteGuard<'_, T> {}
417+
unsafe impl<T: ?Sized + Sync> Sync for RwLockWriteGuard<'_, T> {}
416418

417-
impl<T> Drop for RwLockWriteGuard<'_, T> {
419+
impl<T: ?Sized> Drop for RwLockWriteGuard<'_, T> {
418420
fn drop(&mut self) {
419421
self.0.state.store(0, Ordering::SeqCst);
420422

@@ -427,27 +429,27 @@ impl<T> Drop for RwLockWriteGuard<'_, T> {
427429
}
428430
}
429431

430-
impl<T: fmt::Debug> fmt::Debug for RwLockWriteGuard<'_, T> {
432+
impl<T: ?Sized + fmt::Debug> fmt::Debug for RwLockWriteGuard<'_, T> {
431433
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
432434
fmt::Debug::fmt(&**self, f)
433435
}
434436
}
435437

436-
impl<T: fmt::Display> fmt::Display for RwLockWriteGuard<'_, T> {
438+
impl<T: ?Sized + fmt::Display> fmt::Display for RwLockWriteGuard<'_, T> {
437439
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
438440
(**self).fmt(f)
439441
}
440442
}
441443

442-
impl<T> Deref for RwLockWriteGuard<'_, T> {
444+
impl<T: ?Sized> Deref for RwLockWriteGuard<'_, T> {
443445
type Target = T;
444446

445447
fn deref(&self) -> &T {
446448
unsafe { &*self.0.value.get() }
447449
}
448450
}
449451

450-
impl<T> DerefMut for RwLockWriteGuard<'_, T> {
452+
impl<T: ?Sized> DerefMut for RwLockWriteGuard<'_, T> {
451453
fn deref_mut(&mut self) -> &mut T {
452454
unsafe { &mut *self.0.value.get() }
453455
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /