Преглед изворни кода

src/net: use to_string from Display trait for OutboundState and P2pState enums

ghassmo пре 4 година
родитељ
комит
bcd4dcc2ee
2 измењених фајлова са 34 додато и 22 уклоњено
  1. 17 11
      src/net/p2p.rs
  2. 17 11
      src/net/session/outbound_session.rs

+ 17 - 11
src/net/p2p.rs

@@ -1,13 +1,15 @@
-use async_executor::Executor;
 use async_std::sync::Mutex;
-use log::debug;
-use serde_json::json;
 use std::{
     collections::{HashMap, HashSet},
+    fmt,
     net::SocketAddr,
     sync::Arc,
 };
 
+use async_executor::Executor;
+use log::debug;
+use serde_json::json;
+
 use crate::{
     error::{Error, Result},
     net::{
@@ -37,14 +39,18 @@ enum P2pState {
     Run,
 }
 
-impl P2pState {
-    fn to_string(&self) -> String {
-        match self {
-            Self::Open => "open".to_string(),
-            Self::Start => "start".to_string(),
-            Self::Started => "started".to_string(),
-            Self::Run => "run".to_string(),
-        }
+impl fmt::Display for P2pState {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        write!(
+            f,
+            "{}",
+            match self {
+                Self::Open => "open",
+                Self::Start => "start",
+                Self::Started => "started",
+                Self::Run => "run",
+            }
+        )
     }
 }
 

+ 17 - 11
src/net/session/outbound_session.rs

@@ -1,13 +1,15 @@
-use async_executor::Executor;
 use async_std::{sync::Mutex, task::yield_now};
-use async_trait::async_trait;
-use log::{error, info};
-use serde_json::json;
 use std::{
+    fmt,
     net::SocketAddr,
     sync::{Arc, Weak},
 };
 
+use async_executor::Executor;
+use async_trait::async_trait;
+use log::{error, info};
+use serde_json::json;
+
 use crate::{
     error::{Error, Result},
     net::{
@@ -24,13 +26,17 @@ enum OutboundState {
     Connected,
 }
 
-impl OutboundState {
-    fn to_string(&self) -> String {
-        match self {
-            Self::Open => "open".to_string(),
-            Self::Pending => "pending".to_string(),
-            Self::Connected => "connected".to_string(),
-        }
+impl fmt::Display for OutboundState {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        write!(
+            f,
+            "{}",
+            match self {
+                Self::Open => "open",
+                Self::Pending => "pending",
+                Self::Connected => "connected",
+            }
+        )
     }
 }