Skip to content

Commit

Permalink
fix(swarm): implement ConnectionHandler::poll_close for combinators
Browse files Browse the repository at this point in the history
Follow-up to libp2p#4076.

This is especially relevant since `libp2p-swarm-derive` uses `SelectConnectionHandler`.
  • Loading branch information
mxinden committed Nov 2, 2023
1 parent e2e9179 commit 1f74f96
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
8 changes: 8 additions & 0 deletions swarm/src/behaviour/toggle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,4 +369,12 @@ where
}
}
}

fn poll_close(&mut self, cx: &mut Context<'_>) -> Poll<Option<Self::ToBehaviour>> {
if let Some(inner) = self.inner.as_mut() {
inner.poll_close(cx)
} else {
Poll::Ready(None)
}
}
}
14 changes: 14 additions & 0 deletions swarm/src/handler/either.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,20 @@ where
Poll::Ready(event)
}

fn poll_close(
&mut self,
cx: &mut Context<'_>,
) -> Poll<Option<Self::ToBehaviour>> {
let event = match self {
Either::Left(handler) => futures::ready!(handler.poll_close(cx))
.map(Either::Left),
Either::Right(handler) => futures::ready!(handler.poll_close(cx))
.map(Either::Right),
};

Poll::Ready(event)
}

fn on_connection_event(
&mut self,
event: ConnectionEvent<
Expand Down
7 changes: 7 additions & 0 deletions swarm/src/handler/map_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ where
self.inner.poll(cx)
}

fn poll_close(
&mut self,
cx: &mut Context<'_>,
) -> Poll<Option<Self::ToBehaviour>> {
self.inner.poll_close(cx)
}

fn on_connection_event(
&mut self,
event: ConnectionEvent<
Expand Down
6 changes: 6 additions & 0 deletions swarm/src/handler/map_out.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ where
})
}

fn poll_close(&mut self, cx: &mut Context<'_>) -> Poll<Option<Self::ToBehaviour>> {
self.inner
.poll_close(cx)
.map(|ev| ev.map(|ev| (self.map)(ev)))
}

fn on_connection_event(
&mut self,
event: ConnectionEvent<
Expand Down

0 comments on commit 1f74f96

Please sign in to comment.