From a69484b52df51e6422772559c6b667e196c6a299 Mon Sep 17 00:00:00 2001 From: Max Inden Date: Fri, 25 Feb 2022 21:56:00 +0100 Subject: [PATCH] *: Apply clippy suggestions --- protocols/gossipsub/src/behaviour.rs | 7 +-- protocols/identify/src/identify.rs | 3 +- protocols/relay/src/v1/behaviour.rs | 5 +- .../relay/src/v1/protocol/outgoing_dst_req.rs | 6 +-- .../src/v1/protocol/outgoing_relay_req.rs | 6 +-- protocols/relay/src/v2/relay.rs | 3 +- swarm/src/handler/multi.rs | 3 +- swarm/src/lib.rs | 8 +-- transports/wasm-ext/src/lib.rs | 52 ++++++++----------- 9 files changed, 37 insertions(+), 56 deletions(-) diff --git a/protocols/gossipsub/src/behaviour.rs b/protocols/gossipsub/src/behaviour.rs index 4f3fdcfb427..583af5ec1cb 100644 --- a/protocols/gossipsub/src/behaviour.rs +++ b/protocols/gossipsub/src/behaviour.rs @@ -473,14 +473,9 @@ where /// Lists all mesh peers for a certain topic hash. pub fn mesh_peers(&self, topic_hash: &TopicHash) -> impl Iterator { - self.mesh - .get(topic_hash) - .into_iter() - .map(|x| x.iter()) - .flatten() + self.mesh.get(topic_hash).into_iter().flat_map(|x| x.iter()) } - /// Lists all mesh peers for all topics. pub fn all_mesh_peers(&self) -> impl Iterator { let mut res = BTreeSet::new(); for peers in self.mesh.values() { diff --git a/protocols/identify/src/identify.rs b/protocols/identify/src/identify.rs index 54c00ab86d9..f39e9d6900e 100644 --- a/protocols/identify/src/identify.rs +++ b/protocols/identify/src/identify.rs @@ -240,8 +240,7 @@ impl NetworkBehaviour for Identify { if let Some(entry) = self.discovered_peers.get_mut(peer_id) { for addr in failed_addresses .into_iter() - .map(|addresses| addresses.into_iter()) - .flatten() + .flat_map(|addresses| addresses.into_iter()) { entry.remove(addr); } diff --git a/protocols/relay/src/v1/behaviour.rs b/protocols/relay/src/v1/behaviour.rs index b0e3ed8d774..57e5b080864 100644 --- a/protocols/relay/src/v1/behaviour.rs +++ b/protocols/relay/src/v1/behaviour.rs @@ -189,12 +189,11 @@ impl NetworkBehaviour for Relay { .get(remote_peer_id) .into_iter() .flatten() - .map( + .flat_map( |IncomingRelayReq::DialingDst { incoming_relay_req, .. }| incoming_relay_req.dst_peer().addrs.clone(), - ) - .flatten(), + ), ) .collect() } diff --git a/protocols/relay/src/v1/protocol/outgoing_dst_req.rs b/protocols/relay/src/v1/protocol/outgoing_dst_req.rs index 60871b17a93..1cd3e78d18d 100644 --- a/protocols/relay/src/v1/protocol/outgoing_dst_req.rs +++ b/protocols/relay/src/v1/protocol/outgoing_dst_req.rs @@ -110,8 +110,7 @@ impl upgrade::OutboundUpgrade for OutgoingDstReq { } = CircuitRelay::decode(msg)?; match r#type - .map(circuit_relay::Type::from_i32) - .flatten() + .and_then(circuit_relay::Type::from_i32) .ok_or(OutgoingDstReqError::ParseTypeField)? { circuit_relay::Type::Status => {} @@ -126,8 +125,7 @@ impl upgrade::OutboundUpgrade for OutgoingDstReq { } match code - .map(circuit_relay::Status::from_i32) - .flatten() + .and_then(circuit_relay::Status::from_i32) .ok_or(OutgoingDstReqError::ParseStatusField)? { circuit_relay::Status::Success => {} diff --git a/protocols/relay/src/v1/protocol/outgoing_relay_req.rs b/protocols/relay/src/v1/protocol/outgoing_relay_req.rs index b6e062d304e..f67a57712dd 100644 --- a/protocols/relay/src/v1/protocol/outgoing_relay_req.rs +++ b/protocols/relay/src/v1/protocol/outgoing_relay_req.rs @@ -120,8 +120,7 @@ impl upgrade::OutboundUpgrade for OutgoingRelayReq { } = CircuitRelay::decode(msg)?; match r#type - .map(circuit_relay::Type::from_i32) - .flatten() + .and_then(circuit_relay::Type::from_i32) .ok_or(OutgoingRelayReqError::ParseTypeField)? { circuit_relay::Type::Status => {} @@ -129,8 +128,7 @@ impl upgrade::OutboundUpgrade for OutgoingRelayReq { } match code - .map(circuit_relay::Status::from_i32) - .flatten() + .and_then(circuit_relay::Status::from_i32) .ok_or(OutgoingRelayReqError::ParseStatusField)? { circuit_relay::Status::Success => {} diff --git a/protocols/relay/src/v2/relay.rs b/protocols/relay/src/v2/relay.rs index 428418780de..e5f2cdbfe77 100644 --- a/protocols/relay/src/v2/relay.rs +++ b/protocols/relay/src/v2/relay.rs @@ -437,8 +437,7 @@ impl NetworkBehaviour for Relay { } else if let Some(dst_conn) = self .reservations .get(&inbound_circuit_req.dst()) - .map(|cs| cs.iter().next()) - .flatten() + .and_then(|cs| cs.iter().next()) { // Accept circuit request if reservation present. let circuit_id = self.circuits.insert(Circuit { diff --git a/swarm/src/handler/multi.rs b/swarm/src/handler/multi.rs index 02ce4b24039..7530c35a77a 100644 --- a/swarm/src/handler/multi.rs +++ b/swarm/src/handler/multi.rs @@ -452,8 +452,7 @@ where self.upgrades .iter() .enumerate() - .map(|(i, (_, h))| iter::repeat(i).zip(h.protocol_info())) - .flatten() + .flat_map(|(i, (_, h))| iter::repeat(i).zip(h.protocol_info())) .map(|(i, h)| IndexedProtoName(i, h)) .collect::>() .into_iter() diff --git a/swarm/src/lib.rs b/swarm/src/lib.rs index c9e58ee0e87..c1262d1cbb1 100644 --- a/swarm/src/lib.rs +++ b/swarm/src/lib.rs @@ -1192,16 +1192,16 @@ pub struct SwarmPollParameters<'a> { } impl<'a> PollParameters for SwarmPollParameters<'a> { - type SupportedProtocolsIter = std::vec::IntoIter>; - type ListenedAddressesIter = std::vec::IntoIter; + type SupportedProtocolsIter = std::iter::Cloned>>; + type ListenedAddressesIter = std::iter::Cloned>; type ExternalAddressesIter = AddressIntoIter; fn supported_protocols(&self) -> Self::SupportedProtocolsIter { - self.supported_protocols.to_vec().into_iter() + self.supported_protocols.iter().cloned() } fn listened_addresses(&self) -> Self::ListenedAddressesIter { - self.listened_addrs.to_vec().into_iter() + self.listened_addrs.iter().cloned() } fn external_addresses(&self) -> Self::ExternalAddressesIter { diff --git a/transports/wasm-ext/src/lib.rs b/transports/wasm-ext/src/lib.rs index 5ef8dbb0bbd..be32907d2f5 100644 --- a/transports/wasm-ext/src/lib.rs +++ b/transports/wasm-ext/src/lib.rs @@ -311,39 +311,33 @@ impl Stream for Listen { return Poll::Ready(None); }; - for addr in event - .new_addrs() - .into_iter() - .flat_map(|e| e.to_vec().into_iter()) - { - let addr = js_value_to_addr(&addr)?; - self.pending_events - .push_back(ListenerEvent::NewAddress(addr)); + if let Some(addrs) = event.new_addrs() { + for addr in addrs.into_iter() { + let addr = js_value_to_addr(&addr)?; + self.pending_events + .push_back(ListenerEvent::NewAddress(addr)); + } } - for upgrade in event - .new_connections() - .into_iter() - .flat_map(|e| e.to_vec().into_iter()) - { - let upgrade: ffi::ConnectionEvent = upgrade.into(); - self.pending_events.push_back(ListenerEvent::Upgrade { - local_addr: upgrade.local_addr().parse()?, - remote_addr: upgrade.observed_addr().parse()?, - upgrade: futures::future::ok(Connection::new(upgrade.connection())), - }); + if let Some(upgrades) = event.new_connections() { + for upgrade in upgrades.into_iter().cloned() { + let upgrade: ffi::ConnectionEvent = upgrade.into(); + self.pending_events.push_back(ListenerEvent::Upgrade { + local_addr: upgrade.local_addr().parse()?, + remote_addr: upgrade.observed_addr().parse()?, + upgrade: futures::future::ok(Connection::new(upgrade.connection())), + }); + } } - for addr in event - .expired_addrs() - .into_iter() - .flat_map(|e| e.to_vec().into_iter()) - { - match js_value_to_addr(&addr) { - Ok(addr) => self - .pending_events - .push_back(ListenerEvent::NewAddress(addr)), - Err(err) => self.pending_events.push_back(ListenerEvent::Error(err)), + if let Some(addrs) = event.expired_addrs() { + for addr in addrs.into_iter() { + match js_value_to_addr(&addr) { + Ok(addr) => self + .pending_events + .push_back(ListenerEvent::NewAddress(addr)), + Err(err) => self.pending_events.push_back(ListenerEvent::Error(err)), + } } } }