Skip to content

Commit 4d19347

Browse files
beebfadeevab
authored andcommitted
perf(theme): replace Mutex with RwLock
1 parent bfb67d9 commit 4d19347

9 files changed

+33
-33
lines changed

src/confirm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl PromptInteraction<bool> for Confirm {
7474
}
7575

7676
fn render(&mut self, state: &State<bool>) -> String {
77-
let theme = THEME.lock().unwrap();
77+
let theme = THEME.read().unwrap();
7878
let line1 = theme.format_header(&state.into(), &self.prompt);
7979
let line2 = theme.format_confirm(&state.into(), self.input);
8080
let line3 = theme.format_footer(&state.into());

src/input.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ where
237237
}
238238

239239
fn render(&mut self, state: &State<T>) -> String {
240-
let theme = THEME.lock().unwrap();
240+
let theme = THEME.read().unwrap();
241241

242242
let part1 = theme.format_header(&state.into(), &self.prompt);
243243
let part2 = if self.input.is_empty() {

src/lib.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -297,19 +297,19 @@ pub fn clear_screen() -> io::Result<()> {
297297

298298
/// Prints a header of the prompt sequence.
299299
pub fn intro(title: impl Display) -> io::Result<()> {
300-
term_write(THEME.lock().unwrap().format_intro(&title.to_string()))
300+
term_write(THEME.read().unwrap().format_intro(&title.to_string()))
301301
}
302302

303303
/// Prints a footer of the prompt sequence.
304304
pub fn outro(message: impl Display) -> io::Result<()> {
305-
term_write(THEME.lock().unwrap().format_outro(&message.to_string()))
305+
term_write(THEME.read().unwrap().format_outro(&message.to_string()))
306306
}
307307

308308
/// Prints a footer of the prompt sequence with a failure style.
309309
pub fn outro_cancel(message: impl Display) -> io::Result<()> {
310310
term_write(
311311
THEME
312-
.lock()
312+
.read()
313313
.unwrap()
314314
.format_outro_cancel(&message.to_string()),
315315
)
@@ -319,7 +319,7 @@ pub fn outro_cancel(message: impl Display) -> io::Result<()> {
319319
pub fn outro_note(prompt: impl Display, message: impl Display) -> io::Result<()> {
320320
term_write(
321321
THEME
322-
.lock()
322+
.read()
323323
.unwrap()
324324
.format_outro_note(&prompt.to_string(), &message.to_string()),
325325
)
@@ -385,7 +385,7 @@ pub fn multi_progress(prompt: impl Display) -> MultiProgress {
385385
pub fn note(prompt: impl Display, message: impl Display) -> io::Result<()> {
386386
term_write(
387387
THEME
388-
.lock()
388+
.read()
389389
.unwrap()
390390
.format_note(&prompt.to_string(), &message.to_string()),
391391
)
@@ -398,45 +398,45 @@ pub mod log {
398398
fn log(text: impl Display, symbol: impl Display) -> io::Result<()> {
399399
term_write(
400400
THEME
401-
.lock()
401+
.read()
402402
.unwrap()
403403
.format_log(&text.to_string(), &symbol.to_string()),
404404
)
405405
}
406406

407407
/// Prints a remark message.
408408
pub fn remark(text: impl Display) -> io::Result<()> {
409-
let symbol = THEME.lock().unwrap().remark_symbol();
409+
let symbol = THEME.read().unwrap().remark_symbol();
410410
log(text, symbol)
411411
}
412412

413413
/// Prints an info message.
414414
pub fn info(text: impl Display) -> io::Result<()> {
415-
let symbol = THEME.lock().unwrap().info_symbol();
415+
let symbol = THEME.read().unwrap().info_symbol();
416416
log(text, symbol)
417417
}
418418

419419
/// Prints a warning message.
420420
pub fn warning(message: impl Display) -> io::Result<()> {
421-
let symbol = THEME.lock().unwrap().warning_symbol();
421+
let symbol = THEME.read().unwrap().warning_symbol();
422422
log(message, symbol)
423423
}
424424

425425
/// Prints an error message.
426426
pub fn error(message: impl Display) -> io::Result<()> {
427-
let symbol = THEME.lock().unwrap().error_symbol();
427+
let symbol = THEME.read().unwrap().error_symbol();
428428
log(message, symbol)
429429
}
430430

431431
/// Prints a success message.
432432
pub fn success(message: impl Display) -> io::Result<()> {
433-
let symbol = THEME.lock().unwrap().active_symbol();
433+
let symbol = THEME.read().unwrap().active_symbol();
434434
log(message, symbol)
435435
}
436436

437437
/// Prints a submitted step message.
438438
pub fn step(message: impl Display) -> io::Result<()> {
439-
let symbol = THEME.lock().unwrap().submit_symbol();
439+
let symbol = THEME.read().unwrap().submit_symbol();
440440
log(message, symbol)
441441
}
442442
}

src/multiprogress.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub struct MultiProgress {
2424
impl MultiProgress {
2525
/// Creates a new multi-progress bar with a given prompt.
2626
pub fn new(prompt: impl Display) -> Self {
27-
let theme = THEME.lock().unwrap();
27+
let theme = THEME.read().unwrap();
2828
let multi = indicatif::MultiProgress::new();
2929

3030
let header =
@@ -83,7 +83,7 @@ impl MultiProgress {
8383
/// this function. To add an empty line, use a line
8484
/// return character (`\n`) at the end of the message.
8585
pub fn println(&self, message: impl Display) {
86-
let theme = THEME.lock().unwrap();
86+
let theme = THEME.read().unwrap();
8787
let symbol = theme.remark_symbol();
8888
let log = theme.format_log_with_spacing(&message.to_string(), &symbol, false);
8989
self.logs.fetch_add(log.lines().count(), Ordering::SeqCst);
@@ -123,7 +123,7 @@ impl MultiProgress {
123123
term.clear_last_lines(HEADER_HEIGHT).ok();
124124
term.write_str(
125125
&THEME
126-
.lock()
126+
.read()
127127
.unwrap()
128128
.format_header(state, (self.prompt.clone() + "\n ").trim_end()),
129129
)

src/multiselect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ impl<T: Clone> PromptInteraction<Vec<T>> for MultiSelect<T> {
161161
}
162162

163163
fn render(&mut self, state: &State<Vec<T>>) -> String {
164-
let theme = THEME.lock().unwrap();
164+
let theme = THEME.read().unwrap();
165165

166166
// Render the static header.
167167
let header = theme.format_header(&state.into(), &self.prompt);

src/password.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl Password {
3030
pub fn new(prompt: impl Display) -> Self {
3131
Self {
3232
prompt: prompt.to_string(),
33-
mask: THEME.lock().unwrap().password_mask(),
33+
mask: THEME.read().unwrap().password_mask(),
3434
..Default::default()
3535
}
3636
}
@@ -132,7 +132,7 @@ impl PromptInteraction<String> for Password {
132132
*chr = self.mask;
133133
}
134134

135-
let theme = THEME.lock().unwrap();
135+
let theme = THEME.read().unwrap();
136136

137137
let line1 = theme.format_header(&state.into(), &self.prompt);
138138
let line2 = theme.format_input(&state.into(), &masked);

src/progress.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,20 @@ impl ProgressBar {
3535
options: Default::default(),
3636
};
3737

38-
this.options_write().template = THEME.lock().unwrap().default_progress_template();
38+
this.options_write().template = THEME.read().unwrap().default_progress_template();
3939

4040
this
4141
}
4242

4343
/// Sets a default spinner visual template for the progress bar.
4444
pub fn with_spinner_template(self) -> Self {
45-
self.options_write().template = THEME.lock().unwrap().default_spinner_template();
45+
self.options_write().template = THEME.read().unwrap().default_spinner_template();
4646
self
4747
}
4848

4949
/// Sets a default visual template for downloading.
5050
pub fn with_download_template(self) -> Self {
51-
self.options_write().template = THEME.lock().unwrap().default_download_template();
51+
self.options_write().template = THEME.read().unwrap().default_download_template();
5252
self
5353
}
5454

@@ -93,7 +93,7 @@ impl ProgressBar {
9393

9494
/// Starts the progress bar.
9595
pub fn start(&self, message: impl Display) {
96-
let theme = THEME.lock().unwrap();
96+
let theme = THEME.read().unwrap();
9797
let options = self.options();
9898

9999
self.bar.set_style(
@@ -148,7 +148,7 @@ impl ProgressBar {
148148
///
149149
/// The method is semi-open for multi-progress bar purposes.
150150
pub(crate) fn redraw_finished(&self, message: impl Display, state: &ThemeState) -> usize {
151-
let theme = THEME.lock().unwrap();
151+
let theme = THEME.read().unwrap();
152152
let options = self.options.read().unwrap();
153153

154154
let render = theme.format_progress_with_state(
@@ -177,7 +177,7 @@ impl ProgressBar {
177177

178178
/// Redraws the progress bar without changing the message.
179179
fn redraw_active_as_started(&self) {
180-
let theme = THEME.lock().unwrap();
180+
let theme = THEME.read().unwrap();
181181
let options = self.options();
182182

183183
self.bar.set_style(
@@ -194,7 +194,7 @@ impl ProgressBar {
194194

195195
/// Redraws the progress bar without changing the message.
196196
fn redraw_active_as_stopped(&self) {
197-
let theme = THEME.lock().unwrap();
197+
let theme = THEME.read().unwrap();
198198
let options = self.options();
199199

200200
self.bar.set_style(

src/select.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ impl<T: Clone> PromptInteraction<T> for Select<T> {
134134
}
135135

136136
fn render(&mut self, state: &State<T>) -> String {
137-
let theme = THEME.lock().unwrap();
137+
let theme = THEME.read().unwrap();
138138

139139
let header_display = theme.format_header(&state.into(), &self.prompt);
140140
let footer_display = theme.format_footer(&state.into());

src/theme.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::sync::Mutex;
1+
use std::sync::RwLock;
22

33
use console::{style, Emoji, Style};
44
use once_cell::sync::Lazy;
@@ -691,19 +691,19 @@ impl Theme for ClackTheme {}
691691
/// The global theme instance (singleton).
692692
///
693693
/// It can be set with [`set_theme`] function.
694-
pub(crate) static THEME: Lazy<Mutex<Box<dyn Theme + Send + Sync>>> =
695-
Lazy::new(|| Mutex::new(Box::new(ClackTheme)));
694+
pub(crate) static THEME: Lazy<RwLock<Box<dyn Theme + Send + Sync>>> =
695+
Lazy::new(|| RwLock::new(Box::new(ClackTheme)));
696696

697697
/// Sets the global theme, which is used by all prompts.
698698
///
699699
/// See [`reset_theme`] for returning to the default theme.
700700
pub fn set_theme<T: Theme + Sync + Send + 'static>(theme: T) {
701-
*THEME.lock().unwrap() = Box::new(theme);
701+
*THEME.write().unwrap() = Box::new(theme);
702702
}
703703

704704
/// Resets the global theme to the default one.
705705
pub fn reset_theme() {
706-
*THEME.lock().unwrap() = Box::new(ClackTheme);
706+
*THEME.write().unwrap() = Box::new(ClackTheme);
707707
}
708708

709709
#[cfg(test)]

0 commit comments

Comments
 (0)