Remove disconnect method from Connection

Use Drop implementation instead
This commit is contained in:
Patrick Auernig 2018-12-06 00:49:23 +01:00
parent caaae615f0
commit ecd391a44e
3 changed files with 8 additions and 12 deletions

View File

@ -38,8 +38,6 @@ pub trait Connection: Sized {
fn connect() -> Result<Self>;
fn disconnect(&self) -> Result<()>;
fn socket_path(n: u8) -> PathBuf {
Self::ipc_path().join(format!("discord-ipc-{}", n))
}

View File

@ -20,17 +20,12 @@ impl Connection for UnixConnection {
fn connect() -> Result<Self> {
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");
}
}

View File

@ -23,10 +23,6 @@ impl Connection for WindowsConnection {
Ok(Self { socket })
}
fn disconnect(&self) -> Result<()> {
Ok(())
}
fn ipc_path() -> PathBuf {
PathBuf::from(r"\\.\pipe\")
}