Skip to content

Commit

Permalink
reduce public api surface of ConnectionTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
icewind1991 committed Sep 28, 2024
1 parent 5320647 commit 48f9f55
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
21 changes: 15 additions & 6 deletions src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::serverlist::ServerList;
use crate::service_method::ServiceMethodRequest;
use crate::session::{anonymous, hello, login, ConnectionError, Session};
use crate::transport::websocket::connect;
pub(crate) use connection_impl::ConnectionImpl;
pub use filter::MessageFilter;
use futures_util::future::{select, Either};
use futures_util::{FutureExt, Sink, SinkExt};
Expand Down Expand Up @@ -224,12 +225,18 @@ impl Connection {
}
}

pub trait ConnectionTrait: Sync + Debug {
fn timeout(&self) -> Duration;
fn filter(&self) -> &MessageFilter;
fn session(&self) -> &Session;
fn sender(&self) -> &MessageSender;
pub(crate) mod connection_impl {
use super::*;

pub trait ConnectionImpl: Sync + Debug {
fn timeout(&self) -> Duration;
fn filter(&self) -> &MessageFilter;
fn session(&self) -> &Session;
fn sender(&self) -> &MessageSender;
}
}

pub trait ConnectionTrait: ConnectionImpl {
fn on_notification<T: ServiceMethodRequest>(&self) -> impl Stream<Item = Result<T>> + 'static {
BroadcastStream::new(self.filter().on_notification(T::REQ_NAME))
.filter_map(|res| res.ok())
Expand Down Expand Up @@ -375,7 +382,7 @@ pub trait ConnectionTrait: Sync + Debug {
}
}

impl ConnectionTrait for Connection {
impl ConnectionImpl for Connection {
fn timeout(&self) -> Duration {
self.timeout
}
Expand All @@ -392,3 +399,5 @@ impl ConnectionTrait for Connection {
&self.sender
}
}

impl ConnectionTrait for Connection {}
6 changes: 4 additions & 2 deletions src/game_coordinator.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::connection::{ConnectionTrait, MessageFilter, MessageSender};
use crate::connection::{ConnectionImpl, ConnectionTrait, MessageFilter, MessageSender};
use crate::net::{decode_kind, NetMessageHeader, RawNetMessage};
use crate::session::Session;
use crate::{Connection, NetMessage, NetworkError};
Expand Down Expand Up @@ -148,7 +148,7 @@ impl GameCoordinator {
}
}

impl ConnectionTrait for GameCoordinator {
impl ConnectionImpl for GameCoordinator {
fn timeout(&self) -> Duration {
self.timeout
}
Expand All @@ -164,7 +164,9 @@ impl ConnectionTrait for GameCoordinator {
fn sender(&self) -> &MessageSender {
&self.sender
}
}

impl ConnectionTrait for GameCoordinator {
async fn raw_send_with_kind<Msg: NetMessage, K: MsgKindEnum>(
&self,
header: NetMessageHeader,
Expand Down

0 comments on commit 48f9f55

Please sign in to comment.