From 896b31f02f67fe5c02b7fbe5fba08c9f9f34c023 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Fri, 1 May 2020 18:02:50 +0200 Subject: take readers by value The API guidelines for Rust state that readers should be taken by value[1]. If the caller wants to re-use the reader, they have to borrow it. This patch adjusts the parsing functions to do just that. [1]: https://rust-lang.github.io/api-guidelines/interoperability.html#generic-readerwriter-functions-take-r-read-and-w-write-by-value-c-rw-value --- src/raw/mod.rs | 2 +- src/raw/parser.rs | 42 +++++++++++++++++++++--------------------- 2 files changed, 22 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/raw/mod.rs b/src/raw/mod.rs index 88b58ac..77a8571 100644 --- a/src/raw/mod.rs +++ b/src/raw/mod.rs @@ -21,7 +21,7 @@ use std::ffi::CStr; use std::io::{BufReader, Read, Seek}; /// Parse a complete log that was compressed as a zip file. -pub fn parse_zip(input: &mut T) -> ParseResult { +pub fn parse_zip(input: R) -> ParseResult { let mut archive = ZipArchive::new(input)?; let mut file = BufReader::new(archive.by_index(0)?); parse_file(&mut file) diff --git a/src/raw/parser.rs b/src/raw/parser.rs index 5544fd2..a9d8e8a 100644 --- a/src/raw/parser.rs +++ b/src/raw/parser.rs @@ -132,7 +132,7 @@ pub type ParseResult = Result; /// It is expected that the file cursor is at the very first byte of the file. /// /// * `input` - Input stream. -pub fn parse_header(input: &mut T) -> ParseResult
{ +pub fn parse_header(mut input: R) -> ParseResult
{ // Make sure the magic number matches let mut magic_number = [0; 4]; input.read_exact(&mut magic_number)?; @@ -178,10 +178,10 @@ pub fn parse_header(input: &mut T) -> ParseResult
{ /// /// * `input` - Input stream. /// * `count` - Number of agents (found in the header). -pub fn parse_agents(input: &mut T, count: u32) -> ParseResult> { +pub fn parse_agents(mut input: R, count: u32) -> ParseResult> { let mut result = Vec::with_capacity(count as usize); for _ in 0..count { - result.push(parse_agent(input)?); + result.push(parse_agent(&mut input)?); } Ok(result) } @@ -189,7 +189,7 @@ pub fn parse_agents(input: &mut T, count: u32) -> ParseResult(input: &mut T) -> ParseResult { +pub fn parse_agent(mut input: R) -> ParseResult { let addr = input.read_u64::()?; let prof = input.read_u32::()?; let is_elite = input.read_u32::()?; @@ -226,10 +226,10 @@ pub fn parse_agent(input: &mut T) -> ParseResult { /// /// * `input` - Input stream. /// * `count` - Number of skills to parse. -pub fn parse_skills(input: &mut T, count: u32) -> ParseResult> { +pub fn parse_skills(mut input: R, count: u32) -> ParseResult> { let mut result = Vec::with_capacity(count as usize); for _ in 0..count { - result.push(parse_skill(input)?); + result.push(parse_skill(&mut input)?); } Ok(result) } @@ -237,7 +237,7 @@ pub fn parse_skills(input: &mut T, count: u32) -> ParseResult(input: &mut T) -> ParseResult { +pub fn parse_skill(mut input: R) -> ParseResult { let id = input.read_i32::()?; let mut name = [0; 64]; input.read_exact(&mut name)?; @@ -248,13 +248,13 @@ pub fn parse_skill(input: &mut T) -> ParseResult { /// /// * `input` - Input stream. /// * `parser` - The parse function to use. -pub fn parse_events( - input: &mut T, - parser: fn(&mut T) -> ParseResult, +pub fn parse_events( + mut input: R, + parser: fn(&mut R) -> ParseResult, ) -> ParseResult> { let mut result = Vec::new(); loop { - let event = parser(input); + let event = parser(&mut input); match event { Ok(x) => result.push(x), Err(ParseError::Io(ref e)) if e.kind() == ErrorKind::UnexpectedEof => { @@ -270,7 +270,7 @@ pub fn parse_events( /// This works for old combat events, i.e. files with revision == 0. /// /// * `input` - Input stream. -pub fn parse_event_rev0(input: &mut T) -> ParseResult { +pub fn parse_event_rev0(mut input: R) -> ParseResult { let time = input.read_u64::()?; let src_agent = input.read_u64::()?; let dst_agent = input.read_u64::()?; @@ -334,7 +334,7 @@ pub fn parse_event_rev0(input: &mut T) -> ParseResult { /// This works for new combat events, i.e. files with revision == 1. /// /// * `input` - Input stream. -pub fn parse_event_rev1(input: &mut T) -> ParseResult { +pub fn parse_event_rev1(mut input: R) -> ParseResult { let time = input.read_u64::()?; let src_agent = input.read_u64::()?; let dst_agent = input.read_u64::()?; @@ -394,9 +394,9 @@ pub fn parse_event_rev1(input: &mut T) -> ParseResult { /// Parse a partial EVTC file. /// /// * `input` - Input stream. -pub fn parse_partial_file(input: &mut T) -> ParseResult { - let header = parse_header(input)?; - let agents = parse_agents(input, header.agent_count)?; +pub fn parse_partial_file(mut input: R) -> ParseResult { + let header = parse_header(&mut input)?; + let agents = parse_agents(&mut input, header.agent_count)?; let skill_count = input.read_u32::()?; let skills = parse_skills(input, skill_count)?; @@ -412,10 +412,10 @@ pub fn parse_partial_file(input: &mut T) -> ParseResult { /// /// * `partial` - The partial EVTC. /// * `input` - The input stream. -pub fn finish_parsing(partial: PartialEvtc, input: &mut T) -> ParseResult { +pub fn finish_parsing(partial: PartialEvtc, input: R) -> ParseResult { let events = match partial.header.revision { - 0 => parse_events(input, parse_event_rev0)?, - 1 => parse_events(input, parse_event_rev1)?, + 0 => parse_events(input, |r| parse_event_rev0(r))?, + 1 => parse_events(input, |r| parse_event_rev1(r))?, x => return Err(ParseError::UnknownRevision(x)), }; @@ -431,7 +431,7 @@ pub fn finish_parsing(partial: PartialEvtc, input: &mut T) -> ParseResu /// Parse a complete EVTC file. /// /// * `input` - Input stream. -pub fn parse_file(input: &mut T) -> ParseResult { - let partial = parse_partial_file(input)?; +pub fn parse_file(mut input: R) -> ParseResult { + let partial = parse_partial_file(&mut input)?; finish_parsing(partial, input) } -- cgit v1.2.3