@@ -22,17 +22,12 @@ use time::Duration;
22
22
/// due to a time out or not.
23
23
#[ derive( Debug , PartialEq , Eq , Copy , Clone ) ]
24
24
#[ unstable( feature = "wait_timeout" , reason = "newly added" ) ]
25
- pub enum TimedOut {
26
- /// The wait timed out.
27
- Yes ,
28
- /// The wait did not time out.
29
- No
30
- }
25
+ pub struct WaitTimeoutResult ( bool ) ;
31
26
32
- impl TimedOut {
33
- /// Returns `true` iff the value of `self` is `Yes` .
27
+ impl WaitTimeoutResult {
28
+ /// Returns whether the wait was known to have timed out .
34
29
pub fn timed_out ( & self ) -> bool {
35
- * self == TimedOut :: Yes
30
+ self . 0
36
31
}
37
32
}
38
33
@@ -188,16 +183,16 @@ impl Condvar {
188
183
/// preemption or platform differences that may not cause the maximum
189
184
/// amount of time waited to be precisely `dur`.
190
185
///
191
- /// The returned `TimedOut ` value indicates if the timeout is known to have
192
- /// elapsed.
186
+ /// The returned `WaitTimeoutResult ` value indicates if the timeout is
187
+ /// known to have elapsed.
193
188
///
194
189
/// Like `wait`, the lock specified will be re-acquired when this function
195
190
/// returns, regardless of whether the timeout elapsed or not.
196
191
#[ unstable( feature = "wait_timeout" , reason = "waiting for Duration" ,
197
192
issue = "27772" ) ]
198
193
pub fn wait_timeout < ' a , T > ( & self , guard : MutexGuard < ' a , T > ,
199
194
dur : Duration )
200
- -> LockResult < ( MutexGuard < ' a , T > , TimedOut ) > {
195
+ -> LockResult < ( MutexGuard < ' a , T > , WaitTimeoutResult ) > {
201
196
unsafe {
202
197
let me: & ' static Condvar = & * ( self as * const _ ) ;
203
198
me. inner . wait_timeout ( guard, dur)
@@ -217,7 +212,7 @@ impl Condvar {
217
212
guard : MutexGuard < ' a , T > ,
218
213
dur : Duration ,
219
214
f : F )
220
- -> LockResult < ( MutexGuard < ' a , T > , TimedOut ) >
215
+ -> LockResult < ( MutexGuard < ' a , T > , WaitTimeoutResult ) >
221
216
where F : FnMut ( LockResult < & mut T > ) -> bool {
222
217
unsafe {
223
218
let me: & ' static Condvar = & * ( self as * const _ ) ;
@@ -297,10 +292,10 @@ impl StaticCondvar {
297
292
pub fn wait_timeout_ms < ' a , T > ( & ' static self , guard : MutexGuard < ' a , T > , ms : u32 )
298
293
-> LockResult < ( MutexGuard < ' a , T > , bool ) > {
299
294
match self . wait_timeout ( guard, Duration :: from_millis ( ms as u64 ) ) {
300
- Ok ( ( guard, timed_out) ) => Ok ( ( guard, timed_out == TimedOut :: No ) ) ,
295
+ Ok ( ( guard, timed_out) ) => Ok ( ( guard, ! timed_out. timed_out ( ) ) ) ,
301
296
Err ( poison) => {
302
297
let ( guard, timed_out) = poison. into_inner ( ) ;
303
- Err ( PoisonError :: new ( ( guard, timed_out == TimedOut :: No ) ) )
298
+ Err ( PoisonError :: new ( ( guard, ! timed_out. timed_out ( ) ) ) )
304
299
}
305
300
}
306
301
}
@@ -315,21 +310,17 @@ impl StaticCondvar {
315
310
pub fn wait_timeout < ' a , T > ( & ' static self ,
316
311
guard : MutexGuard < ' a , T > ,
317
312
timeout : Duration )
318
- -> LockResult < ( MutexGuard < ' a , T > , TimedOut ) > {
319
- let ( poisoned, success ) = unsafe {
313
+ -> LockResult < ( MutexGuard < ' a , T > , WaitTimeoutResult ) > {
314
+ let ( poisoned, result ) = unsafe {
320
315
let lock = mutex:: guard_lock ( & guard) ;
321
316
self . verify ( lock) ;
322
- let success = if self . inner . wait_timeout ( lock, timeout) {
323
- TimedOut :: No
324
- } else {
325
- TimedOut :: Yes
326
- } ;
327
- ( mutex:: guard_poison ( & guard) . get ( ) , success)
317
+ let success = self . inner . wait_timeout ( lock, timeout) ;
318
+ ( mutex:: guard_poison ( & guard) . get ( ) , WaitTimeoutResult ( !success) )
328
319
} ;
329
320
if poisoned {
330
- Err ( PoisonError :: new ( ( guard, success ) ) )
321
+ Err ( PoisonError :: new ( ( guard, result ) ) )
331
322
} else {
332
- Ok ( ( guard, success ) )
323
+ Ok ( ( guard, result ) )
333
324
}
334
325
}
335
326
@@ -347,7 +338,7 @@ impl StaticCondvar {
347
338
guard : MutexGuard < ' a , T > ,
348
339
dur : Duration ,
349
340
mut f : F )
350
- -> LockResult < ( MutexGuard < ' a , T > , TimedOut ) >
341
+ -> LockResult < ( MutexGuard < ' a , T > , WaitTimeoutResult ) >
351
342
where F : FnMut ( LockResult < & mut T > ) -> bool {
352
343
// This could be made more efficient by pushing the implementation into
353
344
// sys::condvar
@@ -361,7 +352,7 @@ impl StaticCondvar {
361
352
let consumed = & now - & start;
362
353
let guard = guard_result. unwrap_or_else ( |e| e. into_inner ( ) ) ;
363
354
let ( new_guard_result, timed_out) = if consumed > dur {
364
- ( Ok ( guard) , TimedOut :: Yes )
355
+ ( Ok ( guard) , WaitTimeoutResult ( true ) )
365
356
} else {
366
357
match self . wait_timeout ( guard, dur - consumed) {
367
358
Ok ( ( new_guard, timed_out) ) => ( Ok ( new_guard) , timed_out) ,
@@ -372,21 +363,17 @@ impl StaticCondvar {
372
363
}
373
364
} ;
374
365
guard_result = new_guard_result;
375
- if timed_out == TimedOut :: Yes {
366
+ if timed_out. timed_out ( ) {
376
367
let result = f ( guard_result
377
368
. as_mut ( )
378
369
. map ( |g| & mut * * g)
379
370
. map_err ( |e| PoisonError :: new ( & mut * * e. get_mut ( ) ) ) ) ;
380
- let result = if result {
381
- TimedOut :: No
382
- } else {
383
- TimedOut :: Yes
384
- } ;
371
+ let result = WaitTimeoutResult ( !result) ;
385
372
return poison:: map_result ( guard_result, |g| ( g, result) ) ;
386
373
}
387
374
}
388
375
389
- poison:: map_result ( guard_result, |g| ( g, TimedOut :: No ) )
376
+ poison:: map_result ( guard_result, |g| ( g, WaitTimeoutResult ( false ) ) )
390
377
}
391
378
392
379
/// Wakes up one blocked thread on this condvar.
@@ -443,7 +430,7 @@ mod tests {
443
430
444
431
use super :: StaticCondvar ;
445
432
use sync:: mpsc:: channel;
446
- use sync:: { StaticMutex , Condvar , Mutex , Arc , TimedOut } ;
433
+ use sync:: { StaticMutex , Condvar , Mutex , Arc } ;
447
434
use sync:: atomic:: { AtomicUsize , Ordering } ;
448
435
use thread;
449
436
use time:: Duration ;
@@ -544,7 +531,7 @@ mod tests {
544
531
let ( g, timed_out) = C . wait_timeout_with ( g, Duration :: new ( 0 , 1000 ) , |_| {
545
532
false
546
533
} ) . unwrap ( ) ;
547
- assert_eq ! ( timed_out, TimedOut :: Yes ) ;
534
+ assert ! ( timed_out. timed_out ( ) ) ;
548
535
549
536
let ( tx, rx) = channel ( ) ;
550
537
let _t = thread:: spawn ( move || {
@@ -577,7 +564,7 @@ mod tests {
577
564
_ => true ,
578
565
}
579
566
} ) . unwrap ( ) ;
580
- assert_eq ! ( timed_out, TimedOut :: No ) ;
567
+ assert ! ( ! timed_out. timed_out ( ) ) ;
581
568
}
582
569
583
570
#[ test]
0 commit comments