From 82f1f81760274d5efa58142f1348b1297fb804a2 Mon Sep 17 00:00:00 2001 From: Tangent Wantwight Date: Fri, 19 Jun 2020 00:52:54 -0400 Subject: [PATCH] Adapt the socket in main without having to split it --- src/main.rs | 10 +++------- src/net/server.rs | 4 ++-- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 87133ec..56b830b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -94,17 +94,13 @@ fn serve_file(path: &str) -> Result { } async fn handle_socket(game_server: Handle, websocket: WebSocket) -> Result<()> { - let (sink, source) = websocket.split(); - - let mut sink = sink.with(|msg: ServerMessage| { + let mut websocket = websocket.with(|msg: ServerMessage| { ready( to_string(&msg) .context("JSON encoding shouldn't fail") .map(|json| Message::text(json)), ) - }); - - let mut source = source.map_err(Into::into).try_filter_map(|msg| { + }).map_err(Into::into).try_filter_map(|msg| { ready(match msg.to_str() { Ok(json) => from_str::(json) .context("Parsing JSON") @@ -116,5 +112,5 @@ async fn handle_socket(game_server: Handle, websocket: WebSocket) -> Result<()> }) }); - run_client(game_server, &mut source, &mut sink).await + run_client(game_server, &mut websocket).await } diff --git a/src/net/server.rs b/src/net/server.rs index 1bfd10a..0db53b9 100644 --- a/src/net/server.rs +++ b/src/net/server.rs @@ -204,9 +204,9 @@ impl Server { pub async fn run_client( handle: Handle, - source: &mut (impl Stream> + Send + Unpin), - sink: &mut (impl Sink + Send + Unpin), + socket: &mut (impl Stream> + Sink + Send + Unpin), ) -> Result<()> { + let (sink, mut source) = socket.split(); let (sender, receiver) = channel(CHANNEL_BUFFER); // register player