ready/
lib.rs

1#![forbid(unsafe_code, future_incompatible)]
2#![deny(missing_debug_implementations, bad_style)]
3#![deny(missing_docs)]
4#![cfg_attr(test, deny(warnings))]
5
6//! ## Example
7//!
8//! ```rust
9//! #![feature(futures_api)]
10//!
11//! use std::pin::Pin;
12//! use std::task::{Poll, Waker};
13//! use futures::prelude::*;
14//! use std::io;
15//!
16//! struct Fut;
17//!
18//! impl Future for Fut {
19//!   type Output = ();
20//!   fn poll(self: Pin<&mut Self>, waker: &Waker) -> Poll<Self::Output> {
21//!     Poll::Ready(())
22//!   }
23//! }
24//!
25//! impl ready::Ready for Fut {
26//!   type Ok = ();
27//!   type Err = io::Error;
28//!   fn poll_ready(&mut self, waker: &Waker)
29//!     -> Poll<Result<Self::Ok, Self::Err>> {
30//!     Poll::Ready(Ok(()))
31//!   }
32//! }
33//! ```
34
35#![feature(futures_api)]
36
37use std::task::{Poll, Waker};
38
39/// Determine if the underlying API can be written to.
40pub trait WriteReady {
41  /// The type of successful values yielded by this trait.
42  type Ok;
43
44  /// The type of failures yielded by this trait.
45  type Err: std::error::Error + Send + Sync;
46
47  /// Check if the underlying API can be written to.
48  fn poll_write_ready(&mut self, waker: &Waker) -> Poll<Result<Self::Ok, Self::Err>>;
49}
50
51/// Determine if the underlying API can be read from.
52pub trait ReadReady {
53  /// The type of successful values yielded by this trait.
54  type Ok;
55
56  /// The type of failures yielded by this trait.
57  type Err: std::error::Error + Send + Sync;
58
59  /// Check if the underlying API can be read from.
60  fn poll_read_ready(&mut self, waker: &Waker) -> Poll<Result<Self::Ok, Self::Err>>;
61}
62
63/// Determine if a struct is ready to yield futures.
64///
65/// This is useful when a `Stream` borrows an internal struct, and the internal
66/// struct is in charge of establishing the io channel. That way the stream and
67/// the readiness can be decoupled.
68///
69/// Once the IO channel is ready, `poll_ready` should always return
70/// `Poll::Ready`.
71pub trait Ready {
72  /// The type of successful values yielded by this trait.
73  type Ok;
74
75  /// The type of failures yielded by this trait.
76  type Err: std::error::Error + Send + Sync;
77
78  /// Check if the stream can be read from.
79  fn poll_ready(&mut self, waker: &Waker) -> Poll<Result<Self::Ok, Self::Err>>;
80}
81
82/// Extract an error from the underlying struct that isn't propagated through
83/// regular channels.
84///
85/// This is common in `TcpListener` / `UdsStream` structs where this trait can
86/// be used to access the `SO_ERROR` option on the socket.
87///
88/// Both `Ok` and `Err` are error types. If no error exists `take_error` should
89/// return `Ok(None)`.
90pub trait TakeError {
91  /// The type of successful values yielded by this trait.
92  type Ok: std::error::Error + Send + Sync;
93
94  /// The type of failures yielded by this trait.
95  type Err: std::error::Error + Send + Sync;
96
97  /// Return an underlying error value of the struct.
98  fn take_error(&self) -> Result<Option<Self::Ok>, Self::Err>;
99}