1+ use crate :: error:: Result ;
12use crate :: { hash, sync, AsyncNotification , FileDiff , CWD } ;
23use crossbeam_channel:: Sender ;
34use log:: trace;
@@ -42,21 +43,22 @@ impl AsyncDiff {
4243 }
4344
4445 ///
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+ } )
5253 }
5354
5455 ///
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) ? ;
5960 }
61+ Ok ( ( ) )
6062 }
6163
6264 ///
@@ -68,16 +70,16 @@ impl AsyncDiff {
6870 pub fn request (
6971 & mut self ,
7072 params : DiffParams ,
71- ) -> Option < FileDiff > {
73+ ) -> Result < Option < FileDiff > > {
7274 trace ! ( "request" ) ;
7375
7476 let hash = hash ( & params) ;
7577
7678 {
77- let mut current = self . current . lock ( ) . unwrap ( ) ;
79+ let mut current = self . current . lock ( ) ? ;
7880
7981 if current. 0 == hash {
80- return current. 1 . clone ( ) ;
82+ return Ok ( current. 1 . clone ( ) ) ;
8183 }
8284
8385 current. 0 = hash;
@@ -91,25 +93,13 @@ impl AsyncDiff {
9193 rayon_core:: spawn ( move || {
9294 arc_pending. fetch_add ( 1 , Ordering :: Relaxed ) ;
9395
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" ) ;
113103
114104 arc_pending. fetch_sub ( 1 , Ordering :: Relaxed ) ;
115105
@@ -120,16 +110,49 @@ impl AsyncDiff {
120110 }
121111 } ) ;
122112
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)
124146 }
125147
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 ) )
128150 }
129151
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 ( ) ? ;
132154 current. 0 = 0 ;
133155 current. 1 = None ;
156+ Ok ( ( ) )
134157 }
135158}
0 commit comments