From 923b222b5c98132215b37877b91c3219d15489bd Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Sun, 20 Mar 2022 20:36:45 -0700 Subject: [PATCH] Make sure with_style() and set_style() behave the same In #395, we made it so that message and prefix are carried over when calling ProgressBar::with_style(), but this did not change ProgressBar::set_style(). Reuse the same logic by having with_style() call set_style(), so that these cannot fall out of sync again. --- src/progress_bar.rs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/progress_bar.rs b/src/progress_bar.rs index 9b8dfd82..9652eae4 100644 --- a/src/progress_bar.rs +++ b/src/progress_bar.rs @@ -60,13 +60,8 @@ impl ProgressBar { } /// A convenience builder-like function for a progress bar with a given style - pub fn with_style(self, mut style: ProgressStyle) -> ProgressBar { - let mut state = self.state(); - mem::swap(&mut state.style.message, &mut style.message); - mem::swap(&mut state.style.prefix, &mut style.prefix); - state.style = style; - drop(state); - + pub fn with_style(self, style: ProgressStyle) -> ProgressBar { + self.set_style(style); self } @@ -122,8 +117,11 @@ impl ProgressBar { /// Overrides the stored style /// /// This does not redraw the bar. Call [`ProgressBar::tick()`] to force it. - pub fn set_style(&self, style: ProgressStyle) { - self.state().style = style; + pub fn set_style(&self, mut style: ProgressStyle) { + let mut state = self.state(); + mem::swap(&mut state.style.message, &mut style.message); + mem::swap(&mut state.style.prefix, &mut style.prefix); + state.style = style; } /// Spawns a background thread to tick the progress bar