Skip to content

Commit

Permalink
refactor: remove unnecessary clones and async move blocks
Browse files Browse the repository at this point in the history
It became possible after migration to async/await.
  • Loading branch information
kurnevsky committed Sep 19, 2020
1 parent 91eb563 commit f6c9150
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 15 deletions.
5 changes: 2 additions & 3 deletions examples/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,9 @@ async fn main() -> Result<(), Error> {
onion_client.add_path_node(node).await;
}

let tcp_connections_c = tcp_connections.clone();
let net_crypto_tcp_future = async move {
let net_crypto_tcp_future = async {
while let Some((packet, pk)) = net_crypto_tcp_rx.next().await {
tcp_connections_c.send_data(pk, packet).await?;
tcp_connections.send_data(pk, packet).await?;
}
Result::<(), Error>::Ok(())
};
Expand Down
13 changes: 6 additions & 7 deletions examples/onion_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ extern crate log;
use std::net::{SocketAddr, IpAddr};

use failure::Error;
use futures::future::{FutureExt};
use futures::stream::{StreamExt};
use futures::future::FutureExt;
use futures::stream::StreamExt;
use futures::sink::SinkExt;
use futures::channel::mpsc;
use tokio_util::udp::UdpFramed;
Expand Down Expand Up @@ -84,8 +84,7 @@ async fn main() -> Result<(), Error> {

let (mut sink, mut stream) = UdpFramed::new(socket, codec).split();

let client = onion_client.clone();
let network_reader = async move {
let network_reader = async {
while let Some(event) = stream.next().await {
let (packet, addr) = match event {
Ok(ev) => ev,
Expand All @@ -103,11 +102,11 @@ async fn main() -> Result<(), Error> {
Packet::OnionAnnounceResponse(packet) => {
let is_global = IsGlobal::is_global(&addr.ip());

client.handle_announce_response(&packet, is_global).await
onion_client.handle_announce_response(&packet, is_global).await
.map_err(Error::from)
},
Packet::OnionDataResponse(packet) =>
client.handle_data_response(&packet).await
onion_client.handle_data_response(&packet).await
.map_err(Error::from),
_ => Ok(()),
};
Expand All @@ -122,7 +121,7 @@ async fn main() -> Result<(), Error> {
Ok(())
};

let network_writer = async move {
let network_writer = async {
while let Some((packet, mut addr)) = rx.next().await {
if is_ipv4 && addr.is_ipv6() { continue }

Expand Down
2 changes: 1 addition & 1 deletion examples/tcp_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn main() {
let server = Server::new();

let stats = Stats::new();
let future = async move {
let future = async {
let listener = TcpListener::bind(&addr).await.unwrap();
drop(tcp_run(&server, listener, server_sk, stats, TCP_CONNECTIONS_LIMIT).await);
};
Expand Down
4 changes: 2 additions & 2 deletions tox_core/src/friend_connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,7 @@ mod tests {

let net_crypto = friend_connections.net_crypto.clone();
let dht = friend_connections.dht.clone();
let packets_future = async move {
let packets_future = async {
dht.handle_packet(DhtPacket::CryptoHandshake(handshake), friend_saddr).await.unwrap();

let session_pk = net_crypto.get_session_pk(&friend_pk).await.unwrap();
Expand All @@ -984,7 +984,7 @@ mod tests {
assert_eq!(received_data, vec![PACKET_ID_ALIVE]);
};

let connection_status_future = async move {
let connection_status_future = async {
let packet = connection_status_rx.next().await;
let (pk, status) = packet.unwrap();
assert_eq!(pk, friend_pk);
Expand Down
2 changes: 1 addition & 1 deletion tox_core/src/onion/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ impl OnionClient {
}

/// Run periodical announcements and friends searching.
pub async fn run(self) -> Result<(), RunError> {
pub async fn run(&self) -> Result<(), RunError> {
let interval = Duration::from_secs(1);
let mut wakeups = tokio::time::interval(interval);

Expand Down
2 changes: 1 addition & 1 deletion tox_core/src/relay/client/connections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ impl Connections {

/// Run TCP periodical tasks. Result future will never be completed
/// successfully.
pub async fn run(self) -> Result<(), ConnectionError> {
pub async fn run(&self) -> Result<(), ConnectionError> {
let mut wakeups = tokio::time::interval(CONNECTIONS_INTERVAL);

while wakeups.next().await.is_some() {
Expand Down

0 comments on commit f6c9150

Please sign in to comment.