Skip to content

Commit

Permalink
chore: replace mem::swap on Option with Option::take
Browse files Browse the repository at this point in the history
  • Loading branch information
mobiusklein committed Oct 17, 2024
1 parent 93c19a9 commit 710653a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 25 deletions.
4 changes: 1 addition & 3 deletions src/io/compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ impl<R: BufRead + Seek> RestartableGzDecoder<R> {
}

fn reset(&mut self) -> io::Result<u64> {
let mut handle = None;
swap(&mut self.handle, &mut handle);
let handle = handle.unwrap();
let handle = self.handle.take().unwrap();
let mut inner = handle.into_inner();
let res = inner.seek(io::SeekFrom::Start(0));
self.handle = Some(MultiGzDecoder::new(inner));
Expand Down
16 changes: 6 additions & 10 deletions src/io/mzml/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,7 @@ impl<
match self.state {
MzMLParserState::SpectrumDone => Ok(()),
MzMLParserState::ParserError => {
let mut error = None;
mem::swap(&mut error, &mut self.error);
Err(error.unwrap())
Err(self.error.take().unwrap_or_else(|| MzMLParserError::UnknownError(MzMLParserState::ParserError)))
}
_ => Err(MzMLParserError::IncompleteSpectrum),
}
Expand All @@ -275,7 +273,7 @@ impl<
Ok(state) => {
match state {
MzMLParserState::ParserError => {
eprintln!(
warn!(
"Encountered an error while starting {:?}",
String::from_utf8_lossy(&self.buffer)
);
Expand Down Expand Up @@ -369,12 +367,10 @@ impl<
match self.state {
MzMLParserState::SpectrumDone => Ok((offset, accumulator)),
MzMLParserState::ParserError if self.error.is_some() => {
let mut error = None;
mem::swap(&mut error, &mut self.error);
Err(error.unwrap())
Err(self.error.take().unwrap())
}
MzMLParserState::ParserError if self.error.is_none() => {
eprintln!(
warn!(
"Terminated with ParserError but no error set: {:?}",
self.error
);
Expand All @@ -397,10 +393,10 @@ impl<
self.state = MzMLParserState::Resume;
},
MzMLParserState::ParserError => {
eprintln!("Starting parsing from error: {:?}", self.error);
warn!("Starting parsing from error: {:?}", self.error);
}
state if state > MzMLParserState::SpectrumDone => {
eprintln!("Attempting to start parsing a spectrum in state {}", self.state);
warn!("Attempting to start parsing a spectrum in state {}", self.state);
}
_ => {}
}
Expand Down
20 changes: 8 additions & 12 deletions src/io/mzml/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1208,9 +1208,7 @@ impl<
match self.state {
MzMLParserState::SpectrumDone | MzMLParserState::ChromatogramDone => Ok(()),
MzMLParserState::ParserError => {
let mut error = None;
mem::swap(&mut error, &mut self.error);
Err(error.unwrap())
Err(self.error.take().unwrap_or_else(|| MzMLParserError::UnknownError(MzMLParserState::ParserError)))
}
_ => Err(MzMLParserError::IncompleteSpectrum),
}
Expand Down Expand Up @@ -1243,7 +1241,7 @@ impl<
Ok(state) => {
self.state = state;
if state == MzMLParserState::ParserError {
eprintln!(
warn!(
"Encountered an error while starting {:?}",
String::from_utf8_lossy(&self.buffer)
);
Expand Down Expand Up @@ -1341,12 +1339,10 @@ impl<
Ok((accumulator, offset))
}
MzMLParserState::ParserError if self.error.is_some() => {
let mut error = None;
mem::swap(&mut error, &mut self.error);
Err(error.unwrap())
Err(self.error.take().unwrap())
}
MzMLParserState::ParserError if self.error.is_none() => {
eprintln!(
warn!(
"Terminated with ParserError but no error set: {:?}",
self.error
);
Expand Down Expand Up @@ -1411,13 +1407,13 @@ impl<
self.state = MzMLParserState::Resume;
}
MzMLParserState::ParserError => {
eprintln!("Starting parsing from error: {:?}", self.error);
warn!("Starting parsing from error: {:?}", self.error);
}
state
if state > MzMLParserState::ChromatogramDone
&& state < MzMLParserState::Chromatogram =>
{
eprintln!(
warn!(
"Attempting to start parsing a spectrum in state {}",
self.state
);
Expand Down Expand Up @@ -1877,7 +1873,7 @@ impl<
b"id" => {
let scan_id = attr
.unescape_value()
.expect("Error decoding id")
.expect("Error decoding spectrum id in streaming mzML index")
.to_string();
// This count is off by 2 because somehow the < and > bytes are removed?
self.spectrum_index.insert(
Expand Down Expand Up @@ -2393,7 +2389,7 @@ mod test {
}

#[cfg(feature = "mzsignal")]
#[test]
#[test_log::test]
fn test_averaging() -> io::Result<()> {
use crate::spectrum::group::SpectrumAveragingIterator;

Expand Down

0 comments on commit 710653a

Please sign in to comment.