-
Notifications
You must be signed in to change notification settings - Fork 999
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
swarm: change Stream to return all SwarmEvents #2100
Conversation
Change Stream implementation for ExpandedSwarm to return all SwarmEvents instead of only Behaviour events. Remove redundant next_event() method. Rename next() method to behaviour_next(), adjust examples.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, rust-libp2p
doesn't use rustfmt, see #1966.
Do you mind removing the formatting diffs? Makes it quite hard to review.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @elenaf9 for tackling this!
When using libraries that offer async
methods only, I find it difficult to judge whether the Future
s returned by these async
methods are cancellation correct (i.e. state lesss or safe to poll once and then drop on Poll::Pending
). A library can make this explicit by offering both a poll
style and async
method. Long story short, what do you think of offering both poll_next_behaviour_event
and next_behaviour_event
along the lines of:
/// Convenience function only returning events produced by the [`NetworkBehaviour`].
///
/// For `async` version see [`ExpandedSwarm::next_behaviour_event`].
///
/// For all events via [`SwarmEvent`] see [`Stream`] implementation on [`ExpandedSwarm`].
fn poll_next_behaviour_event(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<TBehaviour::OutEvent> {
loop {
let event = futures::ready!(ExpandedSwarm::poll_next_event(Pin::new(self), cx));
if let SwarmEvent::Behaviour(event) = event {
return Poll::Ready(event);
}
}
}
/// Convenience function only returning events produced by the [`NetworkBehaviour`].
///
/// For `poll` version see [`ExpandedSwarm::poll_next_behaviour_event`].
///
/// For all events via [`SwarmEvent`] see [`Stream`] implementation on [`ExpandedSwarm`].
pub async fn next_behaviour_event(&mut self) -> TBehaviour::OutEvent {
future::poll_fn(self.poll_next_behaviour_event)
}
I agree that generally the |
I have no need for That said, in case people do need either On a related note, by removing the original I think the loss in type safety by removing Everyone, please speak up in case you think differently. |
I share the same view and would argue that the provided convenience is almost negligible given that in any application, there will hopefully only be a single place where the swarm is being polled. It is not like
That is a good point but same as above, I would argue that the convenience hit is minimal given that it probably only occurs within a single place in any application. One could even argue that providing only a single way of doing things is better. For example, by committing fully to the |
I completely agree with this, but maybe we should add to the
Please feel free to suggest some rephrasing or different terminology.
Considering your arguments, I agree that both |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great. Thanks for the follow up work!
Would you mind including the diff below, adding a comment to ExpandedSwarm
and a changelog entry?
Other than that, this is ready to merge from my side.
diff --git a/swarm/CHANGELOG.md b/swarm/CHANGELOG.md
index 422220fe..29019da5 100644
--- a/swarm/CHANGELOG.md
+++ b/swarm/CHANGELOG.md
@@ -2,6 +2,21 @@
- Update dependencies.
+- Drive `ExpandedSwarm` via `Stream` trait only.
+
+ - Change `Stream` implementation of `ExpandedSwarm` to return all
+ `SwarmEvents` instead of only the `NetworkBehaviour`'s events.
+
+ - Remove `ExpandedSwarm::next_event`. Users can use `<ExpandedSwarm as
+ StreamExt>::next` instead.
+
+ - Remove `ExpandedSwarm::next`. Users can use `<ExpandedSwarm as
+ StreamExt>::filter_map` instead.
+
+ See [PR 2100] for details.
+
+[PR 2100]: https://github.com/libp2p/rust-libp2p/pull/2100
+
# 0.29.0 [2021-04-13]
- Remove `Deref` and `DerefMut` implementations previously dereferencing to the
diff --git a/swarm/src/lib.rs b/swarm/src/lib.rs
index 28ea83ee..af0936bd 100644
--- a/swarm/src/lib.rs
+++ b/swarm/src/lib.rs
@@ -256,6 +256,9 @@ pub enum SwarmEvent<TBvEv, THandleErr> {
}
/// Contains the state of the network, plus the way it should behave.
+///
+/// Note: Needs to be polled via [`<ExpandedSwarm as Stream>`] in order to make
+/// progress.
pub struct ExpandedSwarm<TBehaviour, TInEvent, TOutEvent, THandler>
where
THandler: IntoProtocolsHandler,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @elenaf9! I will merge later today.
PR for the suggested changes from @mxinden in #1876.
Change Stream implementation for
ExpandedSwarm
to return allSwarmEvents
instead of only the network behaviour 's events.Remove redundant next_event() method.
Rename next() method to behaviour_next() (maybe find a better name for it?). I'd suggest to keep this method for awaiting the next network behaviour event, because it is more convenient than using StreamExt::filter()/StreamExt::filter_map().
Adjust examples to the change.