Skip to content

Fix clippy::new_ret_no_self #287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions process/fuser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,7 @@ mod linux {

Ok(())
}

/// Checks a directory within a process's `/proc` entry for matching devices and inodes,
/// and updates the `Names` object with relevant process information.
///
Expand All @@ -629,6 +630,7 @@ mod linux {
/// # Returns
///
/// Returns `Ok(())` on success.
#[allow(clippy::too_many_arguments)]
fn check_dir(
names: &mut Names,
pid: i32,
Expand Down Expand Up @@ -695,6 +697,7 @@ mod linux {

Ok(())
}

/// Checks the memory map of a process for matching devices and updates the `Names` object.
///
/// # Arguments
Expand Down
57 changes: 31 additions & 26 deletions users/talk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,35 +199,38 @@ pub struct Osockaddr {
pub sa_data: [u8; 14],
}

impl Osockaddr {
// Converts the packed address structure into a SocketAddrV4
pub fn to_socketaddr(&self) -> Option<SocketAddrV4> {
impl From<&Osockaddr> for SocketAddrV4 {
fn from(value: &Osockaddr) -> Self {
// Extract the port
let port = u16::from_be_bytes([self.sa_data[0], self.sa_data[1]]);
let port = u16::from_be_bytes([value.sa_data[0], value.sa_data[1]]);

// Extract the IP address
let ip = Ipv4Addr::new(
self.sa_data[2],
self.sa_data[3],
self.sa_data[4],
self.sa_data[5],
value.sa_data[2],
value.sa_data[3],
value.sa_data[4],
value.sa_data[5],
);

Some(SocketAddrV4::new(ip, port))
Self::new(ip, port)
}
}

impl From<&SocketAddr> for Osockaddr {
fn from(value: &SocketAddr) -> Self {
match value {
SocketAddr::V4(v) => Self::from(v),
SocketAddr::V6(_) => unimplemented!(),
}
}
}

impl From<&SocketAddrV4> for Osockaddr {
fn from(value: &SocketAddrV4) -> Self {
let ip = value.ip().to_string();
let port = value.port();
// TODO use as.bytes()

/// Creates a new `sa_data` array from the given IP address and port.
///
/// # Arguments
///
/// * `ip` - A string slice representing the IP address (e.g., "192.168.1.1").
/// * `port` - A u16 representing the port number.
///
/// # Returns
///
/// Returns an `Option<[u8; 14]>` containing the packed address if successful,
/// or `None` if the IP address format is invalid.
fn new(ip: &str, port: u16) -> [u8; 14] {
let mut sa_data: [u8; 14] = [0; 14];

let ip_segments: Result<Vec<u8>, _> = ip.split('.').map(|s| s.parse::<u8>()).collect();
Expand All @@ -243,7 +246,10 @@ impl Osockaddr {
}
}

sa_data
Self {
sa_family: 0, // TODO use enum
sa_data,
}
}
}

Expand Down Expand Up @@ -458,9 +464,7 @@ fn handle_existing_invitation(
output_buffer: &mut String,
res: &mut CtlRes,
) -> Result<(), TalkError> {
let tcp_addr = res.addr.to_socketaddr().ok_or_else(|| {
TalkError::AddressResolutionFailed("Failed to convert address to socket address.".into())
})?;
let tcp_addr = SocketAddrV4::from(&res.addr);

// Establish a TCP connection to the `tcp_addr`. Map any IO errors to `TalkError::IoError`.
let stream = TcpStream::connect(tcp_addr).map_err(TalkError::IoError)?;
Expand Down Expand Up @@ -903,6 +907,7 @@ fn send_byte(write_stream: &TcpStream, byte: u8) -> Result<(), io::Error> {
/// # Returns
///
/// A `Result` indicating success or a `TalkError`.
#[allow(clippy::too_many_arguments)]
fn handle_new_invitation(
talkd_addr: SocketAddr,
daemon_port: u16,
Expand All @@ -919,7 +924,7 @@ fn handle_new_invitation(
logger.set_state("[Service connection established.]");

// Create the socket address data and set it in the `msg`.
let tcp_data = Osockaddr::new(&socket_addr.ip().to_string(), socket_addr.port());
let tcp_data = Osockaddr::from(&socket_addr).sa_data;

logger.set_state("[Waiting for your party to respond]");

Expand Down