@@ -53,28 +53,29 @@ extern crate nix;
5353#[ cfg( feature = "async-tokio" ) ]
5454extern crate tokio;
5555
56- use std:: fs;
57- use std:: fs:: File ;
5856use std:: io;
5957use std:: io:: prelude:: * ;
6058#[ cfg( any( target_os = "linux" , target_os = "android" , feature = "async-tokio" ) ) ]
6159use std:: io:: SeekFrom ;
6260#[ cfg( not( target_os = "wasi" ) ) ]
6361use std:: os:: unix:: prelude:: * ;
6462use std:: path:: Path ;
63+ use std:: { fs, fs:: File } ;
6564
6665#[ cfg( feature = "async-tokio" ) ]
67- use futures:: { Async , Poll , Stream } ;
66+ use futures:: { ready , Stream } ;
6867#[ cfg( feature = "mio-evented" ) ]
69- use mio:: unix :: EventedFd ;
68+ use mio:: event :: Source ;
7069#[ cfg( feature = "mio-evented" ) ]
71- use mio:: Evented ;
70+ use mio:: unix :: SourceFd ;
7271#[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
7372use nix:: sys:: epoll:: * ;
7473#[ cfg( not( target_os = "wasi" ) ) ]
7574use nix:: unistd:: close;
7675#[ cfg( feature = "async-tokio" ) ]
77- use tokio:: reactor:: { Handle , PollEvented } ;
76+ use std:: task:: Poll ;
77+ #[ cfg( feature = "async-tokio" ) ]
78+ use tokio:: io:: unix:: AsyncFd ;
7879
7980pub use error:: Error ;
8081
@@ -472,17 +473,6 @@ impl Pin {
472473 AsyncPinPoller :: new ( self . pin_num )
473474 }
474475
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-
486476 /// Get a Stream of pin interrupts for this pin
487477 ///
488478 /// The PinStream object can be used with the `tokio` crate. You should probably call
@@ -494,22 +484,6 @@ impl Pin {
494484 PinStream :: init ( * self )
495485 }
496486
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-
513487 /// Get a Stream of pin values for this pin
514488 ///
515489 /// The PinStream object can be used with the `tokio` crate. You should probably call
@@ -536,9 +510,9 @@ fn extract_pin_fom_path_test() {
536510 let tok3 = Pin :: extract_pin_from_path ( & "../../devices/soc0/gpiochip3/gpio/gpio124" ) ;
537511 assert_eq ! ( 124 , tok3. unwrap( ) ) ;
538512 let err1 = Pin :: extract_pin_from_path ( & "/sys/CLASS/gpio/gpio" ) ;
539- assert_eq ! ( true , err1. is_err( ) ) ;
513+ assert ! ( err1. is_err( ) ) ;
540514 let err2 = Pin :: extract_pin_from_path ( & "/sys/class/gpio/gpioSDS" ) ;
541- assert_eq ! ( true , err2. is_err( ) ) ;
515+ assert ! ( err2. is_err( ) ) ;
542516}
543517#[ cfg( not( target_os = "wasi" ) ) ]
544518#[ derive( Debug ) ]
@@ -643,76 +617,70 @@ impl AsyncPinPoller {
643617}
644618
645619#[ cfg( feature = "mio-evented" ) ]
646- impl Evented for AsyncPinPoller {
620+ impl Source for AsyncPinPoller {
647621 fn register (
648- & self ,
649- poll : & mio:: Poll ,
622+ & mut self ,
623+ poll : & mio:: Registry ,
650624 token : mio:: Token ,
651- interest : mio:: Ready ,
652- opts : mio:: PollOpt ,
625+ interest : mio:: Interest ,
653626 ) -> io:: Result < ( ) > {
654- EventedFd ( & self . devfile . as_raw_fd ( ) ) . register ( poll, token, interest, opts )
627+ SourceFd ( & self . as_raw_fd ( ) ) . register ( poll, token, interest)
655628 }
656629
657630 fn reregister (
658- & self ,
659- poll : & mio:: Poll ,
631+ & mut self ,
632+ poll : & mio:: Registry ,
660633 token : mio:: Token ,
661- interest : mio:: Ready ,
662- opts : mio:: PollOpt ,
634+ interest : mio:: Interest ,
663635 ) -> io:: Result < ( ) > {
664- EventedFd ( & self . devfile . as_raw_fd ( ) ) . reregister ( poll, token, interest, opts )
636+ SourceFd ( & self . as_raw_fd ( ) ) . reregister ( poll, token, interest)
665637 }
666638
667- fn deregister ( & self , poll : & mio:: Poll ) -> io:: Result < ( ) > {
668- EventedFd ( & self . devfile . as_raw_fd ( ) ) . deregister ( poll)
639+ fn deregister ( & mut self , poll : & mio:: Registry ) -> io:: Result < ( ) > {
640+ SourceFd ( & self . as_raw_fd ( ) ) . deregister ( poll)
669641 }
670642}
671643
672- #[ cfg( feature = "async-tokio" ) ]
673- pub struct PinStream {
674- evented : PollEvented < AsyncPinPoller > ,
675- skipped_first_event : bool ,
644+ #[ cfg( any( feature = "async-tokio" , feature = "mio-evented" ) ) ]
645+ impl AsRawFd for AsyncPinPoller {
646+ fn as_raw_fd ( & self ) -> RawFd {
647+ self . devfile . as_raw_fd ( )
648+ }
676649}
677650
678651#[ 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- }
652+ pub struct PinStream {
653+ evented : AsyncFd < AsyncPinPoller > ,
654+ skipped_first_event : bool ,
686655}
687656
688657#[ cfg( feature = "async-tokio" ) ]
689658impl PinStream {
690659 pub fn init ( pin : Pin ) -> Result < Self > {
691660 Ok ( PinStream {
692- evented : PollEvented :: new ( pin. get_async_poller ( ) ?, & Handle :: default ( ) ) ?,
661+ evented : AsyncFd :: new ( pin. get_async_poller ( ) ?) ?,
693662 skipped_first_event : false ,
694663 } )
695664 }
696665}
697666
698667#[ cfg( feature = "async-tokio" ) ]
699668impl 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- }
669+ type Item = Result < ( ) > ;
670+
671+ fn poll_next (
672+ mut self : std :: pin :: Pin < & mut Self > ,
673+ cx : & mut std :: task :: Context < ' _ > ,
674+ ) -> Poll < Option < Self :: Item > > {
675+ loop {
676+ let mut guard = ready ! ( self . evented . poll_read_ready ( cx ) ) ? ;
677+ guard . clear_ready ( ) ;
678+ if self . skipped_first_event {
679+ return Poll :: Ready ( Some ( Ok ( ( ) ) ) ) ;
680+ } else {
681+ self . skipped_first_event = true ;
713682 }
714- Async :: NotReady => Async :: NotReady ,
715- } )
683+ }
716684 }
717685}
718686
@@ -729,18 +697,13 @@ impl PinValueStream {
729697
730698#[ cfg( feature = "async-tokio" ) ]
731699impl 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- }
700+ type Item = Result < u8 > ;
701+
702+ fn poll_next (
703+ mut self : std:: pin:: Pin < & mut Self > ,
704+ cx : & mut std:: task:: Context < ' _ > ,
705+ ) -> Poll < Option < Self :: Item > > {
706+ ready ! ( std:: pin:: Pin :: new( & mut self . 0 ) . poll_next( cx) ) ;
707+ Poll :: Ready ( Some ( Ok ( self . get_value ( ) ?) ) )
745708 }
746709}
0 commit comments