From 0c562689be86c23f1abd68e5d74dc84aa7eafc01 Mon Sep 17 00:00:00 2001 From: Kevin Phoenix Date: Thu, 18 Jan 2024 13:12:02 -0700 Subject: [PATCH 1/2] Fix lint issues reported by clippy --- crates/bh_agent_server/src/main.rs | 2 +- crates/bh_agent_server/src/state.rs | 41 +++++++++++++++-------------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/crates/bh_agent_server/src/main.rs b/crates/bh_agent_server/src/main.rs index 90a0508..a195864 100644 --- a/crates/bh_agent_server/src/main.rs +++ b/crates/bh_agent_server/src/main.rs @@ -1,4 +1,4 @@ -use std::{any, net::IpAddr}; +use std::net::IpAddr; use clap::Parser; use futures::{future, prelude::*}; diff --git a/crates/bh_agent_server/src/state.rs b/crates/bh_agent_server/src/state.rs index be1ee01..d5de36b 100644 --- a/crates/bh_agent_server/src/state.rs +++ b/crates/bh_agent_server/src/state.rs @@ -70,19 +70,19 @@ impl BhAgentState { Ok(modes.contains( self.file_modes .read()? - .get(&fd) + .get(fd) .ok_or(InvalidFileDescriptor)?, )) } pub fn file_type(&self, fd: &FileId) -> Result { trace!("Getting file type for {}", fd); - Ok(self + self .file_types .read()? - .get(&fd) + .get(fd) .ok_or(InvalidFileDescriptor) - .and_then(|t| Ok(t.clone()))?) + .map(|t| *t) } pub fn open_path( @@ -148,7 +148,7 @@ impl BhAgentState { config .argv .iter() - .map(|s| OsStr::new(s)) + .map(OsStr::new) .collect::>() .as_slice(), popenconfig, @@ -215,13 +215,13 @@ impl BhAgentState { channel_ids .read()? - .get_by_left(&proc_id) - .map(|i| i.clone()) - .ok_or((|| { + .get_by_left(proc_id) + .copied() + .ok_or({ debug!("Failed to get process channel"); debug!("Process ID: {}", proc_id); debug!("Channel: {:?}", channel); - let proc_is_valid = self.processes.read().unwrap().contains_key(&proc_id); + let proc_is_valid = self.processes.read().unwrap().contains_key(proc_id); debug!("Process is valid: {}", proc_is_valid); debug!( "Process with valid channels: {:?}", @@ -229,7 +229,7 @@ impl BhAgentState { ); InvalidProcessId - })()) + }) } pub fn process_poll(&self, proc_id: &ProcessId) -> Result, AgentError> { @@ -237,14 +237,14 @@ impl BhAgentState { let proc = self .processes .read()? - .get(&proc_id) + .get(proc_id) .ok_or(InvalidProcessId)? .clone(); let exit_status = proc.write()?.poll(); match exit_status { None => Ok(None), Some(status) => match status { - subprocess::ExitStatus::Exited(code) => Ok(Some(code as u32)), + subprocess::ExitStatus::Exited(code) => Ok(Some(code)), subprocess::ExitStatus::Signaled(code) => Ok(Some(code as u32)), subprocess::ExitStatus::Other(code) => Ok(Some(code as u32)), subprocess::ExitStatus::Undetermined => Err(Unknown), @@ -280,7 +280,7 @@ impl BhAgentState { let proc = self .processes .read()? - .get(&proc_id) + .get(proc_id) .ok_or(InvalidProcessId)? .clone(); let exit_status = proc.read()?.exit_status(); @@ -297,16 +297,17 @@ impl BhAgentState { pub fn close_file(&self, fd: &FileId) -> Result<(), AgentError> { trace!("Closing file {}", fd); - Ok(drop( + drop( self.files .write()? - .remove(&fd) + .remove(fd) .ok_or(InvalidFileDescriptor)?, - )) + ); + Ok(()) } pub fn is_file_closed(&self, fd: &FileId) -> Result { - Ok(self.files.read()?.contains_key(&fd)) + Ok(self.files.read()?.contains_key(fd)) } pub fn do_mut_operation( @@ -324,7 +325,7 @@ impl BhAgentState { } // If these unwraps fail, the state is bad - if let Some(pid) = self.proc_stdin_ids.read()?.get_by_right(&fd) { + if let Some(pid) = self.proc_stdin_ids.read()?.get_by_right(fd) { let procs_binding = self.processes.read()?; let mut proc_binding = procs_binding.get(pid).unwrap().write()?; let file = proc_binding.stdin.as_mut().unwrap(); @@ -332,7 +333,7 @@ impl BhAgentState { } else { trace!("Process stdin id map: {:?}", self.proc_stdin_ids.read()?); } - if let Some(pid) = self.proc_stdout_ids.read()?.get_by_right(&fd) { + if let Some(pid) = self.proc_stdout_ids.read()?.get_by_right(fd) { let procs_binding = self.processes.read()?; let mut proc_binding = procs_binding.get(pid).unwrap().write()?; let file = proc_binding.stdout.as_mut().unwrap(); @@ -340,7 +341,7 @@ impl BhAgentState { } else { trace!("Process stdout id map: {:?}", self.proc_stdout_ids.read()?); } - if let Some(pid) = self.proc_stderr_ids.read()?.get_by_right(&fd) { + if let Some(pid) = self.proc_stderr_ids.read()?.get_by_right(fd) { let procs_binding = self.processes.read()?; let mut proc_binding = procs_binding.get(pid).unwrap().write()?; let file = proc_binding.stderr.as_mut().unwrap(); From 45b7040f1fb0336e24aa78f2e369126307001fa0 Mon Sep 17 00:00:00 2001 From: Kevin Phoenix Date: Thu, 18 Jan 2024 13:15:42 -0700 Subject: [PATCH 2/2] cargo fmt --- crates/bh_agent_server/src/state.rs | 31 ++++++++++++----------------- 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/crates/bh_agent_server/src/state.rs b/crates/bh_agent_server/src/state.rs index d5de36b..6dda211 100644 --- a/crates/bh_agent_server/src/state.rs +++ b/crates/bh_agent_server/src/state.rs @@ -77,8 +77,7 @@ impl BhAgentState { pub fn file_type(&self, fd: &FileId) -> Result { trace!("Getting file type for {}", fd); - self - .file_types + self.file_types .read()? .get(fd) .ok_or(InvalidFileDescriptor) @@ -213,23 +212,19 @@ impl BhAgentState { ProcessChannel::Stderr => &self.proc_stderr_ids, }; - channel_ids - .read()? - .get_by_left(proc_id) - .copied() - .ok_or({ - debug!("Failed to get process channel"); - debug!("Process ID: {}", proc_id); - debug!("Channel: {:?}", channel); - let proc_is_valid = self.processes.read().unwrap().contains_key(proc_id); - debug!("Process is valid: {}", proc_is_valid); - debug!( - "Process with valid channels: {:?}", - channel_ids.read().unwrap().left_values() - ); + channel_ids.read()?.get_by_left(proc_id).copied().ok_or({ + debug!("Failed to get process channel"); + debug!("Process ID: {}", proc_id); + debug!("Channel: {:?}", channel); + let proc_is_valid = self.processes.read().unwrap().contains_key(proc_id); + debug!("Process is valid: {}", proc_is_valid); + debug!( + "Process with valid channels: {:?}", + channel_ids.read().unwrap().left_values() + ); - InvalidProcessId - }) + InvalidProcessId + }) } pub fn process_poll(&self, proc_id: &ProcessId) -> Result, AgentError> {