From bcc6721257889f85f57af1b40351540585ffd41d Mon Sep 17 00:00:00 2001 From: hozan23 Date: Thu, 27 Jun 2024 03:52:18 +0200 Subject: Remove redundant type aliases --- core/src/event.rs | 5 ++--- core/src/pubsub.rs | 17 ++++++++--------- p2p/README.md | 2 +- p2p/examples/chat.rs | 8 ++++---- p2p/src/backend.rs | 17 +++++------------ p2p/src/discovery/mod.rs | 4 +--- p2p/src/lib.rs | 4 ++-- p2p/src/monitor/mod.rs | 6 +++--- p2p/src/peer/mod.rs | 18 ++++++++---------- p2p/src/peer_pool.rs | 15 ++++----------- p2p/src/protocol.rs | 14 ++++++-------- p2p/src/protocols/ping.rs | 8 ++++---- 12 files changed, 48 insertions(+), 70 deletions(-) diff --git a/core/src/event.rs b/core/src/event.rs index 1632df3..a4e356b 100644 --- a/core/src/event.rs +++ b/core/src/event.rs @@ -14,7 +14,6 @@ use crate::{async_runtime::lock::Mutex, util::random_32, Result}; const CHANNEL_BUFFER_SIZE: usize = 1000; -pub type ArcEventSys = Arc>; pub type EventListenerID = u32; type Listeners = HashMap>>>; @@ -84,7 +83,7 @@ where T: std::hash::Hash + Eq + std::fmt::Debug + Clone, { /// Creates a new [`EventSys`] - pub fn new() -> ArcEventSys { + pub fn new() -> Arc> { Arc::new(Self { listeners: Mutex::new(HashMap::new()), listener_buffer_size: CHANNEL_BUFFER_SIZE, @@ -101,7 +100,7 @@ where /// starts to consume the buffered events. /// /// If `size` is zero, this function will panic. - pub fn with_buffer_size(size: usize) -> ArcEventSys { + pub fn with_buffer_size(size: usize) -> Arc> { Arc::new(Self { listeners: Mutex::new(HashMap::new()), listener_buffer_size: size, diff --git a/core/src/pubsub.rs b/core/src/pubsub.rs index 09b62ea..7aa4936 100644 --- a/core/src/pubsub.rs +++ b/core/src/pubsub.rs @@ -7,7 +7,6 @@ use crate::{async_runtime::lock::Mutex, util::random_32, Result}; const CHANNEL_BUFFER_SIZE: usize = 1000; -pub type ArcPublisher = Arc>; pub type SubscriptionID = u32; /// A simple publish-subscribe system. @@ -36,7 +35,7 @@ pub struct Publisher { impl Publisher { /// Creates a new [`Publisher`] - pub fn new() -> ArcPublisher { + pub fn new() -> Arc> { Arc::new(Self { subs: Mutex::new(HashMap::new()), subscription_buffer_size: CHANNEL_BUFFER_SIZE, @@ -53,7 +52,7 @@ impl Publisher { /// the buffered messages. /// /// If `size` is zero, this function will panic. - pub fn with_buffer_size(size: usize) -> ArcPublisher { + pub fn with_buffer_size(size: usize) -> Arc> { Arc::new(Self { subs: Mutex::new(HashMap::new()), subscription_buffer_size: size, @@ -79,7 +78,7 @@ impl Publisher { sub } - /// Unsubscribes from the publisher + /// Unsubscribes by providing subscription id pub async fn unsubscribe(self: &Arc, id: &SubscriptionID) { self.subs.lock().await.remove(id); } @@ -114,14 +113,14 @@ impl Publisher { pub struct Subscription { id: SubscriptionID, recv_chan: async_channel::Receiver, - publisher: ArcPublisher, + publisher: Arc>, } impl Subscription { - /// Creates a new Subscription + /// Creates a new [`Subscription`] pub fn new( id: SubscriptionID, - publisher: ArcPublisher, + publisher: Arc>, recv_chan: async_channel::Receiver, ) -> Subscription { Self { @@ -131,13 +130,13 @@ impl Subscription { } } - /// Receive a message from the Publisher + /// Receive a message from the [`Publisher`] pub async fn recv(&self) -> Result { let msg = self.recv_chan.recv().await?; Ok(msg) } - /// Unsubscribe from the Publisher + /// Unsubscribe from the [`Publisher`] pub async fn unsubscribe(&self) { self.publisher.unsubscribe(&self.id).await; } diff --git a/p2p/README.md b/p2p/README.md index e63120c..efd6d60 100644 --- a/p2p/README.md +++ b/p2p/README.md @@ -81,7 +81,7 @@ pub struct NewProtocol { } impl NewProtocol { - fn new(peer: ArcPeer) -> ArcProtocol { + fn new(peer: Arc) -> Arc { Arc::new(Self { peer, }) diff --git a/p2p/examples/chat.rs b/p2p/examples/chat.rs index d162371..4dd87c5 100644 --- a/p2p/examples/chat.rs +++ b/p2p/examples/chat.rs @@ -10,8 +10,8 @@ use smol::{channel, Executor}; use karyon_p2p::{ endpoint::{Endpoint, Port}, keypair::{KeyPair, KeyPairType}, - protocol::{ArcProtocol, Protocol, ProtocolEvent, ProtocolID}, - ArcPeer, Backend, Config, Error, Version, + protocol::{Protocol, ProtocolEvent, ProtocolID}, + Backend, Config, Error, Peer, Version, }; use shared::run_executor; @@ -42,12 +42,12 @@ struct Cli { pub struct ChatProtocol { username: String, - peer: ArcPeer, + peer: Arc, executor: Arc>, } impl ChatProtocol { - fn new(username: &str, peer: ArcPeer, executor: Arc>) -> ArcProtocol { + fn new(username: &str, peer: Arc, executor: Arc>) -> Arc { Arc::new(Self { peer, username: username.to_string(), diff --git a/p2p/src/backend.rs b/p2p/src/backend.rs index f21d70b..4d24be6 100644 --- a/p2p/src/backend.rs +++ b/p2p/src/backend.rs @@ -6,17 +6,10 @@ use karyon_core::{async_runtime::Executor, crypto::KeyPair}; use karyon_net::Endpoint; use crate::{ - config::Config, - conn_queue::ConnQueue, - discovery::{ArcDiscovery, Discovery}, - monitor::Monitor, - peer_pool::PeerPool, - protocol::{ArcProtocol, Protocol}, - ArcPeer, PeerID, Result, + config::Config, conn_queue::ConnQueue, discovery::Discovery, monitor::Monitor, peer::Peer, + peer_pool::PeerPool, protocol::Protocol, PeerID, Result, }; -pub type ArcBackend = Arc; - /// Backend serves as the central entry point for initiating and managing /// the P2P network. pub struct Backend { @@ -33,7 +26,7 @@ pub struct Backend { monitor: Arc, /// Discovery instance. - discovery: ArcDiscovery, + discovery: Arc, /// PeerPool instance. peer_pool: Arc, @@ -41,7 +34,7 @@ pub struct Backend { impl Backend { /// Creates a new Backend. - pub fn new(key_pair: &KeyPair, config: Config, ex: Executor) -> ArcBackend { + pub fn new(key_pair: &KeyPair, config: Config, ex: Executor) -> Arc { let config = Arc::new(config); let monitor = Arc::new(Monitor::new(config.clone())); let conn_queue = ConnQueue::new(); @@ -87,7 +80,7 @@ impl Backend { /// Attach a custom protocol to the network pub async fn attach_protocol( &self, - c: impl Fn(ArcPeer) -> ArcProtocol + Send + Sync + 'static, + c: impl Fn(Arc) -> Arc + Send + Sync + 'static, ) -> Result<()> { self.peer_pool.attach_protocol::

(Box::new(c)).await } diff --git a/p2p/src/discovery/mod.rs b/p2p/src/discovery/mod.rs index 99f880d..dae4d3f 100644 --- a/p2p/src/discovery/mod.rs +++ b/p2p/src/discovery/mod.rs @@ -32,8 +32,6 @@ use crate::{ use lookup::LookupService; use refresh::RefreshService; -pub type ArcDiscovery = Arc; - pub struct Discovery { /// Routing table table: Arc, @@ -69,7 +67,7 @@ impl Discovery { config: Arc, monitor: Arc, ex: Executor, - ) -> ArcDiscovery { + ) -> Arc { let table = Arc::new(RoutingTable::new(peer_id.0)); let refresh_service = Arc::new(RefreshService::new( diff --git a/p2p/src/lib.rs b/p2p/src/lib.rs index ebc51d8..b21a353 100644 --- a/p2p/src/lib.rs +++ b/p2p/src/lib.rs @@ -61,9 +61,9 @@ pub mod monitor; /// [`Read More`](./protocol/trait.Protocol.html) pub mod protocol; -pub use backend::{ArcBackend, Backend}; +pub use backend::Backend; pub use config::Config; -pub use peer::{ArcPeer, PeerID}; +pub use peer::{Peer, PeerID}; pub use version::Version; pub mod endpoint { diff --git a/p2p/src/monitor/mod.rs b/p2p/src/monitor/mod.rs index 34d252e..4ecb431 100644 --- a/p2p/src/monitor/mod.rs +++ b/p2p/src/monitor/mod.rs @@ -2,7 +2,7 @@ mod event; use std::sync::Arc; -use karyon_core::event::{ArcEventSys, EventListener, EventSys, EventValue, EventValueTopic}; +use karyon_core::event::{EventListener, EventSys, EventValue, EventValueTopic}; use karyon_net::Endpoint; @@ -15,7 +15,7 @@ use crate::{Config, PeerID}; /// Responsible for network and system monitoring. /// -/// It use pub-sub pattern to notify the subscribers with new events. +/// It use event emitter to notify the registerd listeners about new events. /// /// # Example /// @@ -45,7 +45,7 @@ use crate::{Config, PeerID}; /// }; /// ``` pub struct Monitor { - event_sys: ArcEventSys, + event_sys: Arc>, config: Arc, } diff --git a/p2p/src/peer/mod.rs b/p2p/src/peer/mod.rs index 0d42da3..2068789 100644 --- a/p2p/src/peer/mod.rs +++ b/p2p/src/peer/mod.rs @@ -2,7 +2,7 @@ mod peer_id; pub use peer_id::PeerID; -use std::sync::Arc; +use std::sync::{Arc, Weak}; use async_channel::{Receiver, Sender}; use bincode::{Decode, Encode}; @@ -11,7 +11,7 @@ use log::{error, trace}; use karyon_core::{ async_runtime::{lock::RwLock, Executor}, async_util::{select, Either, TaskGroup, TaskResult}, - event::{ArcEventSys, EventListener, EventSys}, + event::{EventListener, EventSys}, util::{decode, encode}, }; @@ -20,19 +20,17 @@ use karyon_net::{Conn, Endpoint}; use crate::{ conn_queue::ConnDirection, message::{NetMsg, NetMsgCmd, ProtocolMsg, ShutdownMsg}, - peer_pool::{ArcPeerPool, WeakPeerPool}, + peer_pool::PeerPool, protocol::{Protocol, ProtocolEvent, ProtocolID}, Config, Error, Result, }; -pub type ArcPeer = Arc; - pub struct Peer { /// Peer's ID id: PeerID, /// A weak pointer to `PeerPool` - peer_pool: WeakPeerPool, + peer_pool: Weak, /// Holds the peer connection conn: Conn, @@ -47,7 +45,7 @@ pub struct Peer { protocol_ids: RwLock>, /// `EventSys` responsible for sending events to the protocols. - protocol_events: ArcEventSys, + protocol_events: Arc>, /// This channel is used to send a stop signal to the read loop. stop_chan: (Sender>, Receiver>), @@ -59,13 +57,13 @@ pub struct Peer { impl Peer { /// Creates a new peer pub fn new( - peer_pool: WeakPeerPool, + peer_pool: Weak, id: &PeerID, conn: Conn, remote_endpoint: Endpoint, conn_direction: ConnDirection, ex: Executor, - ) -> ArcPeer { + ) -> Arc { Arc::new(Peer { id: id.clone(), peer_pool, @@ -228,7 +226,7 @@ impl Peer { } } - fn peer_pool(&self) -> ArcPeerPool { + fn peer_pool(&self) -> Arc { self.peer_pool.upgrade().unwrap() } } diff --git a/p2p/src/peer_pool.rs b/p2p/src/peer_pool.rs index f8dda66..02a74ac 100644 --- a/p2p/src/peer_pool.rs +++ b/p2p/src/peer_pool.rs @@ -1,8 +1,4 @@ -use std::{ - collections::HashMap, - sync::{Arc, Weak}, - time::Duration, -}; +use std::{collections::HashMap, sync::Arc, time::Duration}; use async_channel::Sender; use bincode::{Decode, Encode}; @@ -21,16 +17,13 @@ use crate::{ conn_queue::{ConnDirection, ConnQueue}, message::{get_msg_payload, NetMsg, NetMsgCmd, VerAckMsg, VerMsg}, monitor::{Monitor, PPEvent}, - peer::{ArcPeer, Peer, PeerID}, + peer::Peer, protocol::{Protocol, ProtocolConstructor, ProtocolID}, protocols::PingProtocol, version::{version_match, Version, VersionInt}, - Error, Result, + Error, PeerID, Result, }; -pub type ArcPeerPool = Arc; -pub type WeakPeerPool = Weak; - pub struct PeerPool { /// Peer's ID pub id: PeerID, @@ -39,7 +32,7 @@ pub struct PeerPool { conn_queue: Arc, /// Holds the running peers. - peers: RwLock>, + peers: RwLock>>, /// Hashmap contains protocol constructors. pub(crate) protocols: RwLock>>, diff --git a/p2p/src/protocol.rs b/p2p/src/protocol.rs index d42f092..021844f 100644 --- a/p2p/src/protocol.rs +++ b/p2p/src/protocol.rs @@ -4,11 +4,9 @@ use async_trait::async_trait; use karyon_core::event::EventValue; -use crate::{peer::ArcPeer, version::Version, Result}; +use crate::{peer::Peer, version::Version, Result}; -pub type ArcProtocol = Arc; - -pub type ProtocolConstructor = dyn Fn(ArcPeer) -> Arc + Send + Sync; +pub type ProtocolConstructor = dyn Fn(Arc) -> Arc + Send + Sync; pub type ProtocolID = String; @@ -38,17 +36,17 @@ impl EventValue for ProtocolEvent { /// use smol::Executor; /// /// use karyon_p2p::{ -/// protocol::{ArcProtocol, Protocol, ProtocolID, ProtocolEvent}, -/// Backend, PeerID, Config, Version, Error, ArcPeer, +/// protocol::{Protocol, ProtocolID, ProtocolEvent}, +/// Backend, PeerID, Config, Version, Error, Peer, /// keypair::{KeyPair, KeyPairType}, /// }; /// /// pub struct NewProtocol { -/// peer: ArcPeer, +/// peer: Arc, /// } /// /// impl NewProtocol { -/// fn new(peer: ArcPeer) -> ArcProtocol { +/// fn new(peer: Arc) -> Arc { /// Arc::new(Self { /// peer, /// }) diff --git a/p2p/src/protocols/ping.rs b/p2p/src/protocols/ping.rs index 654644a..ef1b54e 100644 --- a/p2p/src/protocols/ping.rs +++ b/p2p/src/protocols/ping.rs @@ -16,8 +16,8 @@ use karyon_core::{ use karyon_net::Error as NetError; use crate::{ - peer::ArcPeer, - protocol::{ArcProtocol, Protocol, ProtocolEvent, ProtocolID}, + peer::Peer, + protocol::{Protocol, ProtocolEvent, ProtocolID}, version::Version, Result, }; @@ -31,7 +31,7 @@ enum PingProtocolMsg { } pub struct PingProtocol { - peer: ArcPeer, + peer: Arc, ping_interval: u64, ping_timeout: u64, task_group: TaskGroup, @@ -39,7 +39,7 @@ pub struct PingProtocol { impl PingProtocol { #[allow(clippy::new_ret_no_self)] - pub fn new(peer: ArcPeer, executor: Executor) -> ArcProtocol { + pub fn new(peer: Arc, executor: Executor) -> Arc { let ping_interval = peer.config().ping_interval; let ping_timeout = peer.config().ping_timeout; Arc::new(Self { -- cgit v1.2.3