@@ -240,15 +240,8 @@ impl Sleeper {
240240 /// Wait until one of the [`Future`]s registered with this [`Sleeper`] has completed.
241241 pub fn wait ( & self ) {
242242 let ( cv, notified_fut_mtx) = self . setup_wait ( ) ;
243- let notified_fut = {
244- let mut notified_fut_lck = notified_fut_mtx. lock ( ) . unwrap ( ) ;
245- loop {
246- if let Some ( notified_fut) = notified_fut_lck. take ( ) {
247- break notified_fut;
248- }
249- notified_fut_lck = cv. wait ( notified_fut_lck) . unwrap ( ) ;
250- }
251- } ;
243+ let notified_fut = cv. wait_while ( notified_fut_mtx. lock ( ) . unwrap ( ) , |fut_opt| fut_opt. is_none ( ) )
244+ . unwrap ( ) . take ( ) . expect ( "CV wait shouldn't have returned until the notifying future was set" ) ;
252245 notified_fut. lock ( ) . unwrap ( ) . callbacks_made = true ;
253246 }
254247
@@ -257,19 +250,14 @@ impl Sleeper {
257250 /// elapsed.
258251 #[ cfg( any( test, feature = "std" ) ) ]
259252 pub fn wait_timeout ( & self , max_wait : Duration ) -> bool {
260- let start_time = Instant :: now ( ) ;
261253 let ( cv, notified_fut_mtx) = self . setup_wait ( ) ;
262- let notified_fut = {
263- let mut notified_fut_lck = notified_fut_mtx. lock ( ) . unwrap ( ) ;
264- loop {
265- if let Some ( notified_fut) = notified_fut_lck. take ( ) {
266- break notified_fut;
267- }
268- let sleep_time = max_wait. checked_sub ( start_time. elapsed ( ) ) . unwrap_or ( Duration :: from_secs ( 0 ) ) ;
269- if sleep_time == Duration :: from_secs ( 0 ) { return false ; }
270- notified_fut_lck = cv. wait_timeout ( notified_fut_lck, max_wait) . unwrap ( ) . 0 ;
271- }
272- } ;
254+ let notified_fut =
255+ match cv. wait_timeout_while ( notified_fut_mtx. lock ( ) . unwrap ( ) , max_wait, |fut_opt| fut_opt. is_none ( ) ) {
256+ Ok ( ( _, e) ) if e. timed_out ( ) => return false ,
257+ Ok ( ( mut notified_fut, _) ) =>
258+ notified_fut. take ( ) . expect ( "CV wait shouldn't have returned until the notifying future was set" ) ,
259+ Err ( _) => panic ! ( "Previous panic while a lock was held led to a lock panic" ) ,
260+ } ;
273261 notified_fut. lock ( ) . unwrap ( ) . callbacks_made = true ;
274262 true
275263 }
0 commit comments