use {Future, IntoFuture, Poll}; use core::fmt; use super::chain::Chain; /// Future for the `flatten` combinator, flattening a future-of-a-future to get just /// the result of the final future. /// /// This is created by the `Future::flatten` method. #[must_use = "futures do nothing unless polled"] pub struct Flatten where A: Future, A::Item: IntoFuture { state: Chain::Future, ()>, } impl fmt::Debug for Flatten where A: Future + fmt::Debug, A::Item: IntoFuture, <::Item as IntoFuture>::Future: fmt::Debug, { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fmt.debug_struct("Flatten") .field("state", &self.state) .finish() } } pub fn new(future: A) -> Flatten where A: Future, A::Item: IntoFuture, { Flatten { state: Chain::new(future, ()), } } impl Future for Flatten where A: Future, A::Item: IntoFuture, <::Item as IntoFuture>::Error: From<::Error> { type Item = <::Item as IntoFuture>::Item; type Error = <::Item as IntoFuture>::Error; fn poll(&mut self) -> Poll { self.state.poll(|a, ()| { let future = a?.into_future(); Ok(Err(future)) }) } }