tokio笔记.md

Future

//虽然和标准库中的不要太一样,但是更好理解
trait Future {
    /// The type of the value returned when the future completes.
    type Item;

    /// The type representing errors that occurred while processing the computation.
    type Error;

    /// The function that will be repeatedly called to see if the future
    /// has completed or not. The `Async` enum can either be `Ready` or
    /// `NotReady` and indicates whether the future is ready to produce
    /// a value or not.
    fn poll(&mut self) -> Result<Async<Self::Item>, Self::Error>;
}

成功的时候返回Ok(Async::Ready(value)),如果没有准备好则返回Async::NotReady.

Stream

trait Stream {
    /// The type of the value yielded by the stream.
    type Item;

    /// The type representing errors that occurred while processing the computation.
    type Error;

    /// The function that will be repeatedly called to see if the stream has
    /// another value it can yield
    fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error>;
}

和Future不一样的是他要持续返回数据,所以Ready中又分了Some和None两种情况.
分成四种情况返回:

  1. Ok(Async::Ready(Some(value))) 表示数据准备好了
  2. Ok(Async::NotReady) 数据没准备好
  3. Ok(Async::Ready(None)) 没数据了
  4. Err(error) 出错了