From 6c7e6c73a7bafe4ee90e145e4c067f4658e9a827 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Fri, 23 Oct 2020 15:09:43 +0200 Subject: use iterator methods instead of for loops No particular reason other than it's more idiomatic and shorter. inline has been added to get_agent_by_addr since it "feels" like a short function that can and should be inlined, but it doesn't matter too much. --- src/processing.rs | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/src/processing.rs b/src/processing.rs index 11bdda7..3f72b39 100644 --- a/src/processing.rs +++ b/src/processing.rs @@ -100,21 +100,12 @@ pub fn process_file>(path: P, compression: Compression) -> Result } fn setup_agents(data: &raw::Evtc) -> Result, EvtcError> { - let mut agents = Vec::with_capacity(data.agents.len()); - - for raw_agent in &data.agents { - agents.push(Agent::try_from(raw_agent)?); - } - Ok(agents) + data.agents.iter().map(Agent::try_from).collect() } +#[inline] fn get_agent_by_addr(agents: &mut [Agent], addr: u64) -> Option<&mut Agent> { - for agent in agents { - if agent.addr() == addr { - return Some(agent); - } - } - None + agents.iter_mut().find(|agent| agent.addr() == addr) } fn set_agent_awares(data: &raw::Evtc, agents: &mut [Agent]) -> Result<(), EvtcError> { -- cgit v1.2.3