diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8802871dc9d..3ccaa53f381 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -70,7 +70,7 @@ jobs: echo "Package version: $PACKAGE_VERSION"; echo "Specified version: $SPECIFIED_VERSION"; - test "$PACKAGE_VERSION" = "$SPECIFIED_VERSION" + test "$PACKAGE_VERSION" = "$SPECIFIED_VERSION" || test "=$PACKAGE_VERSION" = "$SPECIFIED_VERSION" - name: Ensure manifest and CHANGELOG are properly updated if: > diff --git a/Cargo.toml b/Cargo.toml index 4be55edb103..d676d69a57a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -99,7 +99,7 @@ libp2p-rendezvous = { version = "0.14.0", path = "protocols/rendezvous" } libp2p-request-response = { version = "0.26.0", path = "protocols/request-response" } libp2p-server = { version = "0.12.3", path = "misc/server" } libp2p-swarm = { version = "0.44.0", path = "swarm" } -libp2p-swarm-derive = { version = "0.34.0", path = "swarm-derive" } +libp2p-swarm-derive = { version = "=0.34.0", path = "swarm-derive" } # `libp2p-swarm-derive` may not be compatible with different `libp2p-swarm` non-breaking releases. E.g. `libp2p-swarm` might introduce a new enum variant `FromSwarm` (which is `#[non-exhaustive]`) in a non-breaking release. Older versions of `libp2p-swarm-derive` would not forward this enum variant within the `NetworkBehaviour` hierarchy. Thus the version pinning is required. libp2p-swarm-test = { version = "0.3.0", path = "swarm-test" } libp2p-tcp = { version = "0.41.0", path = "transports/tcp" } libp2p-tls = { version = "0.3.0", path = "transports/tls" } diff --git a/misc/allow-block-list/src/lib.rs b/misc/allow-block-list/src/lib.rs index 9f2524733e6..c1d31433db1 100644 --- a/misc/allow-block-list/src/lib.rs +++ b/misc/allow-block-list/src/lib.rs @@ -231,23 +231,7 @@ where Ok(dummy::ConnectionHandler) } - fn on_swarm_event(&mut self, event: FromSwarm) { - match event { - FromSwarm::ConnectionClosed(_) => {} - FromSwarm::ConnectionEstablished(_) => {} - FromSwarm::AddressChange(_) => {} - FromSwarm::DialFailure(_) => {} - FromSwarm::ListenFailure(_) => {} - FromSwarm::NewListener(_) => {} - FromSwarm::NewListenAddr(_) => {} - FromSwarm::ExpiredListenAddr(_) => {} - FromSwarm::ListenerError(_) => {} - FromSwarm::ListenerClosed(_) => {} - FromSwarm::NewExternalAddrCandidate(_) => {} - FromSwarm::ExternalAddrExpired(_) => {} - FromSwarm::ExternalAddrConfirmed(_) => {} - } - } + fn on_swarm_event(&mut self, _event: FromSwarm) {} fn on_connection_handler_event( &mut self, diff --git a/misc/connection-limits/src/lib.rs b/misc/connection-limits/src/lib.rs index d0ea3436177..af76e9a57d9 100644 --- a/misc/connection-limits/src/lib.rs +++ b/misc/connection-limits/src/lib.rs @@ -340,18 +340,10 @@ impl NetworkBehaviour for Behaviour { FromSwarm::DialFailure(DialFailure { connection_id, .. }) => { self.pending_outbound_connections.remove(&connection_id); } - FromSwarm::AddressChange(_) => {} FromSwarm::ListenFailure(ListenFailure { connection_id, .. }) => { self.pending_inbound_connections.remove(&connection_id); } - FromSwarm::NewListener(_) => {} - FromSwarm::NewListenAddr(_) => {} - FromSwarm::ExpiredListenAddr(_) => {} - FromSwarm::ListenerError(_) => {} - FromSwarm::ListenerClosed(_) => {} - FromSwarm::NewExternalAddrCandidate(_) => {} - FromSwarm::ExternalAddrExpired(_) => {} - FromSwarm::ExternalAddrConfirmed(_) => {} + _ => {} } } diff --git a/misc/metrics/src/swarm.rs b/misc/metrics/src/swarm.rs index 20d3ce2eff3..fff28e5f639 100644 --- a/misc/metrics/src/swarm.rs +++ b/misc/metrics/src/swarm.rs @@ -23,7 +23,7 @@ use std::sync::{Arc, Mutex}; use crate::protocol_stack; use instant::Instant; -use libp2p_swarm::{ConnectionId, SwarmEvent}; +use libp2p_swarm::{ConnectionId, DialError, SwarmEvent}; use prometheus_client::encoding::{EncodeLabelSet, EncodeLabelValue}; use prometheus_client::metrics::counter::Counter; use prometheus_client::metrics::family::Family; @@ -185,11 +185,11 @@ impl Metrics { } } -impl super::Recorder> for Metrics { - fn record(&self, event: &libp2p_swarm::SwarmEvent) { +impl super::Recorder> for Metrics { + fn record(&self, event: &SwarmEvent) { match event { - libp2p_swarm::SwarmEvent::Behaviour(_) => {} - libp2p_swarm::SwarmEvent::ConnectionEstablished { + SwarmEvent::Behaviour(_) => {} + SwarmEvent::ConnectionEstablished { endpoint, established_in: time_taken, connection_id, @@ -208,7 +208,7 @@ impl super::Recorder super::Recorder { + SwarmEvent::IncomingConnection { send_back_addr, .. } => { self.connections_incoming .get_or_create(&AddressLabels { protocols: protocol_stack::as_string(send_back_addr), }) .inc(); } - libp2p_swarm::SwarmEvent::IncomingConnectionError { + SwarmEvent::IncomingConnectionError { error, send_back_addr, .. @@ -250,7 +250,7 @@ impl super::Recorder { + SwarmEvent::OutgoingConnectionError { error, peer_id, .. } => { let peer = match peer_id { Some(_) => PeerStatus::Known, None => PeerStatus::Unknown, @@ -263,7 +263,7 @@ impl super::Recorder { + DialError::Transport(errors) => { for (_multiaddr, error) in errors { match error { libp2p_core::transport::TransportError::MultiaddrNotSupported( @@ -277,39 +277,31 @@ impl super::Recorder { - record(OutgoingConnectionError::LocalPeerId) - } - libp2p_swarm::DialError::NoAddresses => { - record(OutgoingConnectionError::NoAddresses) - } - libp2p_swarm::DialError::DialPeerConditionFalse(_) => { + DialError::LocalPeerId { .. } => record(OutgoingConnectionError::LocalPeerId), + DialError::NoAddresses => record(OutgoingConnectionError::NoAddresses), + DialError::DialPeerConditionFalse(_) => { record(OutgoingConnectionError::DialPeerConditionFalse) } - libp2p_swarm::DialError::Aborted => record(OutgoingConnectionError::Aborted), - libp2p_swarm::DialError::WrongPeerId { .. } => { - record(OutgoingConnectionError::WrongPeerId) - } - libp2p_swarm::DialError::Denied { .. } => { - record(OutgoingConnectionError::Denied) - } + DialError::Aborted => record(OutgoingConnectionError::Aborted), + DialError::WrongPeerId { .. } => record(OutgoingConnectionError::WrongPeerId), + DialError::Denied { .. } => record(OutgoingConnectionError::Denied), }; } - libp2p_swarm::SwarmEvent::NewListenAddr { address, .. } => { + SwarmEvent::NewListenAddr { address, .. } => { self.new_listen_addr .get_or_create(&AddressLabels { protocols: protocol_stack::as_string(address), }) .inc(); } - libp2p_swarm::SwarmEvent::ExpiredListenAddr { address, .. } => { + SwarmEvent::ExpiredListenAddr { address, .. } => { self.expired_listen_addr .get_or_create(&AddressLabels { protocols: protocol_stack::as_string(address), }) .inc(); } - libp2p_swarm::SwarmEvent::ListenerClosed { addresses, .. } => { + SwarmEvent::ListenerClosed { addresses, .. } => { for address in addresses { self.listener_closed .get_or_create(&AddressLabels { @@ -318,10 +310,10 @@ impl super::Recorder { + SwarmEvent::ListenerError { .. } => { self.listener_error.inc(); } - libp2p_swarm::SwarmEvent::Dialing { .. } => { + SwarmEvent::Dialing { .. } => { self.dial_attempt.inc(); } SwarmEvent::NewExternalAddrCandidate { address } => { @@ -345,6 +337,7 @@ impl super::Recorder {} } } } diff --git a/protocols/autonat/src/behaviour.rs b/protocols/autonat/src/behaviour.rs index 06c945eb888..27f5474c924 100644 --- a/protocols/autonat/src/behaviour.rs +++ b/protocols/autonat/src/behaviour.rs @@ -593,6 +593,7 @@ impl NetworkBehaviour for Behaviour { self.inner.on_swarm_event(listener_closed) } confirmed @ FromSwarm::ExternalAddrConfirmed(_) => self.inner.on_swarm_event(confirmed), + _ => {} } } diff --git a/protocols/dcutr/src/behaviour.rs b/protocols/dcutr/src/behaviour.rs index d0b46abb0b4..b644d90b6f2 100644 --- a/protocols/dcutr/src/behaviour.rs +++ b/protocols/dcutr/src/behaviour.rs @@ -339,16 +339,7 @@ impl NetworkBehaviour for Behaviour { FromSwarm::NewExternalAddrCandidate(NewExternalAddrCandidate { addr }) => { self.address_candidates.add(addr.clone()); } - FromSwarm::AddressChange(_) - | FromSwarm::ConnectionEstablished(_) - | FromSwarm::ListenFailure(_) - | FromSwarm::NewListener(_) - | FromSwarm::NewListenAddr(_) - | FromSwarm::ExpiredListenAddr(_) - | FromSwarm::ListenerError(_) - | FromSwarm::ListenerClosed(_) - | FromSwarm::ExternalAddrExpired(_) - | FromSwarm::ExternalAddrConfirmed(_) => {} + _ => {} } } } diff --git a/protocols/dcutr/src/handler/relayed.rs b/protocols/dcutr/src/handler/relayed.rs index b4daefce15f..4d9bf3c910d 100644 --- a/protocols/dcutr/src/handler/relayed.rs +++ b/protocols/dcutr/src/handler/relayed.rs @@ -312,9 +312,7 @@ impl ConnectionHandler for Handler { ConnectionEvent::DialUpgradeError(dial_upgrade_error) => { self.on_dial_upgrade_error(dial_upgrade_error) } - ConnectionEvent::AddressChange(_) - | ConnectionEvent::LocalProtocolsChange(_) - | ConnectionEvent::RemoteProtocolsChange(_) => {} + _ => {} } } } diff --git a/protocols/floodsub/src/layer.rs b/protocols/floodsub/src/layer.rs index 7fa9f3001b1..cfd10024ca3 100644 --- a/protocols/floodsub/src/layer.rs +++ b/protocols/floodsub/src/layer.rs @@ -490,17 +490,7 @@ impl NetworkBehaviour for Floodsub { FromSwarm::ConnectionClosed(connection_closed) => { self.on_connection_closed(connection_closed) } - FromSwarm::AddressChange(_) - | FromSwarm::DialFailure(_) - | FromSwarm::ListenFailure(_) - | FromSwarm::NewListener(_) - | FromSwarm::NewListenAddr(_) - | FromSwarm::ExpiredListenAddr(_) - | FromSwarm::ListenerError(_) - | FromSwarm::ListenerClosed(_) - | FromSwarm::NewExternalAddrCandidate(_) - | FromSwarm::ExternalAddrExpired(_) - | FromSwarm::ExternalAddrConfirmed(_) => {} + _ => {} } } } diff --git a/protocols/gossipsub/src/behaviour.rs b/protocols/gossipsub/src/behaviour.rs index f1069658b73..4ad0784f807 100644 --- a/protocols/gossipsub/src/behaviour.rs +++ b/protocols/gossipsub/src/behaviour.rs @@ -3458,16 +3458,7 @@ where self.on_connection_closed(connection_closed) } FromSwarm::AddressChange(address_change) => self.on_address_change(address_change), - FromSwarm::DialFailure(_) - | FromSwarm::ListenFailure(_) - | FromSwarm::NewListener(_) - | FromSwarm::NewListenAddr(_) - | FromSwarm::ExpiredListenAddr(_) - | FromSwarm::ListenerError(_) - | FromSwarm::ListenerClosed(_) - | FromSwarm::NewExternalAddrCandidate(_) - | FromSwarm::ExternalAddrExpired(_) - | FromSwarm::ExternalAddrConfirmed(_) => {} + _ => {} } } } diff --git a/protocols/gossipsub/src/handler.rs b/protocols/gossipsub/src/handler.rs index 4f3dd5c9f63..55480384ffa 100644 --- a/protocols/gossipsub/src/handler.rs +++ b/protocols/gossipsub/src/handler.rs @@ -533,10 +533,7 @@ impl ConnectionHandler for Handler { }) => { tracing::debug!("Protocol negotiation failed: {e}") } - ConnectionEvent::AddressChange(_) - | ConnectionEvent::ListenUpgradeError(_) - | ConnectionEvent::LocalProtocolsChange(_) - | ConnectionEvent::RemoteProtocolsChange(_) => {} + _ => {} } } Handler::Disabled(_) => {} diff --git a/protocols/identify/src/behaviour.rs b/protocols/identify/src/behaviour.rs index 75ddfc812bf..25e2b14bdcd 100644 --- a/protocols/identify/src/behaviour.rs +++ b/protocols/identify/src/behaviour.rs @@ -395,16 +395,7 @@ impl NetworkBehaviour for Behaviour { } } } - FromSwarm::NewListenAddr(_) - | FromSwarm::ExpiredListenAddr(_) - | FromSwarm::AddressChange(_) - | FromSwarm::ListenFailure(_) - | FromSwarm::NewListener(_) - | FromSwarm::ListenerError(_) - | FromSwarm::ListenerClosed(_) - | FromSwarm::NewExternalAddrCandidate(_) - | FromSwarm::ExternalAddrExpired(_) => {} - FromSwarm::ExternalAddrConfirmed(_) => {} + _ => {} } } } diff --git a/protocols/identify/src/handler.rs b/protocols/identify/src/handler.rs index 963397e2274..5012868c17a 100644 --- a/protocols/identify/src/handler.rs +++ b/protocols/identify/src/handler.rs @@ -407,9 +407,6 @@ impl ConnectionHandler for Handler { )); self.trigger_next_identify.reset(self.interval); } - ConnectionEvent::AddressChange(_) - | ConnectionEvent::ListenUpgradeError(_) - | ConnectionEvent::RemoteProtocolsChange(_) => {} ConnectionEvent::LocalProtocolsChange(change) => { let before = tracing::enabled!(Level::DEBUG) .then(|| self.local_protocols_to_string()) @@ -436,6 +433,7 @@ impl ConnectionHandler for Handler { }); } } + _ => {} } } } diff --git a/protocols/kad/src/behaviour.rs b/protocols/kad/src/behaviour.rs index cc80b9c1be9..fc942cf635a 100644 --- a/protocols/kad/src/behaviour.rs +++ b/protocols/kad/src/behaviour.rs @@ -2554,15 +2554,7 @@ where } FromSwarm::DialFailure(dial_failure) => self.on_dial_failure(dial_failure), FromSwarm::AddressChange(address_change) => self.on_address_change(address_change), - FromSwarm::ExpiredListenAddr(_) - | FromSwarm::NewExternalAddrCandidate(_) - | FromSwarm::NewListenAddr(_) - | FromSwarm::ListenFailure(_) - | FromSwarm::NewListener(_) - | FromSwarm::ListenerClosed(_) - | FromSwarm::ListenerError(_) - | FromSwarm::ExternalAddrExpired(_) - | FromSwarm::ExternalAddrConfirmed(_) => {} + _ => {} } } } diff --git a/protocols/kad/src/handler.rs b/protocols/kad/src/handler.rs index 0f36800a904..21dad8a82b9 100644 --- a/protocols/kad/src/handler.rs +++ b/protocols/kad/src/handler.rs @@ -777,9 +777,6 @@ impl ConnectionHandler for Handler { ConnectionEvent::DialUpgradeError(dial_upgrade_error) => { self.on_dial_upgrade_error(dial_upgrade_error) } - ConnectionEvent::AddressChange(_) - | ConnectionEvent::ListenUpgradeError(_) - | ConnectionEvent::LocalProtocolsChange(_) => {} ConnectionEvent::RemoteProtocolsChange(change) => { let dirty = self.remote_supported_protocols.on_protocols_change(change); @@ -795,6 +792,7 @@ impl ConnectionHandler for Handler { )) } } + _ => {} } } } diff --git a/protocols/perf/src/client/behaviour.rs b/protocols/perf/src/client/behaviour.rs index 79c73d55102..216c10be9c0 100644 --- a/protocols/perf/src/client/behaviour.rs +++ b/protocols/perf/src/client/behaviour.rs @@ -121,17 +121,7 @@ impl NetworkBehaviour for Behaviour { assert!(self.connected.remove(&peer_id)); } } - FromSwarm::AddressChange(_) - | FromSwarm::DialFailure(_) - | FromSwarm::ListenFailure(_) - | FromSwarm::NewListener(_) - | FromSwarm::NewListenAddr(_) - | FromSwarm::ExpiredListenAddr(_) - | FromSwarm::ListenerError(_) - | FromSwarm::ListenerClosed(_) - | FromSwarm::NewExternalAddrCandidate(_) - | FromSwarm::ExternalAddrExpired(_) - | FromSwarm::ExternalAddrConfirmed(_) => {} + _ => {} } } diff --git a/protocols/perf/src/client/handler.rs b/protocols/perf/src/client/handler.rs index d5d05284a85..0cc2dd23abe 100644 --- a/protocols/perf/src/client/handler.rs +++ b/protocols/perf/src/client/handler.rs @@ -150,6 +150,7 @@ impl ConnectionHandler for Handler { ConnectionEvent::ListenUpgradeError(ListenUpgradeError { info: (), error }) => { void::unreachable(error) } + _ => {} } } diff --git a/protocols/perf/src/server/behaviour.rs b/protocols/perf/src/server/behaviour.rs index 370bc2ae188..8f5ac93e1e0 100644 --- a/protocols/perf/src/server/behaviour.rs +++ b/protocols/perf/src/server/behaviour.rs @@ -75,23 +75,7 @@ impl NetworkBehaviour for Behaviour { Ok(Handler::default()) } - fn on_swarm_event(&mut self, event: FromSwarm) { - match event { - FromSwarm::ConnectionEstablished(_) => {} - FromSwarm::ConnectionClosed(_) => {} - FromSwarm::AddressChange(_) => {} - FromSwarm::DialFailure(_) => {} - FromSwarm::ListenFailure(_) => {} - FromSwarm::NewListener(_) => {} - FromSwarm::NewListenAddr(_) => {} - FromSwarm::ExpiredListenAddr(_) => {} - FromSwarm::ListenerError(_) => {} - FromSwarm::ListenerClosed(_) => {} - FromSwarm::NewExternalAddrCandidate(_) => {} - FromSwarm::ExternalAddrExpired(_) => {} - FromSwarm::ExternalAddrConfirmed(_) => {} - } - } + fn on_swarm_event(&mut self, _event: FromSwarm) {} fn on_connection_handler_event( &mut self, diff --git a/protocols/perf/src/server/handler.rs b/protocols/perf/src/server/handler.rs index 7f262ac4820..ed42162cb7e 100644 --- a/protocols/perf/src/server/handler.rs +++ b/protocols/perf/src/server/handler.rs @@ -112,6 +112,7 @@ impl ConnectionHandler for Handler { ConnectionEvent::ListenUpgradeError(ListenUpgradeError { info: (), error }) => { void::unreachable(error) } + _ => {} } } diff --git a/protocols/ping/src/handler.rs b/protocols/ping/src/handler.rs index 71ebcd97261..3ee6bfdf5d6 100644 --- a/protocols/ping/src/handler.rs +++ b/protocols/ping/src/handler.rs @@ -359,10 +359,7 @@ impl ConnectionHandler for Handler { ConnectionEvent::DialUpgradeError(dial_upgrade_error) => { self.on_dial_upgrade_error(dial_upgrade_error) } - ConnectionEvent::AddressChange(_) - | ConnectionEvent::ListenUpgradeError(_) - | ConnectionEvent::LocalProtocolsChange(_) - | ConnectionEvent::RemoteProtocolsChange(_) => {} + _ => {} } } } diff --git a/protocols/ping/src/lib.rs b/protocols/ping/src/lib.rs index 3e17db300e7..5eaa6d4952a 100644 --- a/protocols/ping/src/lib.rs +++ b/protocols/ping/src/lib.rs @@ -150,21 +150,5 @@ impl NetworkBehaviour for Behaviour { } } - fn on_swarm_event(&mut self, event: FromSwarm) { - match event { - FromSwarm::ConnectionEstablished(_) - | FromSwarm::ConnectionClosed(_) - | FromSwarm::AddressChange(_) - | FromSwarm::DialFailure(_) - | FromSwarm::ListenFailure(_) - | FromSwarm::NewListener(_) - | FromSwarm::NewListenAddr(_) - | FromSwarm::ExpiredListenAddr(_) - | FromSwarm::ListenerError(_) - | FromSwarm::ListenerClosed(_) - | FromSwarm::NewExternalAddrCandidate(_) - | FromSwarm::ExternalAddrExpired(_) - | FromSwarm::ExternalAddrConfirmed(_) => {} - } - } + fn on_swarm_event(&mut self, _event: FromSwarm) {} } diff --git a/protocols/relay/src/behaviour.rs b/protocols/relay/src/behaviour.rs index 98e2a5a53bb..2c2870f5618 100644 --- a/protocols/relay/src/behaviour.rs +++ b/protocols/relay/src/behaviour.rs @@ -350,22 +350,8 @@ impl NetworkBehaviour for Behaviour { fn on_swarm_event(&mut self, event: FromSwarm) { self.external_addresses.on_swarm_event(&event); - match event { - FromSwarm::ConnectionClosed(connection_closed) => { - self.on_connection_closed(connection_closed) - } - FromSwarm::ConnectionEstablished(_) - | FromSwarm::DialFailure(_) - | FromSwarm::AddressChange(_) - | FromSwarm::ListenFailure(_) - | FromSwarm::NewListener(_) - | FromSwarm::NewListenAddr(_) - | FromSwarm::ExpiredListenAddr(_) - | FromSwarm::ListenerError(_) - | FromSwarm::ListenerClosed(_) - | FromSwarm::NewExternalAddrCandidate(_) - | FromSwarm::ExternalAddrExpired(_) - | FromSwarm::ExternalAddrConfirmed(_) => {} + if let FromSwarm::ConnectionClosed(connection_closed) = event { + self.on_connection_closed(connection_closed) } } diff --git a/protocols/relay/src/behaviour/handler.rs b/protocols/relay/src/behaviour/handler.rs index 361fb8ac333..4e729b1993e 100644 --- a/protocols/relay/src/behaviour/handler.rs +++ b/protocols/relay/src/behaviour/handler.rs @@ -898,10 +898,7 @@ impl ConnectionHandler for Handler { ConnectionEvent::DialUpgradeError(dial_upgrade_error) => { self.on_dial_upgrade_error(dial_upgrade_error); } - ConnectionEvent::AddressChange(_) - | ConnectionEvent::ListenUpgradeError(_) - | ConnectionEvent::LocalProtocolsChange(_) - | ConnectionEvent::RemoteProtocolsChange(_) => {} + _ => {} } } } diff --git a/protocols/relay/src/priv_client.rs b/protocols/relay/src/priv_client.rs index ae2ceb2e97d..53d75364e9d 100644 --- a/protocols/relay/src/priv_client.rs +++ b/protocols/relay/src/priv_client.rs @@ -202,16 +202,7 @@ impl NetworkBehaviour for Behaviour { FromSwarm::DialFailure(DialFailure { connection_id, .. }) => { self.pending_handler_commands.remove(&connection_id); } - FromSwarm::AddressChange(_) - | FromSwarm::ListenFailure(_) - | FromSwarm::NewListener(_) - | FromSwarm::NewListenAddr(_) - | FromSwarm::ExpiredListenAddr(_) - | FromSwarm::ListenerError(_) - | FromSwarm::ListenerClosed(_) - | FromSwarm::NewExternalAddrCandidate(_) - | FromSwarm::ExternalAddrExpired(_) - | FromSwarm::ExternalAddrConfirmed(_) => {} + _ => {} } } diff --git a/protocols/relay/src/priv_client/handler.rs b/protocols/relay/src/priv_client/handler.rs index d884f15c7eb..f30f24a949b 100644 --- a/protocols/relay/src/priv_client/handler.rs +++ b/protocols/relay/src/priv_client/handler.rs @@ -537,9 +537,7 @@ impl ConnectionHandler for Handler { ConnectionEvent::DialUpgradeError(dial_upgrade_error) => { self.on_dial_upgrade_error(dial_upgrade_error) } - ConnectionEvent::AddressChange(_) - | ConnectionEvent::LocalProtocolsChange(_) - | ConnectionEvent::RemoteProtocolsChange(_) => {} + _ => {} } } } diff --git a/protocols/rendezvous/src/client.rs b/protocols/rendezvous/src/client.rs index c6072533194..beef7496274 100644 --- a/protocols/rendezvous/src/client.rs +++ b/protocols/rendezvous/src/client.rs @@ -280,16 +280,7 @@ impl NetworkBehaviour for Behaviour { )) => { unreachable!("rendezvous clients never receive requests") } - Poll::Ready( - other @ (ToSwarm::ExternalAddrConfirmed(_) - | ToSwarm::ExternalAddrExpired(_) - | ToSwarm::NewExternalAddrCandidate(_) - | ToSwarm::NotifyHandler { .. } - | ToSwarm::Dial { .. } - | ToSwarm::CloseConnection { .. } - | ToSwarm::ListenOn { .. } - | ToSwarm::RemoveListener { .. }), - ) => { + Poll::Ready(other) => { let new_to_swarm = other.map_out(|_| unreachable!("we manually map `GenerateEvent` variants")); diff --git a/protocols/rendezvous/src/server.rs b/protocols/rendezvous/src/server.rs index 886b64cc829..e1e03f41375 100644 --- a/protocols/rendezvous/src/server.rs +++ b/protocols/rendezvous/src/server.rs @@ -227,6 +227,7 @@ impl NetworkBehaviour for Behaviour { return Poll::Ready(new_to_swarm); } + _ => {} }; } diff --git a/protocols/request-response/src/handler.rs b/protocols/request-response/src/handler.rs index ef4b5b44fe0..9ccbc49fc4b 100644 --- a/protocols/request-response/src/handler.rs +++ b/protocols/request-response/src/handler.rs @@ -484,9 +484,7 @@ where ConnectionEvent::ListenUpgradeError(listen_upgrade_error) => { self.on_listen_upgrade_error(listen_upgrade_error) } - ConnectionEvent::AddressChange(_) - | ConnectionEvent::LocalProtocolsChange(_) - | ConnectionEvent::RemoteProtocolsChange(_) => {} + _ => {} } } } diff --git a/protocols/request-response/src/lib.rs b/protocols/request-response/src/lib.rs index 68a6b689fe5..9737663a876 100644 --- a/protocols/request-response/src/lib.rs +++ b/protocols/request-response/src/lib.rs @@ -804,15 +804,7 @@ where } FromSwarm::AddressChange(address_change) => self.on_address_change(address_change), FromSwarm::DialFailure(dial_failure) => self.on_dial_failure(dial_failure), - FromSwarm::ListenFailure(_) => {} - FromSwarm::NewListener(_) => {} - FromSwarm::NewListenAddr(_) => {} - FromSwarm::ExpiredListenAddr(_) => {} - FromSwarm::ListenerError(_) => {} - FromSwarm::ListenerClosed(_) => {} - FromSwarm::NewExternalAddrCandidate(_) => {} - FromSwarm::ExternalAddrExpired(_) => {} - FromSwarm::ExternalAddrConfirmed(_) => {} + _ => {} } } diff --git a/protocols/upnp/src/behaviour.rs b/protocols/upnp/src/behaviour.rs index 5410b8dd13f..648b28e1c82 100644 --- a/protocols/upnp/src/behaviour.rs +++ b/protocols/upnp/src/behaviour.rs @@ -351,17 +351,7 @@ impl NetworkBehaviour for Behaviour { } } } - FromSwarm::ConnectionEstablished(_) - | FromSwarm::ConnectionClosed(_) - | FromSwarm::AddressChange(_) - | FromSwarm::DialFailure(_) - | FromSwarm::ListenFailure(_) - | FromSwarm::NewListener(_) - | FromSwarm::ListenerError(_) - | FromSwarm::ListenerClosed(_) - | FromSwarm::NewExternalAddrCandidate(_) - | FromSwarm::ExternalAddrConfirmed(_) - | FromSwarm::ExternalAddrExpired(_) => {} + _ => {} } } diff --git a/swarm-derive/CHANGELOG.md b/swarm-derive/CHANGELOG.md index 269c2f1af7f..3ed7b9931df 100644 --- a/swarm-derive/CHANGELOG.md +++ b/swarm-derive/CHANGELOG.md @@ -6,7 +6,7 @@ To same functionality is available using `#[behaviour(to_swarm = "...")]` See [PR 4737](https://github.com/libp2p/rust-libp2p/pull/4737). -## 0.33.0 +## 0.33.0 - Raise MSRV to 1.65. See [PR 3715]. diff --git a/swarm-derive/src/lib.rs b/swarm-derive/src/lib.rs index e8aa79d8470..514975390b0 100644 --- a/swarm-derive/src/lib.rs +++ b/swarm-derive/src/lib.rs @@ -672,79 +672,47 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> syn::Result { - return std::task::Poll::Ready(#network_behaviour_action::GenerateEvent(#into_out_event)) - } - } - }; + let map_in_event = quote! { |event| #wrapped_event }; - quote!{ - match #trait_to_impl::poll(&mut self.#field, cx) { - #generate_event_match_arm - std::task::Poll::Ready(#network_behaviour_action::Dial { opts }) => { - return std::task::Poll::Ready(#network_behaviour_action::Dial { opts }); - } - std::task::Poll::Ready(#network_behaviour_action::ListenOn { opts }) => { - return std::task::Poll::Ready(#network_behaviour_action::ListenOn { opts }); - } - std::task::Poll::Ready(#network_behaviour_action::RemoveListener { id }) => { - return std::task::Poll::Ready(#network_behaviour_action::RemoveListener { id }); - } - std::task::Poll::Ready(#network_behaviour_action::NotifyHandler { peer_id, handler, event }) => { - return std::task::Poll::Ready(#network_behaviour_action::NotifyHandler { - peer_id, - handler, - event: #wrapped_event, - }); - } - std::task::Poll::Ready(#network_behaviour_action::NewExternalAddrCandidate(addr)) => { - return std::task::Poll::Ready(#network_behaviour_action::NewExternalAddrCandidate(addr)); - } - std::task::Poll::Ready(#network_behaviour_action::ExternalAddrConfirmed(addr)) => { - return std::task::Poll::Ready(#network_behaviour_action::ExternalAddrConfirmed(addr)); - } - std::task::Poll::Ready(#network_behaviour_action::ExternalAddrExpired(addr)) => { - return std::task::Poll::Ready(#network_behaviour_action::ExternalAddrExpired(addr)); - } - std::task::Poll::Ready(#network_behaviour_action::CloseConnection { peer_id, connection }) => { - return std::task::Poll::Ready(#network_behaviour_action::CloseConnection { peer_id, connection }); + quote! { + match #trait_to_impl::poll(&mut self.#field, cx) { + std::task::Poll::Ready(e) => return std::task::Poll::Ready(e.map_out(#map_out_event).map_in(#map_in_event)), + std::task::Poll::Pending => {}, } - std::task::Poll::Pending => {}, } - } - }); + }); let out_event_reference = if out_event_definition.is_some() { quote! { #out_event_name #ty_generics } @@ -819,7 +787,6 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> syn::Result std::task::Poll<#network_behaviour_action>> { - use #prelude_path::futures::*; #(#poll_stmts)* std::task::Poll::Pending } diff --git a/swarm/CHANGELOG.md b/swarm/CHANGELOG.md index 6e2e9bb1c0c..717674c9dd1 100644 --- a/swarm/CHANGELOG.md +++ b/swarm/CHANGELOG.md @@ -1,5 +1,7 @@ ## 0.44.0 - unreleased +- Add `#[non_exhaustive]` to `FromSwarm`, `ToSwarm`, `SwarmEvent`, `ConnectionHandlerEvent`, `ConnectionEvent`. + See [PR 4581](https://github.com/libp2p/rust-libp2p/pull/4581). - Remove `handler` field from `ConnectionClosed`. If you need to transfer state from a `ConnectionHandler` to its `NetworkBehaviour` when a connection closes, use `ConnectionHandler::poll_close`. See [PR 4076](https://github.com/libp2p/rust-libp2p/pull/4076). diff --git a/swarm/src/behaviour.rs b/swarm/src/behaviour.rs index 27e62f71831..7fbb72b9260 100644 --- a/swarm/src/behaviour.rs +++ b/swarm/src/behaviour.rs @@ -217,6 +217,7 @@ pub trait NetworkBehaviour: 'static { /// /// [`Swarm`]: super::Swarm #[derive(Debug)] +#[non_exhaustive] pub enum ToSwarm { /// Instructs the `Swarm` to return an event when it is being polled. GenerateEvent(TOutEvent), @@ -394,6 +395,7 @@ pub enum CloseConnection { /// Enumeration with the list of the possible events /// to pass to [`on_swarm_event`](NetworkBehaviour::on_swarm_event). #[derive(Debug)] +#[non_exhaustive] pub enum FromSwarm<'a> { /// Informs the behaviour about a newly established connection to a peer. ConnectionEstablished(ConnectionEstablished<'a>), diff --git a/swarm/src/dummy.rs b/swarm/src/dummy.rs index 1005c4be035..c3e9c22a422 100644 --- a/swarm/src/dummy.rs +++ b/swarm/src/dummy.rs @@ -54,23 +54,7 @@ impl NetworkBehaviour for Behaviour { Poll::Pending } - fn on_swarm_event(&mut self, event: FromSwarm) { - match event { - FromSwarm::ConnectionEstablished(_) - | FromSwarm::ConnectionClosed(_) - | FromSwarm::AddressChange(_) - | FromSwarm::DialFailure(_) - | FromSwarm::ListenFailure(_) - | FromSwarm::NewListener(_) - | FromSwarm::NewListenAddr(_) - | FromSwarm::ExpiredListenAddr(_) - | FromSwarm::ListenerError(_) - | FromSwarm::ListenerClosed(_) - | FromSwarm::NewExternalAddrCandidate(_) - | FromSwarm::ExternalAddrExpired(_) - | FromSwarm::ExternalAddrConfirmed(_) => {} - } - } + fn on_swarm_event(&mut self, _event: FromSwarm) {} } /// An implementation of [`ConnectionHandler`] that neither handles any protocols nor does it keep the connection alive. diff --git a/swarm/src/handler.rs b/swarm/src/handler.rs index ea42f64cca9..f0a46129250 100644 --- a/swarm/src/handler.rs +++ b/swarm/src/handler.rs @@ -214,6 +214,7 @@ pub trait ConnectionHandler: Send + 'static { /// Enumeration with the list of the possible stream events /// to pass to [`on_connection_event`](ConnectionHandler::on_connection_event). +#[non_exhaustive] pub enum ConnectionEvent<'a, IP: InboundUpgradeSend, OP: OutboundUpgradeSend, IOI, OOI> { /// Informs the handler about the output of a successful upgrade on a new inbound substream. FullyNegotiatedInbound(FullyNegotiatedInbound), @@ -539,6 +540,7 @@ impl SubstreamProtocol { /// Event produced by a handler. #[derive(Debug, Clone, PartialEq, Eq)] +#[non_exhaustive] pub enum ConnectionHandlerEvent { /// Request a new outbound substream to be opened with the remote. OutboundSubstreamRequest { diff --git a/swarm/src/lib.rs b/swarm/src/lib.rs index 7bbc1c68924..1aa6bfb30cb 100644 --- a/swarm/src/lib.rs +++ b/swarm/src/lib.rs @@ -172,6 +172,7 @@ pub type THandlerErr = as ConnectionHandler>:: /// Event generated by the `Swarm`. #[derive(Debug)] +#[non_exhaustive] pub enum SwarmEvent { /// Event generated by the `NetworkBehaviour`. Behaviour(TBehaviourOutEvent), diff --git a/swarm/src/test.rs b/swarm/src/test.rs index 9a192444aec..4f6adfc37b0 100644 --- a/swarm/src/test.rs +++ b/swarm/src/test.rs @@ -114,23 +114,7 @@ where self.next_action.take().map_or(Poll::Pending, Poll::Ready) } - fn on_swarm_event(&mut self, event: FromSwarm) { - match event { - FromSwarm::ConnectionEstablished(_) - | FromSwarm::ConnectionClosed(_) - | FromSwarm::AddressChange(_) - | FromSwarm::DialFailure(_) - | FromSwarm::ListenFailure(_) - | FromSwarm::NewListener(_) - | FromSwarm::NewListenAddr(_) - | FromSwarm::ExpiredListenAddr(_) - | FromSwarm::ListenerError(_) - | FromSwarm::ListenerClosed(_) - | FromSwarm::NewExternalAddrCandidate(_) - | FromSwarm::ExternalAddrExpired(_) - | FromSwarm::ExternalAddrConfirmed(_) => {} - } - } + fn on_swarm_event(&mut self, _event: FromSwarm) {} fn on_connection_handler_event( &mut self, diff --git a/swarm/tests/swarm_derive.rs b/swarm/tests/swarm_derive.rs index f13917dcd6c..3337cb7b137 100644 --- a/swarm/tests/swarm_derive.rs +++ b/swarm/tests/swarm_derive.rs @@ -501,23 +501,7 @@ fn custom_out_event_no_type_parameters() { Poll::Pending } - fn on_swarm_event(&mut self, event: FromSwarm) { - match event { - FromSwarm::ConnectionEstablished(_) - | FromSwarm::ConnectionClosed(_) - | FromSwarm::AddressChange(_) - | FromSwarm::DialFailure(_) - | FromSwarm::ListenFailure(_) - | FromSwarm::NewListener(_) - | FromSwarm::NewListenAddr(_) - | FromSwarm::ExpiredListenAddr(_) - | FromSwarm::ListenerError(_) - | FromSwarm::ListenerClosed(_) - | FromSwarm::NewExternalAddrCandidate(_) - | FromSwarm::ExternalAddrExpired(_) - | FromSwarm::ExternalAddrConfirmed(_) => {} - } - } + fn on_swarm_event(&mut self, _event: FromSwarm) {} } #[derive(NetworkBehaviour)]