Skip to content

Commit 14eeb70

Browse files
committed
Inline single-use with_saved_global_state() function
1 parent 1222fef commit 14eeb70

File tree

1 file changed

+10
-24
lines changed

1 file changed

+10
-24
lines changed

src/cli/self_update/test.rs

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,6 @@ use winreg::{
99
RegKey, RegValue,
1010
};
1111

12-
/// Support testing of code that mutates global state
13-
#[cfg(windows)]
14-
fn with_saved_global_state<S>(
15-
getter: impl Fn() -> io::Result<S>,
16-
setter: impl Fn(S),
17-
f: &mut dyn FnMut(),
18-
) {
19-
// Lock protects concurrent mutation of registry
20-
static LOCK: Mutex<()> = Mutex::new(());
21-
let _g = LOCK.lock();
22-
23-
// Save and restore the global state here to keep from trashing things.
24-
let saved_state =
25-
getter().expect("Error getting global state: Better abort to avoid trashing it");
26-
let _g = scopeguard::guard(saved_state, setter);
27-
28-
f();
29-
}
30-
3112
#[cfg(windows)]
3213
pub fn with_saved_path(f: &mut dyn FnMut()) {
3314
with_saved_reg_value(&RegKey::predef(HKEY_CURRENT_USER), "Environment", "PATH", f)
@@ -45,11 +26,16 @@ pub fn get_path() -> io::Result<Option<RegValue>> {
4526

4627
#[cfg(windows)]
4728
pub fn with_saved_reg_value(root: &RegKey, subkey: &str, name: &str, f: &mut dyn FnMut()) {
48-
with_saved_global_state(
49-
|| get_reg_value(root, subkey, name),
50-
|p| restore_reg_value(root, subkey, name, p),
51-
f,
52-
)
29+
// Lock protects concurrent mutation of registry
30+
static LOCK: Mutex<()> = Mutex::new(());
31+
let _g = LOCK.lock();
32+
33+
// Save and restore the global state here to keep from trashing things.
34+
let saved_state = get_reg_value(root, subkey, name)
35+
.expect("Error getting global state: Better abort to avoid trashing it");
36+
let _g = scopeguard::guard(saved_state, |p| restore_reg_value(root, subkey, name, p));
37+
38+
f();
5339
}
5440

5541
#[cfg(windows)]

0 commit comments

Comments
 (0)