committed by
					
						 Sean McArthur
						Sean McArthur
					
				
			
			
				
	
			
			
			
						parent
						
							7bd3619ece
						
					
				
				
					commit
					c417d6dab8
				
			
							
								
								
									
										53
									
								
								src/wait.rs
									
									
									
									
									
								
							
							
						
						
									
										53
									
								
								src/wait.rs
									
									
									
									
									
								
							| @@ -2,7 +2,7 @@ use std::sync::Arc; | ||||
| use std::thread; | ||||
| use std::time::{Duration, Instant}; | ||||
|  | ||||
| use futures::{Async, AsyncSink, Future, Sink, Stream}; | ||||
| use futures::{Async, Future, Stream}; | ||||
| use futures::executor::{self, Notify}; | ||||
|  | ||||
| // pub(crate) | ||||
| @@ -43,14 +43,6 @@ where S: Stream { | ||||
|     } | ||||
| } | ||||
|  | ||||
| pub fn sink<S>(sink: S, timeout: Option<Duration>) -> WaitSink<S> | ||||
| where S: Sink { | ||||
|     WaitSink { | ||||
|         sink: executor::spawn(sink), | ||||
|         timeout: timeout, | ||||
|     } | ||||
| } | ||||
|  | ||||
| #[derive(Debug)] | ||||
| pub enum Waited<E> { | ||||
|     TimedOut, | ||||
| @@ -113,49 +105,6 @@ where S: Stream { | ||||
|     } | ||||
| } | ||||
|  | ||||
| pub struct WaitSink<S> { | ||||
|     sink: executor::Spawn<S>, | ||||
|     timeout: Option<Duration>, | ||||
| } | ||||
|  | ||||
| impl<S> WaitSink<S> | ||||
| where S: Sink { | ||||
|     pub fn send(&mut self, mut item: S::SinkItem) -> Result<(), Waited<S::SinkError>> { | ||||
|         if let Some(dur) = self.timeout { | ||||
|  | ||||
|             let start = Instant::now(); | ||||
|             let deadline = start + dur; | ||||
|             let notify = Arc::new(ThreadNotify { | ||||
|                 thread: thread::current(), | ||||
|             }); | ||||
|  | ||||
|             loop { | ||||
|                 let now = Instant::now(); | ||||
|                 if now >= deadline { | ||||
|                     return Err(Waited::TimedOut); | ||||
|                 } | ||||
|                 item = match self.sink.start_send_notify(item, ¬ify, 0)? { | ||||
|                     AsyncSink::Ready => return Ok(()), | ||||
|                     AsyncSink::NotReady(val) => val, | ||||
|                 }; | ||||
|                 thread::park_timeout(deadline - now); | ||||
|             } | ||||
|         } else { | ||||
|             let notify = Arc::new(ThreadNotify { | ||||
|                 thread: thread::current(), | ||||
|             }); | ||||
|  | ||||
|             loop { | ||||
|                 item = match self.sink.start_send_notify(item, ¬ify, 0)? { | ||||
|                     AsyncSink::Ready => return Ok(()), | ||||
|                     AsyncSink::NotReady(val) => val, | ||||
|                 }; | ||||
|                 thread::park(); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| struct ThreadNotify { | ||||
|     thread: thread::Thread, | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user