Skip to content

Commit

Permalink
muxers/yamux: Refactor Yamux::close to use ? (#2677)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaseizinger committed May 31, 2022
1 parent 6e1e314 commit 3611d69
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions muxers/yamux/src/lib.rs
Expand Up @@ -31,7 +31,7 @@ use libp2p_core::muxing::{StreamMuxer, StreamMuxerEvent};
use libp2p_core::upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo};
use parking_lot::Mutex;
use std::{
fmt, io, iter,
fmt, io, iter, mem,
pin::Pin,
task::{Context, Poll},
};
Expand Down Expand Up @@ -179,16 +179,21 @@ where

fn poll_close(&self, c: &mut Context<'_>) -> Poll<YamuxResult<()>> {
let mut inner = self.0.lock();
if let std::task::Poll::Ready(x) = Pin::new(&mut inner.control).poll_close(c) {
return Poll::Ready(x.map_err(YamuxError));

if let Poll::Ready(()) = Pin::new(&mut inner.control)
.poll_close(c)
.map_err(YamuxError)?
{
return Poll::Ready(Ok(()));
}
while let std::task::Poll::Ready(x) = inner.incoming.poll_next_unpin(c) {
match x {
Some(Ok(_)) => {} // drop inbound stream
Some(Err(e)) => return Poll::Ready(Err(e)),

while let Poll::Ready(maybe_inbound_stream) = inner.incoming.poll_next_unpin(c)? {
match maybe_inbound_stream {
Some(inbound_stream) => mem::drop(inbound_stream),
None => return Poll::Ready(Ok(())),
}
}

Poll::Pending
}
}
Expand Down

0 comments on commit 3611d69

Please sign in to comment.