1
+ use crate :: error:: Result ;
1
2
use crate :: { hash, sync, AsyncNotification , FileDiff , CWD } ;
2
3
use crossbeam_channel:: Sender ;
3
4
use log:: trace;
@@ -42,21 +43,22 @@ impl AsyncDiff {
42
43
}
43
44
44
45
///
45
- pub fn last ( & mut self ) -> Option < ( DiffParams , FileDiff ) > {
46
- let last = self . last . lock ( ) . unwrap ( ) ;
47
- if let Some ( res ) = last . clone ( ) {
48
- Some ( ( res . params , res . result ) )
49
- } else {
50
- None
51
- }
46
+ pub fn last ( & mut self ) -> Result < Option < ( DiffParams , FileDiff ) > > {
47
+ let last = self . last . lock ( ) ? ;
48
+
49
+ Ok ( match last . clone ( ) {
50
+ Some ( res ) => Some ( ( res . params , res . result ) ) ,
51
+ None => None ,
52
+ } )
52
53
}
53
54
54
55
///
55
- pub fn refresh ( & mut self ) {
56
- if let Some ( param) = self . get_last_param ( ) {
57
- self . clear_current ( ) ;
58
- self . request ( param) ;
56
+ pub fn refresh ( & mut self ) -> Result < ( ) > {
57
+ if let Ok ( Some ( param) ) = self . get_last_param ( ) {
58
+ self . clear_current ( ) ? ;
59
+ self . request ( param) ? ;
59
60
}
61
+ Ok ( ( ) )
60
62
}
61
63
62
64
///
@@ -68,16 +70,16 @@ impl AsyncDiff {
68
70
pub fn request (
69
71
& mut self ,
70
72
params : DiffParams ,
71
- ) -> Option < FileDiff > {
73
+ ) -> Result < Option < FileDiff > > {
72
74
trace ! ( "request" ) ;
73
75
74
76
let hash = hash ( & params) ;
75
77
76
78
{
77
- let mut current = self . current . lock ( ) . unwrap ( ) ;
79
+ let mut current = self . current . lock ( ) ? ;
78
80
79
81
if current. 0 == hash {
80
- return current. 1 . clone ( ) ;
82
+ return Ok ( current. 1 . clone ( ) ) ;
81
83
}
82
84
83
85
current. 0 = hash;
@@ -91,25 +93,13 @@ impl AsyncDiff {
91
93
rayon_core:: spawn ( move || {
92
94
arc_pending. fetch_add ( 1 , Ordering :: Relaxed ) ;
93
95
94
- let res =
95
- sync:: diff:: get_diff ( CWD , params. 0 . clone ( ) , params. 1 ) ;
96
- let mut notify = false ;
97
- {
98
- let mut current = arc_current. lock ( ) . unwrap ( ) ;
99
- if current. 0 == hash {
100
- current. 1 = Some ( res. clone ( ) ) ;
101
- notify = true ;
102
- }
103
- }
104
-
105
- {
106
- let mut last = arc_last. lock ( ) . unwrap ( ) ;
107
- * last = Some ( LastResult {
108
- result : res,
109
- hash,
110
- params,
111
- } ) ;
112
- }
96
+ let notify = AsyncDiff :: get_diff_helper (
97
+ params,
98
+ arc_last,
99
+ arc_current,
100
+ hash,
101
+ )
102
+ . expect ( "error getting diff" ) ;
113
103
114
104
arc_pending. fetch_sub ( 1 , Ordering :: Relaxed ) ;
115
105
@@ -120,16 +110,49 @@ impl AsyncDiff {
120
110
}
121
111
} ) ;
122
112
123
- None
113
+ Ok ( None )
114
+ }
115
+
116
+ fn get_diff_helper (
117
+ params : DiffParams ,
118
+ arc_last : Arc <
119
+ Mutex < Option < LastResult < DiffParams , FileDiff > > > ,
120
+ > ,
121
+ arc_current : Arc < Mutex < Request < u64 , FileDiff > > > ,
122
+ hash : u64 ,
123
+ ) -> Result < bool > {
124
+ let res =
125
+ sync:: diff:: get_diff ( CWD , params. 0 . clone ( ) , params. 1 ) ?;
126
+
127
+ let mut notify = false ;
128
+ {
129
+ let mut current = arc_current. lock ( ) ?;
130
+ if current. 0 == hash {
131
+ current. 1 = Some ( res. clone ( ) ) ;
132
+ notify = true ;
133
+ }
134
+ }
135
+
136
+ {
137
+ let mut last = arc_last. lock ( ) ?;
138
+ * last = Some ( LastResult {
139
+ result : res,
140
+ hash,
141
+ params,
142
+ } ) ;
143
+ }
144
+
145
+ Ok ( notify)
124
146
}
125
147
126
- fn get_last_param ( & self ) -> Option < DiffParams > {
127
- self . last . lock ( ) . unwrap ( ) . clone ( ) . map ( |e| e. params )
148
+ fn get_last_param ( & self ) -> Result < Option < DiffParams > > {
149
+ Ok ( self . last . lock ( ) ? . clone ( ) . map ( |e| e. params ) )
128
150
}
129
151
130
- fn clear_current ( & mut self ) {
131
- let mut current = self . current . lock ( ) . unwrap ( ) ;
152
+ fn clear_current ( & mut self ) -> Result < ( ) > {
153
+ let mut current = self . current . lock ( ) ? ;
132
154
current. 0 = 0 ;
133
155
current. 1 = None ;
156
+ Ok ( ( ) )
134
157
}
135
158
}
0 commit comments