From 5b5b4bbbea7217969509dd3a20318744ecf247cd Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Thu, 19 May 2022 08:09:05 -0400 Subject: [PATCH] Simplify advance and reset functions with mem::replace. --- crates/tor-dirmgr/src/bootstrap.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/crates/tor-dirmgr/src/bootstrap.rs b/crates/tor-dirmgr/src/bootstrap.rs index 009fb9d50..4d120f40a 100644 --- a/crates/tor-dirmgr/src/bootstrap.rs +++ b/crates/tor-dirmgr/src/bootstrap.rs @@ -594,18 +594,14 @@ pub(crate) async fn download( /// Replace `state` with `state.reset()`. fn reset(state: &mut Box) { - let mut this_state: Box = Box::new(PoisonedState); - std::mem::swap(&mut this_state, state); - this_state = this_state.reset(); - std::mem::swap(&mut this_state, state); + let cur_state = std::mem::replace(state, Box::new(PoisonedState)); + *state = cur_state.reset(); } /// Replace `state` with `state.advance()`. fn advance(state: &mut Box) { - let mut this_state: Box = Box::new(PoisonedState); - std::mem::swap(&mut this_state, state); - this_state = this_state.advance(); - std::mem::swap(&mut this_state, state); + let cur_state = std::mem::replace(state, Box::new(PoisonedState)); + *state = cur_state.advance(); } /// Helper: Clamp `v` so that it is no more than one week from `now`.