Simplify advance and reset functions with mem::replace.

This commit is contained in:
Nick Mathewson 2022-05-19 08:09:05 -04:00
parent eab0046d0e
commit 5b5b4bbbea
1 changed files with 4 additions and 8 deletions

View File

@ -594,18 +594,14 @@ pub(crate) async fn download<R: Runtime>(
/// Replace `state` with `state.reset()`. /// Replace `state` with `state.reset()`.
fn reset(state: &mut Box<dyn DirState>) { fn reset(state: &mut Box<dyn DirState>) {
let mut this_state: Box<dyn DirState> = Box::new(PoisonedState); let cur_state = std::mem::replace(state, Box::new(PoisonedState));
std::mem::swap(&mut this_state, state); *state = cur_state.reset();
this_state = this_state.reset();
std::mem::swap(&mut this_state, state);
} }
/// Replace `state` with `state.advance()`. /// Replace `state` with `state.advance()`.
fn advance(state: &mut Box<dyn DirState>) { fn advance(state: &mut Box<dyn DirState>) {
let mut this_state: Box<dyn DirState> = Box::new(PoisonedState); let cur_state = std::mem::replace(state, Box::new(PoisonedState));
std::mem::swap(&mut this_state, state); *state = cur_state.advance();
this_state = this_state.advance();
std::mem::swap(&mut this_state, state);
} }
/// Helper: Clamp `v` so that it is no more than one week from `now`. /// Helper: Clamp `v` so that it is no more than one week from `now`.