@@ -53,28 +53,27 @@ extern crate nix;
53
53
#[ cfg( feature = "async-tokio" ) ]
54
54
extern crate tokio;
55
55
56
- use std:: fs;
57
- use std:: fs:: File ;
58
56
use std:: io;
59
57
use std:: io:: prelude:: * ;
60
58
#[ cfg( any( target_os = "linux" , target_os = "android" , feature = "async-tokio" ) ) ]
61
59
use std:: io:: SeekFrom ;
62
60
#[ cfg( not( target_os = "wasi" ) ) ]
63
61
use std:: os:: unix:: prelude:: * ;
64
62
use std:: path:: Path ;
63
+ use std:: { fs, fs:: File , task:: Poll } ;
65
64
66
65
#[ cfg( feature = "async-tokio" ) ]
67
- use futures:: { Async , Poll , Stream } ;
66
+ use futures:: { ready , Stream } ;
68
67
#[ cfg( feature = "mio-evented" ) ]
69
- use mio:: unix :: EventedFd ;
68
+ use mio:: event :: Source ;
70
69
#[ cfg( feature = "mio-evented" ) ]
71
- use mio:: Evented ;
70
+ use mio:: unix :: SourceFd ;
72
71
#[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
73
72
use nix:: sys:: epoll:: * ;
74
73
#[ cfg( not( target_os = "wasi" ) ) ]
75
74
use nix:: unistd:: close;
76
75
#[ cfg( feature = "async-tokio" ) ]
77
- use tokio:: reactor :: { Handle , PollEvented } ;
76
+ use tokio:: io :: unix :: AsyncFd ;
78
77
79
78
pub use error:: Error ;
80
79
@@ -472,17 +471,6 @@ impl Pin {
472
471
AsyncPinPoller :: new ( self . pin_num )
473
472
}
474
473
475
- /// Get a Stream of pin interrupts for this pin
476
- ///
477
- /// The PinStream object can be used with the `tokio` crate. You should probably call
478
- /// `set_edge()` before using this.
479
- ///
480
- /// This method is only available when the `async-tokio` crate feature is enabled.
481
- #[ cfg( feature = "async-tokio" ) ]
482
- pub fn get_stream_with_handle ( & self , handle : & Handle ) -> Result < PinStream > {
483
- PinStream :: init_with_handle ( * self , handle)
484
- }
485
-
486
474
/// Get a Stream of pin interrupts for this pin
487
475
///
488
476
/// The PinStream object can be used with the `tokio` crate. You should probably call
@@ -494,22 +482,6 @@ impl Pin {
494
482
PinStream :: init ( * self )
495
483
}
496
484
497
- /// Get a Stream of pin values for this pin
498
- ///
499
- /// The PinStream object can be used with the `tokio` crate. You should probably call
500
- /// `set_edge(Edge::BothEdges)` before using this.
501
- ///
502
- /// Note that the values produced are the value of the pin as soon as we get to handling the
503
- /// interrupt in userspace. Each time this stream produces a value, a change has occurred, but
504
- /// it could end up producing the same value multiple times if the value has changed back
505
- /// between when the interrupt occurred and when the value was read.
506
- ///
507
- /// This method is only available when the `async-tokio` crate feature is enabled.
508
- #[ cfg( feature = "async-tokio" ) ]
509
- pub fn get_value_stream_with_handle ( & self , handle : & Handle ) -> Result < PinValueStream > {
510
- Ok ( PinValueStream ( PinStream :: init_with_handle ( * self , handle) ?) )
511
- }
512
-
513
485
/// Get a Stream of pin values for this pin
514
486
///
515
487
/// The PinStream object can be used with the `tokio` crate. You should probably call
@@ -536,9 +508,9 @@ fn extract_pin_fom_path_test() {
536
508
let tok3 = Pin :: extract_pin_from_path ( & "../../devices/soc0/gpiochip3/gpio/gpio124" ) ;
537
509
assert_eq ! ( 124 , tok3. unwrap( ) ) ;
538
510
let err1 = Pin :: extract_pin_from_path ( & "/sys/CLASS/gpio/gpio" ) ;
539
- assert_eq ! ( true , err1. is_err( ) ) ;
511
+ assert ! ( err1. is_err( ) ) ;
540
512
let err2 = Pin :: extract_pin_from_path ( & "/sys/class/gpio/gpioSDS" ) ;
541
- assert_eq ! ( true , err2. is_err( ) ) ;
513
+ assert ! ( err2. is_err( ) ) ;
542
514
}
543
515
#[ cfg( not( target_os = "wasi" ) ) ]
544
516
#[ derive( Debug ) ]
@@ -643,76 +615,70 @@ impl AsyncPinPoller {
643
615
}
644
616
645
617
#[ cfg( feature = "mio-evented" ) ]
646
- impl Evented for AsyncPinPoller {
618
+ impl Source for AsyncPinPoller {
647
619
fn register (
648
- & self ,
649
- poll : & mio:: Poll ,
620
+ & mut self ,
621
+ poll : & mio:: Registry ,
650
622
token : mio:: Token ,
651
- interest : mio:: Ready ,
652
- opts : mio:: PollOpt ,
623
+ interest : mio:: Interest ,
653
624
) -> io:: Result < ( ) > {
654
- EventedFd ( & self . devfile . as_raw_fd ( ) ) . register ( poll, token, interest, opts )
625
+ SourceFd ( & self . as_raw_fd ( ) ) . register ( poll, token, interest)
655
626
}
656
627
657
628
fn reregister (
658
- & self ,
659
- poll : & mio:: Poll ,
629
+ & mut self ,
630
+ poll : & mio:: Registry ,
660
631
token : mio:: Token ,
661
- interest : mio:: Ready ,
662
- opts : mio:: PollOpt ,
632
+ interest : mio:: Interest ,
663
633
) -> io:: Result < ( ) > {
664
- EventedFd ( & self . devfile . as_raw_fd ( ) ) . reregister ( poll, token, interest, opts )
634
+ SourceFd ( & self . as_raw_fd ( ) ) . reregister ( poll, token, interest)
665
635
}
666
636
667
- fn deregister ( & self , poll : & mio:: Poll ) -> io:: Result < ( ) > {
668
- EventedFd ( & self . devfile . as_raw_fd ( ) ) . deregister ( poll)
637
+ fn deregister ( & mut self , poll : & mio:: Registry ) -> io:: Result < ( ) > {
638
+ SourceFd ( & self . as_raw_fd ( ) ) . deregister ( poll)
669
639
}
670
640
}
671
641
672
642
#[ cfg( feature = "async-tokio" ) ]
673
- pub struct PinStream {
674
- evented : PollEvented < AsyncPinPoller > ,
675
- skipped_first_event : bool ,
643
+ impl AsRawFd for AsyncPinPoller {
644
+ fn as_raw_fd ( & self ) -> RawFd {
645
+ self . devfile . as_raw_fd ( )
646
+ }
676
647
}
677
648
678
649
#[ cfg( feature = "async-tokio" ) ]
679
- impl PinStream {
680
- pub fn init_with_handle ( pin : Pin , handle : & Handle ) -> Result < Self > {
681
- Ok ( PinStream {
682
- evented : PollEvented :: new ( pin. get_async_poller ( ) ?, handle) ?,
683
- skipped_first_event : false ,
684
- } )
685
- }
650
+ pub struct PinStream {
651
+ evented : AsyncFd < AsyncPinPoller > ,
652
+ skipped_first_event : bool ,
686
653
}
687
654
688
655
#[ cfg( feature = "async-tokio" ) ]
689
656
impl PinStream {
690
657
pub fn init ( pin : Pin ) -> Result < Self > {
691
658
Ok ( PinStream {
692
- evented : PollEvented :: new ( pin. get_async_poller ( ) ?, & Handle :: default ( ) ) ?,
659
+ evented : AsyncFd :: new ( pin. get_async_poller ( ) ?) ?,
693
660
skipped_first_event : false ,
694
661
} )
695
662
}
696
663
}
697
664
698
665
#[ cfg( feature = "async-tokio" ) ]
699
666
impl Stream for PinStream {
700
- type Item = ( ) ;
701
- type Error = Error ;
702
-
703
- fn poll ( & mut self ) -> Poll < Option < Self :: Item > , Self :: Error > {
704
- Ok ( match self . evented . poll_read ( ) {
705
- Async :: Ready ( ( ) ) = > {
706
- self . evented . need_read ( ) ? ;
707
- if self . skipped_first_event {
708
- Async :: Ready ( Some ( ( ) ) )
709
- } else {
710
- self . skipped_first_event = true ;
711
- Async :: NotReady
712
- }
667
+ type Item = Result < ( ) > ;
668
+
669
+ fn poll_next (
670
+ mut self : std :: pin :: Pin < & mut Self > ,
671
+ cx : & mut std :: task :: Context < ' _ > ,
672
+ ) -> Poll < Option < Self :: Item > > {
673
+ loop {
674
+ let mut guard = ready ! ( self . evented . poll_read_ready ( cx ) ) ? ;
675
+ guard . clear_ready ( ) ;
676
+ if self . skipped_first_event {
677
+ return Poll :: Ready ( Some ( Ok ( ( ) ) ) ) ;
678
+ } else {
679
+ self . skipped_first_event = true ;
713
680
}
714
- Async :: NotReady => Async :: NotReady ,
715
- } )
681
+ }
716
682
}
717
683
}
718
684
@@ -729,18 +695,13 @@ impl PinValueStream {
729
695
730
696
#[ cfg( feature = "async-tokio" ) ]
731
697
impl Stream for PinValueStream {
732
- type Item = u8 ;
733
- type Error = Error ;
734
-
735
- fn poll ( & mut self ) -> Poll < Option < Self :: Item > , Self :: Error > {
736
- match self . 0 . poll ( ) {
737
- Ok ( Async :: Ready ( Some ( ( ) ) ) ) => {
738
- let value = self . get_value ( ) ?;
739
- Ok ( Async :: Ready ( Some ( value) ) )
740
- }
741
- Ok ( Async :: Ready ( None ) ) => Ok ( Async :: Ready ( None ) ) ,
742
- Ok ( Async :: NotReady ) => Ok ( Async :: NotReady ) ,
743
- Err ( e) => Err ( e) ,
744
- }
698
+ type Item = Result < u8 > ;
699
+
700
+ fn poll_next (
701
+ mut self : std:: pin:: Pin < & mut Self > ,
702
+ cx : & mut std:: task:: Context < ' _ > ,
703
+ ) -> Poll < Option < Self :: Item > > {
704
+ ready ! ( std:: pin:: Pin :: new( & mut self . 0 ) . poll_next( cx) ) ;
705
+ Poll :: Ready ( Some ( Ok ( self . get_value ( ) ?) ) )
745
706
}
746
707
}
0 commit comments