diff --git a/async_ip/src/lib.rs b/async_ip/src/lib.rs index b60b33cf6..aae3a4ff8 100644 --- a/async_ip/src/lib.rs +++ b/async_ip/src/lib.rs @@ -12,6 +12,7 @@ use reqwest::Client; use serde::{Deserialize, Serialize}; +use std::fmt::Formatter; use std::net::IpAddr; #[cfg(not(target_family = "wasm"))] use std::net::SocketAddr; @@ -190,10 +191,10 @@ pub enum IpRetrieveError { Error(String), } -impl ToString for IpRetrieveError { - fn to_string(&self) -> String { +impl std::fmt::Display for IpRetrieveError { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { - IpRetrieveError::Error(err) => err.to_string(), + IpRetrieveError::Error(err) => write!(f, "{}", err), } } } diff --git a/citadel_wire/src/error.rs b/citadel_wire/src/error.rs index 93e56574c..30487c570 100644 --- a/citadel_wire/src/error.rs +++ b/citadel_wire/src/error.rs @@ -1,3 +1,4 @@ +use std::fmt::Formatter; use tokio::io::Error; #[derive(Debug)] @@ -16,16 +17,19 @@ impl FirewallError { } } -impl ToString for FirewallError { - fn to_string(&self) -> String { - match self { - FirewallError::UPNP(err) => err.to_string(), - FirewallError::HolePunch(err) => err.to_string(), - FirewallError::NotApplicable => "Method not applicable to local node".to_string(), - FirewallError::HolePunchExhausted => "No more NAT traversal methods exist".to_string(), - FirewallError::LocalIPAddrFail => "Unable to obtain local IP info".to_string(), - FirewallError::Skip => "Skipped".to_string(), - } +impl std::fmt::Display for FirewallError { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{}", + match self { + FirewallError::UPNP(err) | FirewallError::HolePunch(err) => err, + FirewallError::NotApplicable => "Method not applicable to local node", + FirewallError::HolePunchExhausted => "No more NAT traversal methods exist", + FirewallError::LocalIPAddrFail => "Unable to obtain local IP info", + FirewallError::Skip => "Skipped", + } + ) } } diff --git a/netbeam/src/sync/network_application.rs b/netbeam/src/sync/network_application.rs index 698206085..b6bc99656 100644 --- a/netbeam/src/sync/network_application.rs +++ b/netbeam/src/sync/network_application.rs @@ -141,37 +141,43 @@ impl MultiplexedConn { } /// Both nodes execute a function, returning once one of the functions gets evaluated - pub fn net_select<'a, F: Send + 'a, R: Send + 'a>(&'a self, future: F) -> NetSelect<'a, R> - where - F: Future, - { + pub fn net_select<'a, F: Future + Send + 'a, R: Send + 'a>( + &'a self, + future: F, + ) -> NetSelect<'a, R> { NetSelect::new(self, self.node_type(), future) } /// Both nodes execute a function, returning once one of the nodes achieves an Ok result - pub fn net_select_ok<'a, F: Send + 'a, R: Send + 'a>(&'a self, future: F) -> NetSelectOk<'a, R> - where - F: Future>, - { + pub fn net_select_ok< + 'a, + F: Future> + Send + 'a, + R: Send + 'a, + >( + &'a self, + future: F, + ) -> NetSelectOk<'a, R> { NetSelectOk::new(self, self.node_type(), future) } /// Both nodes execute a function, returning the output once both nodes finish the operation - pub fn net_join<'a, F: Send + 'a, R: Send + 'a>(&'a self, future: F) -> NetJoin<'a, R> - where - F: Future, - { + pub fn net_join<'a, F: Future + Send + 'a, R: Send + 'a>( + &'a self, + future: F, + ) -> NetJoin<'a, R> { NetJoin::new(self, self.node_type(), future) } /// Both nodes attempt to execute a fallible function. Returns once both functions return Ok, or, when one returns an error - pub fn net_try_join<'a, F: Send + 'a, R: Send + 'a, E: Send + 'a>( + pub fn net_try_join< + 'a, + F: Future> + Send + 'a, + R: Send + 'a, + E: Send + 'a, + >( &'a self, future: F, - ) -> NetTryJoin<'a, R, E> - where - F: Future>, - { + ) -> NetTryJoin<'a, R, E> { NetTryJoin::new(self, self.node_type(), future) } @@ -181,10 +187,10 @@ impl MultiplexedConn { } /// Returns the payload to the adjacent node at about the same time. This node receives the payload sent by the adjacent node (exchange) - pub fn sync_exchange_payload<'a, R: 'a>(&'a self, payload: R) -> NetSyncStart<'a, R> - where - R: Serialize + DeserializeOwned + Send + Sync, - { + pub fn sync_exchange_payload<'a, R: Serialize + DeserializeOwned + Send + Sync + 'a>( + &'a self, + payload: R, + ) -> NetSyncStart<'a, R> { NetSyncStart::exchange_payload(self, self.node_type(), payload) } @@ -192,21 +198,15 @@ impl MultiplexedConn { /// - payload: an element to exchange with the opposite node pub fn sync_execute< 'a, - F: 'a, - Fx: 'a, + F: Future + Send + 'a, + Fx: FnOnce(P) -> F + Send + 'a, P: Serialize + DeserializeOwned + Send + Sync + 'a, R: 'a, >( &'a self, future: Fx, payload: P, - ) -> NetSyncStart<'a, R> - where - F: Future, - F: Send, - Fx: FnOnce(P) -> F, - Fx: Send, - { + ) -> NetSyncStart<'a, R> { NetSyncStart::new(self, self.node_type(), future, payload) } diff --git a/netbeam/src/sync/operations/net_join.rs b/netbeam/src/sync/operations/net_join.rs index d0063a0f5..728123a50 100644 --- a/netbeam/src/sync/operations/net_join.rs +++ b/netbeam/src/sync/operations/net_join.rs @@ -18,15 +18,12 @@ impl<'a, T: Send + 'a> NetJoin<'a, T> { S: Subscribable, K: MultiplexedConnKey + 'a, Conn: ReliableOrderedStreamToTarget + 'static, - F: Send + 'a, + F: Future + Send + 'a, >( conn: &'a S, local_node_type: RelativeNodeType, future: F, - ) -> Self - where - F: Future, - { + ) -> Self { // we can safely unwrap since we are wrapping the result in an Ok() Self { future: Box::pin( diff --git a/netbeam/src/sync/operations/net_select.rs b/netbeam/src/sync/operations/net_select.rs index 9d8c29e25..734dd0ed3 100644 --- a/netbeam/src/sync/operations/net_select.rs +++ b/netbeam/src/sync/operations/net_select.rs @@ -18,15 +18,12 @@ impl<'a, R: Send + 'a> NetSelect<'a, R> { S: Subscribable, K: MultiplexedConnKey + 'a, Conn: ReliableOrderedStreamToTarget + 'static, - F: Send + 'a, + F: Future + Send + 'a, >( conn: &'a S, local_node_type: RelativeNodeType, future: F, - ) -> Self - where - F: Future, - { + ) -> Self { Self { future: Box::pin( NetSelectOk::new(conn, local_node_type, future.map(Ok)) diff --git a/netbeam/src/sync/operations/net_select_ok.rs b/netbeam/src/sync/operations/net_select_ok.rs index 49452472b..ee315121a 100644 --- a/netbeam/src/sync/operations/net_select_ok.rs +++ b/netbeam/src/sync/operations/net_select_ok.rs @@ -19,15 +19,12 @@ impl<'a, R: Send + 'a> NetSelectOk<'a, R> { S: Subscribable, K: MultiplexedConnKey + 'a, Conn: ReliableOrderedStreamToTarget + 'static, - F: Send + 'a, + F: Future> + Send + 'a, >( conn: &'a S, local_node_type: RelativeNodeType, future: F, - ) -> Self - where - F: Future>, - { + ) -> Self { Self { future: Box::pin(resolve(conn, local_node_type, future)), } diff --git a/netbeam/src/sync/operations/net_try_join.rs b/netbeam/src/sync/operations/net_try_join.rs index ecabedc55..e99dce20e 100644 --- a/netbeam/src/sync/operations/net_try_join.rs +++ b/netbeam/src/sync/operations/net_try_join.rs @@ -19,15 +19,12 @@ impl<'a, T: Send + 'a, E: Send + 'a> NetTryJoin<'a, T, E> { S: Subscribable, K: MultiplexedConnKey + 'a, Conn: ReliableOrderedStreamToTarget + 'static, - F: Send + 'a, + F: Future> + Send + 'a, >( conn: &'a S, local_node_type: RelativeNodeType, future: F, - ) -> NetTryJoin<'a, T, E> - where - F: Future>, - { + ) -> NetTryJoin<'a, T, E> { Self { future: Box::pin(resolve(conn, local_node_type, future)), } diff --git a/netbeam/src/sync/primitives/net_rwlock.rs b/netbeam/src/sync/primitives/net_rwlock.rs index 8b1a3e7e5..aec0fc9de 100644 --- a/netbeam/src/sync/primitives/net_rwlock.rs +++ b/netbeam/src/sync/primitives/net_rwlock.rs @@ -611,7 +611,7 @@ async fn passive_background_handler( LockType::Read => { // load inside local map to allow instant local read access let lock = Some(Arc::new(lock)); - *read_lock_local.write() = lock.clone(); + read_lock_local.write().clone_from(&lock); LocalLockHolder::Read(lock, true) } diff --git a/netbeam/src/sync/sync_start.rs b/netbeam/src/sync/sync_start.rs index 1e8d2597f..84841f731 100644 --- a/netbeam/src/sync/sync_start.rs +++ b/netbeam/src/sync/sync_start.rs @@ -20,21 +20,15 @@ impl<'a, R: 'a> NetSyncStart<'a, R> { S: Subscribable, K: MultiplexedConnKey + 'a, Conn: ReliableOrderedStreamToTarget + 'static, - F: 'a, - Fx: 'a, + F: Future + Send + 'a, + Fx: FnOnce(P) -> F + Send + 'a, P: Serialize + DeserializeOwned + Send + Sync + 'a, >( conn: &'a S, relative_node_type: RelativeNodeType, future: Fx, payload: P, - ) -> Self - where - F: Future, - F: Send, - Fx: FnOnce(P) -> F, - Fx: Send, - { + ) -> Self { Self { future: Box::pin(synchronize(conn, relative_node_type, future, payload)), }