@@ -452,13 +452,7 @@ impl<S: Read + Write> RawClient<S> {
452
452
// might have already received a response for that id, but we don't know it
453
453
// yet. Exiting here forces the calling code to fallback to the sender-receiver
454
454
// method, and it should find a message there waiting for it.
455
- if self
456
- . waiting_map
457
- . lock ( )
458
- . unwrap ( )
459
- . get ( & until_message)
460
- . is_none ( )
461
- {
455
+ if self . waiting_map . lock ( ) ?. get ( & until_message) . is_none ( ) {
462
456
return Err ( Error :: CouldntLockReader ) ;
463
457
}
464
458
}
@@ -494,7 +488,7 @@ impl<S: Read + Write> RawClient<S> {
494
488
) ;
495
489
496
490
// Remove ourselves from the "waiting map"
497
- let mut map = self . waiting_map . lock ( ) . unwrap ( ) ;
491
+ let mut map = self . waiting_map . lock ( ) ? ;
498
492
map. remove ( & resp_id) ;
499
493
500
494
// If the map is not empty, we select a random thread to become the
@@ -512,8 +506,7 @@ impl<S: Read + Write> RawClient<S> {
512
506
// move on
513
507
trace ! ( "Reader thread received response for {}" , resp_id) ;
514
508
515
- if let Some ( sender) = self . waiting_map . lock ( ) . unwrap ( ) . remove ( & resp_id)
516
- {
509
+ if let Some ( sender) = self . waiting_map . lock ( ) ?. remove ( & resp_id) {
517
510
sender
518
511
. send ( ChannelMessage :: Response ( resp) )
519
512
. expect ( "Unable to send the response" ) ;
@@ -556,13 +549,13 @@ impl<S: Read + Write> RawClient<S> {
556
549
// Add our listener to the map before we send the request, to make sure we don't get a
557
550
// reply before the receiver is added
558
551
let ( sender, receiver) = channel ( ) ;
559
- self . waiting_map . lock ( ) . unwrap ( ) . insert ( req. id , sender) ;
552
+ self . waiting_map . lock ( ) ? . insert ( req. id , sender) ;
560
553
561
554
let mut raw = serde_json:: to_vec ( & req) ?;
562
555
trace ! ( "==> {}" , String :: from_utf8_lossy( & raw) ) ;
563
556
564
557
raw. extend_from_slice ( b"\n " ) ;
565
- let mut stream = self . stream . lock ( ) . unwrap ( ) ;
558
+ let mut stream = self . stream . lock ( ) ? ;
566
559
stream. write_all ( & raw ) ?;
567
560
stream. flush ( ) ?;
568
561
drop ( stream) ; // release the lock
@@ -574,7 +567,7 @@ impl<S: Read + Write> RawClient<S> {
574
567
e @ Err ( _) => {
575
568
// In case of error our sender could still be left in the map, depending on where
576
569
// the error happened. Just in case, try to remove it here
577
- self . waiting_map . lock ( ) . unwrap ( ) . remove ( & req. id ) ;
570
+ self . waiting_map . lock ( ) ? . remove ( & req. id ) ;
578
571
return e;
579
572
}
580
573
} ;
@@ -617,14 +610,14 @@ impl<S: Read + Write> RawClient<S> {
617
610
618
611
fn handle_notification ( & self , method : & str , result : serde_json:: Value ) -> Result < ( ) , Error > {
619
612
match method {
620
- "blockchain.headers.subscribe" => self . headers . lock ( ) . unwrap ( ) . append (
613
+ "blockchain.headers.subscribe" => self . headers . lock ( ) ? . append (
621
614
& mut serde_json:: from_value :: < Vec < RawHeaderNotification > > ( result) ?
622
615
. into_iter ( )
623
616
. collect ( ) ,
624
617
) ,
625
618
"blockchain.scripthash.subscribe" => {
626
619
let unserialized: ScriptNotification = serde_json:: from_value ( result) ?;
627
- let mut script_notifications = self . script_notifications . lock ( ) . unwrap ( ) ;
620
+ let mut script_notifications = self . script_notifications . lock ( ) ? ;
628
621
629
622
let queue = script_notifications
630
623
. get_mut ( & unserialized. scripthash )
@@ -668,10 +661,7 @@ impl<T: Read + Write> ElectrumApi for RawClient<T> {
668
661
) ;
669
662
missing_responses. insert ( req. id ) ;
670
663
671
- self . waiting_map
672
- . lock ( )
673
- . unwrap ( )
674
- . insert ( req. id , sender. clone ( ) ) ;
664
+ self . waiting_map . lock ( ) ?. insert ( req. id , sender. clone ( ) ) ;
675
665
676
666
raw. append ( & mut serde_json:: to_vec ( & req) ?) ;
677
667
raw. extend_from_slice ( b"\n " ) ;
@@ -683,7 +673,7 @@ impl<T: Read + Write> ElectrumApi for RawClient<T> {
683
673
684
674
trace ! ( "==> {}" , String :: from_utf8_lossy( & raw) ) ;
685
675
686
- let mut stream = self . stream . lock ( ) . unwrap ( ) ;
676
+ let mut stream = self . stream . lock ( ) ? ;
687
677
stream. write_all ( & raw ) ?;
688
678
stream. flush ( ) ?;
689
679
drop ( stream) ; // release the lock
@@ -699,7 +689,7 @@ impl<T: Read + Write> ElectrumApi for RawClient<T> {
699
689
// the error happened. Just in case, try to remove it here
700
690
warn ! ( "got error for req_id {}: {:?}" , req_id, e) ;
701
691
warn ! ( "removing all waiting req of this batch" ) ;
702
- let mut guard = self . waiting_map . lock ( ) . unwrap ( ) ;
692
+ let mut guard = self . waiting_map . lock ( ) ? ;
703
693
for req_id in missing_responses. iter ( ) {
704
694
guard. remove ( & req_id) ;
705
695
}
@@ -730,7 +720,7 @@ impl<T: Read + Write> ElectrumApi for RawClient<T> {
730
720
}
731
721
732
722
fn block_headers_pop_raw ( & self ) -> Result < Option < RawHeaderNotification > , Error > {
733
- Ok ( self . headers . lock ( ) . unwrap ( ) . pop_front ( ) )
723
+ Ok ( self . headers . lock ( ) ? . pop_front ( ) )
734
724
}
735
725
736
726
fn block_header_raw ( & self , height : usize ) -> Result < Vec < u8 > , Error > {
@@ -796,7 +786,7 @@ impl<T: Read + Write> ElectrumApi for RawClient<T> {
796
786
797
787
fn script_subscribe ( & self , script : & Script ) -> Result < Option < ScriptStatus > , Error > {
798
788
let script_hash = script. to_electrum_scripthash ( ) ;
799
- let mut script_notifications = self . script_notifications . lock ( ) . unwrap ( ) ;
789
+ let mut script_notifications = self . script_notifications . lock ( ) ? ;
800
790
801
791
if script_notifications. contains_key ( & script_hash) {
802
792
return Err ( Error :: AlreadySubscribed ( script_hash) ) ;
@@ -816,7 +806,7 @@ impl<T: Read + Write> ElectrumApi for RawClient<T> {
816
806
817
807
fn script_unsubscribe ( & self , script : & Script ) -> Result < bool , Error > {
818
808
let script_hash = script. to_electrum_scripthash ( ) ;
819
- let mut script_notifications = self . script_notifications . lock ( ) . unwrap ( ) ;
809
+ let mut script_notifications = self . script_notifications . lock ( ) ? ;
820
810
821
811
if !script_notifications. contains_key ( & script_hash) {
822
812
return Err ( Error :: NotSubscribed ( script_hash) ) ;
@@ -838,12 +828,7 @@ impl<T: Read + Write> ElectrumApi for RawClient<T> {
838
828
fn script_pop ( & self , script : & Script ) -> Result < Option < ScriptStatus > , Error > {
839
829
let script_hash = script. to_electrum_scripthash ( ) ;
840
830
841
- match self
842
- . script_notifications
843
- . lock ( )
844
- . unwrap ( )
845
- . get_mut ( & script_hash)
846
- {
831
+ match self . script_notifications . lock ( ) ?. get_mut ( & script_hash) {
847
832
None => Err ( Error :: NotSubscribed ( script_hash) ) ,
848
833
Some ( queue) => Ok ( queue. pop_front ( ) ) ,
849
834
}
0 commit comments