@@ -5,10 +5,18 @@ of general async handling features.
5
5
6
6
use std:: sync:: {
7
7
atomic:: { AtomicBool , Ordering } ,
8
- mpsc, Arc , Condvar , Mutex ,
8
+ mpsc,
9
+ mpsc:: RecvTimeoutError ,
10
+ Arc , Condvar , Mutex ,
9
11
} ;
10
12
use std:: time:: Duration ;
11
13
14
+ #[ cfg( feature = "for_futures" ) ]
15
+ use super :: common:: shared_thread_pool;
16
+ #[ cfg( feature = "for_futures" ) ]
17
+ use crate :: futures:: task:: SpawnExt ;
18
+ // #[cfg(feature = "for_futures")]
19
+ use std:: error:: Error ;
12
20
#[ cfg( feature = "for_futures" ) ]
13
21
use std:: future:: Future ;
14
22
#[ cfg( feature = "for_futures" ) ]
@@ -412,18 +420,16 @@ where
412
420
}
413
421
414
422
fn poll ( & mut self ) -> Option < T > {
415
- if !self . is_alive ( ) {
416
- return None :: < T > ;
417
- }
418
-
419
- {
420
- let result = self . blocking_recever . lock ( ) . unwrap ( ) . try_recv ( ) ;
423
+ let result = self . poll_result ( ) ;
421
424
422
- if self . panic && result. is_err ( ) {
423
- std:: panic:: panic_any ( result. err ( ) ) ;
424
- }
425
+ if self . panic && result. is_err ( ) {
426
+ std:: panic:: panic_any ( result. err ( ) ) ;
427
+ // return None;
428
+ }
425
429
426
- result. ok ( )
430
+ match result {
431
+ Ok ( v) => Some ( v) ,
432
+ Err ( _) => None ,
427
433
}
428
434
}
429
435
@@ -434,34 +440,102 @@ where
434
440
}
435
441
436
442
fn take ( & mut self ) -> Option < T > {
443
+ let result = self . take_result ( ) ;
444
+
445
+ if self . panic && result. is_err ( ) {
446
+ std:: panic:: panic_any ( result. err ( ) ) ;
447
+ // return None;
448
+ }
449
+
450
+ match result {
451
+ Ok ( v) => Some ( v) ,
452
+ Err ( _) => None ,
453
+ }
454
+ }
455
+ }
456
+
457
+ impl < T > BlockingQueue < T >
458
+ where
459
+ T : Send + ' static ,
460
+ {
461
+ pub fn poll_result ( & mut self ) -> Result < T , Box < dyn Error + Send > > {
437
462
if !self . is_alive ( ) {
438
- return None :: < T > ;
463
+ return Err ( Box :: new ( RecvTimeoutError :: Disconnected ) ) ;
464
+ }
465
+
466
+ {
467
+ let result = self . blocking_recever . lock ( ) . unwrap ( ) . try_recv ( ) ;
468
+
469
+ match result {
470
+ Ok ( v) => Ok ( v) ,
471
+ Err ( e) => Err ( Box :: new ( e) ) ,
472
+ }
473
+ }
474
+ }
475
+
476
+ pub fn take_result ( & mut self ) -> Result < T , Box < dyn Error + Send > > {
477
+ if !self . is_alive ( ) {
478
+ return Err ( Box :: new ( RecvTimeoutError :: Disconnected ) ) ;
439
479
}
440
480
441
481
{
442
482
match self . timeout {
443
483
Some ( duration) => {
444
484
let result = self . blocking_recever . lock ( ) . unwrap ( ) . recv_timeout ( duration) ;
445
485
446
- if self . panic && result. is_err ( ) {
447
- std:: panic:: panic_any ( result. err ( ) ) ;
486
+ match result {
487
+ Ok ( v) => Ok ( v) ,
488
+ Err ( e) => Err ( Box :: new ( e) ) ,
448
489
}
449
-
450
- result. ok ( )
451
490
}
452
491
None => {
453
492
let result = self . blocking_recever . lock ( ) . unwrap ( ) . recv ( ) ;
454
493
455
- if self . panic && result. is_err ( ) {
456
- std:: panic:: panic_any ( result. err ( ) ) ;
494
+ match result {
495
+ Ok ( v) => Ok ( v) ,
496
+ Err ( e) => Err ( Box :: new ( e) ) ,
457
497
}
458
-
459
- result. ok ( )
460
498
}
461
499
}
462
500
}
463
501
}
464
502
}
503
+ #[ cfg( feature = "for_futures" ) ]
504
+ impl < T > BlockingQueue < T >
505
+ where
506
+ T : Send + ' static + Clone ,
507
+ {
508
+ pub async fn poll_result_as_future ( & mut self ) -> Result < T , Box < dyn Error + Send > > {
509
+ let mut queue = self . clone ( ) ;
510
+
511
+ let spawn_future_result = {
512
+ shared_thread_pool ( )
513
+ . inner
514
+ . lock ( )
515
+ . unwrap ( )
516
+ . spawn_with_handle ( async move { queue. poll_result ( ) } )
517
+ } ;
518
+ match spawn_future_result {
519
+ Ok ( future) => future. await ,
520
+ Err ( e) => Err ( Box :: new ( e) ) ,
521
+ }
522
+ }
523
+ pub async fn take_result_as_future ( & mut self ) -> Result < T , Box < dyn Error + Send > > {
524
+ let mut queue = self . clone ( ) ;
525
+
526
+ let spawn_future_result = {
527
+ shared_thread_pool ( )
528
+ . inner
529
+ . lock ( )
530
+ . unwrap ( )
531
+ . spawn_with_handle ( async move { queue. take_result ( ) } )
532
+ } ;
533
+ match spawn_future_result {
534
+ Ok ( future) => future. await ,
535
+ Err ( e) => Err ( Box :: new ( e) ) ,
536
+ }
537
+ }
538
+ }
465
539
466
540
#[ cfg( feature = "for_futures" ) ]
467
541
#[ futures_test:: test]
0 commit comments