1
- use std:: cell:: RefCell ;
2
1
use std:: collections:: BTreeMap ;
3
2
use std:: fmt;
4
3
use std:: path:: { Path , PathBuf } ;
5
4
use std:: str:: FromStr ;
5
+ use std:: sync:: RwLock ;
6
6
7
7
use anyhow:: { Context , Result } ;
8
8
use serde:: { Deserialize , Serialize } ;
@@ -13,22 +13,22 @@ use crate::errors::*;
13
13
use crate :: notifications:: * ;
14
14
use crate :: utils;
15
15
16
- #[ derive( Clone , Debug , Eq , PartialEq ) ]
16
+ #[ derive( Debug ) ]
17
17
pub struct SettingsFile {
18
18
path : PathBuf ,
19
- cache : RefCell < Option < Settings > > ,
19
+ cache : RwLock < Option < Settings > > ,
20
20
}
21
21
22
22
impl SettingsFile {
23
23
pub ( crate ) fn new ( path : PathBuf ) -> Self {
24
24
Self {
25
25
path,
26
- cache : RefCell :: new ( None ) ,
26
+ cache : RwLock :: default ( ) ,
27
27
}
28
28
}
29
29
30
30
fn write_settings ( & self ) -> Result < ( ) > {
31
- let settings = self . cache . borrow ( ) ;
31
+ let settings = self . cache . read ( ) . unwrap ( ) ;
32
32
utils:: write_file (
33
33
"settings" ,
34
34
& self . path ,
@@ -40,10 +40,10 @@ impl SettingsFile {
40
40
fn read_settings ( & self ) -> Result < ( ) > {
41
41
let mut needs_save = false ;
42
42
{
43
- let b = self . cache . borrow ( ) ;
43
+ let b = self . cache . read ( ) . unwrap ( ) ;
44
44
if b. is_none ( ) {
45
45
drop ( b) ;
46
- * self . cache . borrow_mut ( ) = Some ( if utils:: is_file ( & self . path ) {
46
+ * self . cache . write ( ) . unwrap ( ) = Some ( if utils:: is_file ( & self . path ) {
47
47
let content = utils:: read_file ( "settings" , & self . path ) ?;
48
48
Settings :: parse ( & content) . with_context ( || RustupError :: ParsingFile {
49
49
name : "settings" ,
@@ -65,14 +65,17 @@ impl SettingsFile {
65
65
self . read_settings ( ) ?;
66
66
67
67
// Settings can no longer be None so it's OK to unwrap
68
- f ( self . cache . borrow ( ) . as_ref ( ) . unwrap ( ) )
68
+ f ( self . cache . read ( ) . unwrap ( ) . as_ref ( ) . unwrap ( ) )
69
69
}
70
70
71
71
pub ( crate ) fn with_mut < T , F : FnOnce ( & mut Settings ) -> Result < T > > ( & self , f : F ) -> Result < T > {
72
72
self . read_settings ( ) ?;
73
73
74
74
// Settings can no longer be None so it's OK to unwrap
75
- let result = { f ( self . cache . borrow_mut ( ) . as_mut ( ) . unwrap ( ) ) ? } ;
75
+ let result = {
76
+ let mut result = self . cache . write ( ) . unwrap ( ) ;
77
+ f ( result. as_mut ( ) . unwrap ( ) ) ?
78
+ } ;
76
79
self . write_settings ( ) ?;
77
80
Ok ( result)
78
81
}
0 commit comments