Skip to content

Commit

Permalink
feat(client,server) Add Connection::without_shutdown()
Browse files Browse the repository at this point in the history
* Add `server::conn::Connection::without_shutdown`

  Returns wrapper Future instance which allows
  to use `poll_without_shutdown` method
  more ergonomically.

* Add `client::conn::Connection::without_shutdown`

  Returns wrapper Future instance which allows
  to use `poll_without_shutdown` method
  more ergonomically.

* Improve `poll_without_shutdown` docs

Closes #1786
  • Loading branch information
vi authored and seanmonstar committed Mar 26, 2019
1 parent fc18b68 commit edf551b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/client/conn.rs
Expand Up @@ -376,6 +376,10 @@ where
/// upgrade. Once the upgrade is completed, the connection would be "done",
/// but it is not desired to actally shutdown the IO object. Instead you
/// would take it back using `into_parts`.
///
/// Use [`poll_fn`](https://docs.rs/futures/0.1.25/futures/future/fn.poll_fn.html)
/// and [`try_ready!`](https://docs.rs/futures/0.1.25/futures/macro.try_ready.html)
/// to work with this function; or use the `without_shutdown` wrapper.
pub fn poll_without_shutdown(&mut self) -> Poll<(), ::Error> {
match self.inner.as_mut().expect("already upgraded") {
&mut Either::A(ref mut h1) => {
Expand All @@ -386,6 +390,16 @@ where
}
}
}

/// Prevent shutdown of the underlying IO object at the end of service the request,
/// instead run `into_parts`. This is a convenience wrapper over `poll_without_shutdown`.
pub fn without_shutdown(self) -> impl Future<Item=Parts<T>, Error=::Error> {
let mut conn = Some(self);
::futures::future::poll_fn(move || -> ::Result<_> {
try_ready!(conn.as_mut().unwrap().poll_without_shutdown());
Ok(conn.take().unwrap().into_parts().into())
})
}
}

impl<T, B> Future for Connection<T, B>
Expand Down
14 changes: 14 additions & 0 deletions src/server/conn.rs
Expand Up @@ -542,6 +542,10 @@ where
/// upgrade. Once the upgrade is completed, the connection would be "done",
/// but it is not desired to actally shutdown the IO object. Instead you
/// would take it back using `into_parts`.
///
/// Use [`poll_fn`](https://docs.rs/futures/0.1.25/futures/future/fn.poll_fn.html)
/// and [`try_ready!`](https://docs.rs/futures/0.1.25/futures/macro.try_ready.html)
/// to work with this function; or use the `without_shutdown` wrapper.
pub fn poll_without_shutdown(&mut self) -> Poll<(), ::Error> {
loop {
let polled = match *self.conn.as_mut().unwrap() {
Expand All @@ -564,6 +568,16 @@ where
}
}

/// Prevent shutdown of the underlying IO object at the end of service the request,
/// instead run `into_parts`. This is a convenience wrapper over `poll_without_shutdown`.
pub fn without_shutdown(self) -> impl Future<Item=Parts<I,S>, Error=::Error> {
let mut conn = Some(self);
::futures::future::poll_fn(move || -> ::Result<_> {
try_ready!(conn.as_mut().unwrap().poll_without_shutdown());
Ok(conn.take().unwrap().into_parts().into())
})
}

fn upgrade_h2(&mut self) {
trace!("Trying to upgrade connection to h2");
let conn = self.conn.take();
Expand Down

0 comments on commit edf551b

Please sign in to comment.