From adde7650d2755d14d5e40b413b540d9885dd30b8 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Tue, 4 Jun 2024 15:53:45 +0100 Subject: [PATCH] Replace connect_{tcp,unix} with a single function Since config::Address now exists, there's no real need to be generic and accept anything TcpStream::connect and UnixStream::connect accept and so a single function taking config::Address can simplify both call sites. --- src/ext.rs | 13 +++++-------- src/proxy.rs | 9 ++------- src/socketwrapper.rs | 25 +++++++++++++------------ 3 files changed, 20 insertions(+), 27 deletions(-) diff --git a/src/ext.rs b/src/ext.rs index 22c799f..ecb5ba8 100644 --- a/src/ext.rs +++ b/src/ext.rs @@ -4,7 +4,7 @@ use anyhow::{bail, Context, Result}; use serde::de::{DeserializeOwned, IgnoredAny}; use tokio::io::BufReader; -use crate::config::{Address, Config}; +use crate::config::Config; use crate::lsp::ext::{self, LspMuxOptions, StatusResponse}; use crate::lsp::jsonrpc::{Message, Request, RequestId, Version}; use crate::lsp::transport::{LspReader, LspWriter}; @@ -15,13 +15,10 @@ pub async fn ext_request(config: &Config, method: ext::Request) -> Result where T: DeserializeOwned, { - let (reader, writer) = match config.connect { - Address::Tcp(ip_addr, port) => Stream::connect_tcp((ip_addr, port)).await, - #[cfg(target_family = "unix")] - Address::Unix(ref path) => Stream::connect_unix(path).await, - } - .context("connect")? - .into_split(); + let (reader, writer) = Stream::connect(&config.connect) + .await + .context("connect")? + .into_split(); let mut writer = LspWriter::new(writer, "lspmux"); let mut reader = LspReader::new(BufReader::new(reader), "lspmux"); diff --git a/src/proxy.rs b/src/proxy.rs index 318e327..fcc2878 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -4,7 +4,7 @@ use std::env; use anyhow::{bail, Context as _, Result}; use tokio::io::{self, BufStream}; -use crate::config::{Address, Config}; +use crate::config::Config; use crate::lsp::ext::{LspMuxOptions, Request}; use crate::lsp::jsonrpc::Message; use crate::lsp::transport::{LspReader, LspWriter}; @@ -23,12 +23,7 @@ pub async fn run(config: &Config, server: String, args: Vec) -> Result<( } } - let mut stream = match config.connect { - Address::Tcp(ip_addr, port) => Stream::connect_tcp((ip_addr, port)).await, - #[cfg(target_family = "unix")] - Address::Unix(ref path) => Stream::connect_unix(path).await, - } - .context("connect")?; + let mut stream = Stream::connect(&config.connect).await.context("connect")?; let mut stdio = BufStream::new(io::join(io::stdin(), io::stdout())); // Wait for the client to send `initialize` request. diff --git a/src/socketwrapper.rs b/src/socketwrapper.rs index 5c814b9..0252bfa 100644 --- a/src/socketwrapper.rs +++ b/src/socketwrapper.rs @@ -8,10 +8,12 @@ use std::{io, net}; use pin_project_lite::pin_project; use tokio::io::{AsyncRead, AsyncWrite, ReadBuf}; -use tokio::net::{tcp, TcpListener, TcpStream, ToSocketAddrs}; +use tokio::net::{tcp, TcpListener, TcpStream}; #[cfg(target_family = "unix")] use tokio::net::{unix, UnixListener, UnixStream}; +use crate::config::Address; + pub enum SocketAddr { Ip(net::SocketAddr), #[cfg(target_family = "unix")] @@ -137,17 +139,16 @@ pin_project! { } impl Stream { - pub async fn connect_tcp(addr: A) -> io::Result { - Ok(Stream::Tcp { - tcp: TcpStream::connect(addr).await?, - }) - } - - #[cfg(target_family = "unix")] - pub async fn connect_unix>(addr: P) -> io::Result { - Ok(Stream::Unix { - unix: UnixStream::connect(addr).await?, - }) + pub async fn connect(addr: &Address) -> io::Result { + match addr { + Address::Tcp(ip_addr, port) => Ok(Stream::Tcp { + tcp: TcpStream::connect((*ip_addr, *port)).await?, + }), + #[cfg(target_family = "unix")] + Address::Unix(path) => Ok(Stream::Unix { + unix: UnixStream::connect(path).await?, + }), + } } pub fn into_split(self) -> (OwnedReadHalf, OwnedWriteHalf) {