Refactor similar conditional branches in `WebSocketStream::poll_close`

pull/104/head
Benoît CORTIER 3 years ago committed by Sebastian Dröge
parent 01282a029f
commit 72fb249d8f
  1. 42
      src/lib.rs

@ -357,34 +357,24 @@ where
} }
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
if self.closing { let res = if self.closing {
// After queueing it, we call `write_pending` to drive the close handshake to completion. // After queueing it, we call `write_pending` to drive the close handshake to completion.
match (*self).with_context(Some((ContextWaker::Write, cx)), |s| s.write_pending()) { (*self).with_context(Some((ContextWaker::Write, cx)), |s| s.write_pending())
Ok(()) => Poll::Ready(Ok(())),
Err(::tungstenite::Error::ConnectionClosed) => Poll::Ready(Ok(())),
Err(::tungstenite::Error::Io(err))
if err.kind() == std::io::ErrorKind::WouldBlock =>
{
trace!("WouldBlock");
Poll::Pending
}
Err(err) => Poll::Ready(Err(err)),
}
} else { } else {
match (*self).with_context(Some((ContextWaker::Write, cx)), |s| s.close(None)) { (*self).with_context(Some((ContextWaker::Write, cx)), |s| s.close(None))
Ok(()) => Poll::Ready(Ok(())), };
Err(::tungstenite::Error::ConnectionClosed) => Poll::Ready(Ok(())),
Err(::tungstenite::Error::Io(err)) match res {
if err.kind() == std::io::ErrorKind::WouldBlock => Ok(()) => Poll::Ready(Ok(())),
{ Err(::tungstenite::Error::ConnectionClosed) => Poll::Ready(Ok(())),
trace!("WouldBlock"); Err(::tungstenite::Error::Io(err)) if err.kind() == std::io::ErrorKind::WouldBlock => {
self.closing = true; trace!("WouldBlock");
Poll::Pending self.closing = true;
} Poll::Pending
Err(err) => { }
debug!("websocket close error: {}", err); Err(err) => {
Poll::Ready(Err(err)) debug!("websocket close error: {}", err);
} Poll::Ready(Err(err))
} }
} }
} }

Loading…
Cancel
Save