From ecd391a44ee12385e68873c7525e5c44d63b3c11 Mon Sep 17 00:00:00 2001 From: Patrick Auernig Date: Thu, 6 Dec 2018 00:49:23 +0100 Subject: [PATCH] Remove disconnect method from Connection Use Drop implementation instead --- src/connection/base.rs | 2 -- src/connection/unix.rs | 14 ++++++++------ src/connection/windows.rs | 4 ---- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/connection/base.rs b/src/connection/base.rs index 59641bd..bc862ba 100644 --- a/src/connection/base.rs +++ b/src/connection/base.rs @@ -38,8 +38,6 @@ pub trait Connection: Sized { fn connect() -> Result; - fn disconnect(&self) -> Result<()>; - fn socket_path(n: u8) -> PathBuf { Self::ipc_path().join(format!("discord-ipc-{}", n)) } diff --git a/src/connection/unix.rs b/src/connection/unix.rs index 121dc9c..a8fd5fb 100644 --- a/src/connection/unix.rs +++ b/src/connection/unix.rs @@ -20,17 +20,12 @@ impl Connection for UnixConnection { fn connect() -> Result { let connection_name = Self::socket_path(0); let socket = UnixStream::connect(connection_name)?; - // socket.set_nonblocking(true)?; + socket.set_nonblocking(true)?; socket.set_write_timeout(Some(time::Duration::from_secs(30)))?; socket.set_read_timeout(Some(time::Duration::from_secs(30)))?; Ok(Self { socket }) } - fn disconnect(&self) -> Result<()> { - self.socket.shutdown(Shutdown::Both)?; - Ok(()) - } - fn ipc_path() -> PathBuf { let tmp = env::var("XDG_RUNTIME_DIR") .or_else(|_| env::var("TMPDIR")) @@ -48,3 +43,10 @@ impl Connection for UnixConnection { &mut self.socket } } + +impl Drop for UnixConnection { + fn drop(&mut self) { + self.socket.shutdown(Shutdown::Both) + .expect("Failed to properly shut down socket"); + } +} diff --git a/src/connection/windows.rs b/src/connection/windows.rs index 0362d79..9cc81be 100644 --- a/src/connection/windows.rs +++ b/src/connection/windows.rs @@ -23,10 +23,6 @@ impl Connection for WindowsConnection { Ok(Self { socket }) } - fn disconnect(&self) -> Result<()> { - Ok(()) - } - fn ipc_path() -> PathBuf { PathBuf::from(r"\\.\pipe\") }