1 use stream::Stream; 2 use executor; 3 4 /// A stream combinator which converts an asynchronous stream to a **blocking 5 /// iterator**. 6 /// 7 /// Created by the `Stream::wait` method, this function transforms any stream 8 /// into a standard iterator. This is implemented by blocking the current thread 9 /// while items on the underlying stream aren't ready yet. 10 #[must_use = "iterators do nothing unless advanced"] 11 #[derive(Debug)] 12 pub struct Wait<S> { 13 stream: executor::Spawn<S>, 14 } 15 16 impl<S> Wait<S> { 17 /// Acquires a reference to the underlying stream that this combinator is 18 /// pulling from. get_ref(&self) -> &S19 pub fn get_ref(&self) -> &S { 20 self.stream.get_ref() 21 } 22 23 /// Acquires a mutable reference to the underlying stream that this 24 /// combinator is pulling from. 25 /// 26 /// Note that care must be taken to avoid tampering with the state of the 27 /// stream which may otherwise confuse this combinator. get_mut(&mut self) -> &mut S28 pub fn get_mut(&mut self) -> &mut S { 29 self.stream.get_mut() 30 } 31 32 /// Consumes this combinator, returning the underlying stream. 33 /// 34 /// Note that this may discard intermediate state of this combinator, so 35 /// care should be taken to avoid losing resources when this is called. into_inner(self) -> S36 pub fn into_inner(self) -> S { 37 self.stream.into_inner() 38 } 39 } 40 new<S: Stream>(s: S) -> Wait<S>41pub fn new<S: Stream>(s: S) -> Wait<S> { 42 Wait { 43 stream: executor::spawn(s), 44 } 45 } 46 47 impl<S: Stream> Iterator for Wait<S> { 48 type Item = Result<S::Item, S::Error>; 49 next(&mut self) -> Option<Self::Item>50 fn next(&mut self) -> Option<Self::Item> { 51 self.stream.wait_stream() 52 } 53 } 54