Skip to content

Commit

Permalink
Update logic to get socket path
Browse files Browse the repository at this point in the history
* Have same fallbacks as the official client
* Reduce number of allocations
  • Loading branch information
Bond-009 committed Oct 22, 2024
1 parent ba2283d commit dd8c61a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
6 changes: 4 additions & 2 deletions src/connection/base.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
io::{Write, Read, ErrorKind},
io::{ErrorKind, Read, Write},
marker::Sized,
path::PathBuf,
thread,
Expand Down Expand Up @@ -44,7 +44,9 @@ pub trait Connection: Sized {

/// The full socket path.
fn socket_path(n: u8) -> PathBuf {
Self::ipc_path().join(format!("discord-ipc-{}", n))
let mut path = Self::ipc_path();
path.push(format!("discord-ipc-{}", n));
path
}

/// Perform a handshake on this socket connection.
Expand Down
14 changes: 5 additions & 9 deletions src/connection/unix.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::{
time,
path::PathBuf,
env,
os::unix::net::UnixStream,
net::Shutdown,
os::unix::net::UnixStream,
path::PathBuf,
time
};
use super::base::Connection;
use crate::Result;
Expand All @@ -28,12 +28,8 @@ impl Connection for UnixConnection {
fn ipc_path() -> PathBuf {
let tmp = env::var("XDG_RUNTIME_DIR")
.or_else(|_| env::var("TMPDIR"))
.or_else(|_| {
match env::temp_dir().to_str() {
None => Err("Failed to convert temp_dir"),
Some(tmp) => Ok(tmp.to_owned())
}
})
.or_else(|_| env::var("TMP"))
.or_else(|_| env::var("TEMP"))
.unwrap_or_else(|_| "/tmp".to_owned());
PathBuf::from(tmp)
}
Expand Down

0 comments on commit dd8c61a

Please sign in to comment.