@@ -258,31 +258,23 @@ impl Sleeper {
258258	/// Wait until one of the [`Future`]s registered with this [`Sleeper`] has completed. 
259259 	pub  fn  wait ( & self )  { 
260260		let  ( cv,  notified_fut_mtx)  = self . setup_wait ( ) ; 
261- 		let  notified_fut = loop  { 
262- 			let  mut  notified_fut_lck = cv. wait ( notified_fut_mtx. lock ( ) . unwrap ( ) ) . unwrap ( ) ; 
263- 			if  let  Some ( notified_fut)  = notified_fut_lck. take ( )  { 
264- 				break  notified_fut; 
265- 			} 
266- 		} ; 
261+ 		let  notified_fut = cv. wait_while ( notified_fut_mtx. lock ( ) . unwrap ( ) ,  |fut_opt| fut_opt. is_none ( ) ) 
262+ 			. unwrap ( ) . take ( ) . expect ( "CV wait shouldn't have returned until the notifying future was set" ) ; 
267263		notified_fut. lock ( ) . unwrap ( ) . callbacks_made  = true ; 
268264	} 
269265
270266	/// Wait until one of the [`Future`]s registered with this [`Sleeper`] has completed or the 
271267 	/// given amount of time has elapsed. Returns true if a [`Future`] completed, false if the time 
272268 	/// elapsed. 
273269 	pub  fn  wait_timeout ( & self ,  max_wait :  Duration )  -> bool  { 
274- 		let  start_time = Instant :: now ( ) ; 
275270		let  ( cv,  notified_fut_mtx)  = self . setup_wait ( ) ; 
276- 		let  notified_fut = loop  { 
277- 			let  wait_duration = max_wait. saturating_sub ( start_time. elapsed ( ) ) ; 
278- 			if  wait_duration == Duration :: from_secs ( 0 )  {  return  false ;  } 
279- 			match  cv. wait_timeout ( notified_fut_mtx. lock ( ) . unwrap ( ) ,  wait_duration)  { 
280- 				Ok ( ( mut  notified_fut,  _) )  if  notified_fut. is_some ( )  =>
281- 					break  notified_fut. take ( ) . unwrap ( ) , 
282- 				Ok ( ( notified_fut_lck,  _) )  => continue , 
271+ 		let  notified_fut =
272+ 			match  cv. wait_timeout_while ( notified_fut_mtx. lock ( ) . unwrap ( ) ,  max_wait,  |fut_opt| fut_opt. is_none ( ) )  { 
273+ 				Ok ( ( _,  e) )  if  e. timed_out ( )  => return  false , 
274+ 				Ok ( ( mut  notified_fut,  _) )  =>
275+ 					notified_fut. take ( ) . expect ( "CV wait shouldn't have returned until the notifying future was set" ) , 
283276				Err ( _)  => panic ! ( "Previous panic while a lock was held led to a lock panic" ) , 
284277			} ; 
285- 		} ; 
286278		notified_fut. lock ( ) . unwrap ( ) . callbacks_made  = true ; 
287279		true 
288280	} 
0 commit comments