@@ -53,28 +53,29 @@ 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 } ;
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 std:: task:: Poll ;
77
+ #[ cfg( feature = "async-tokio" ) ]
78
+ use tokio:: io:: unix:: AsyncFd ;
78
79
79
80
pub use error:: Error ;
80
81
@@ -472,17 +473,6 @@ impl Pin {
472
473
AsyncPinPoller :: new ( self . pin_num )
473
474
}
474
475
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
476
/// Get a Stream of pin interrupts for this pin
487
477
///
488
478
/// The PinStream object can be used with the `tokio` crate. You should probably call
@@ -494,22 +484,6 @@ impl Pin {
494
484
PinStream :: init ( * self )
495
485
}
496
486
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
487
/// Get a Stream of pin values for this pin
514
488
///
515
489
/// The PinStream object can be used with the `tokio` crate. You should probably call
@@ -536,9 +510,9 @@ fn extract_pin_fom_path_test() {
536
510
let tok3 = Pin :: extract_pin_from_path ( & "../../devices/soc0/gpiochip3/gpio/gpio124" ) ;
537
511
assert_eq ! ( 124 , tok3. unwrap( ) ) ;
538
512
let err1 = Pin :: extract_pin_from_path ( & "/sys/CLASS/gpio/gpio" ) ;
539
- assert_eq ! ( true , err1. is_err( ) ) ;
513
+ assert ! ( err1. is_err( ) ) ;
540
514
let err2 = Pin :: extract_pin_from_path ( & "/sys/class/gpio/gpioSDS" ) ;
541
- assert_eq ! ( true , err2. is_err( ) ) ;
515
+ assert ! ( err2. is_err( ) ) ;
542
516
}
543
517
#[ cfg( not( target_os = "wasi" ) ) ]
544
518
#[ derive( Debug ) ]
@@ -643,76 +617,70 @@ impl AsyncPinPoller {
643
617
}
644
618
645
619
#[ cfg( feature = "mio-evented" ) ]
646
- impl Evented for AsyncPinPoller {
620
+ impl Source for AsyncPinPoller {
647
621
fn register (
648
- & self ,
649
- poll : & mio:: Poll ,
622
+ & mut self ,
623
+ poll : & mio:: Registry ,
650
624
token : mio:: Token ,
651
- interest : mio:: Ready ,
652
- opts : mio:: PollOpt ,
625
+ interest : mio:: Interest ,
653
626
) -> io:: Result < ( ) > {
654
- EventedFd ( & self . devfile . as_raw_fd ( ) ) . register ( poll, token, interest, opts )
627
+ SourceFd ( & self . as_raw_fd ( ) ) . register ( poll, token, interest)
655
628
}
656
629
657
630
fn reregister (
658
- & self ,
659
- poll : & mio:: Poll ,
631
+ & mut self ,
632
+ poll : & mio:: Registry ,
660
633
token : mio:: Token ,
661
- interest : mio:: Ready ,
662
- opts : mio:: PollOpt ,
634
+ interest : mio:: Interest ,
663
635
) -> io:: Result < ( ) > {
664
- EventedFd ( & self . devfile . as_raw_fd ( ) ) . reregister ( poll, token, interest, opts )
636
+ SourceFd ( & self . as_raw_fd ( ) ) . reregister ( poll, token, interest)
665
637
}
666
638
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)
669
641
}
670
642
}
671
643
672
644
#[ cfg( feature = "async-tokio" ) ]
673
- pub struct PinStream {
674
- evented : PollEvented < AsyncPinPoller > ,
675
- skipped_first_event : bool ,
645
+ impl AsRawFd for AsyncPinPoller {
646
+ fn as_raw_fd ( & self ) -> RawFd {
647
+ self . devfile . as_raw_fd ( )
648
+ }
676
649
}
677
650
678
651
#[ 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 ,
686
655
}
687
656
688
657
#[ cfg( feature = "async-tokio" ) ]
689
658
impl PinStream {
690
659
pub fn init ( pin : Pin ) -> Result < Self > {
691
660
Ok ( PinStream {
692
- evented : PollEvented :: new ( pin. get_async_poller ( ) ?, & Handle :: default ( ) ) ?,
661
+ evented : AsyncFd :: new ( pin. get_async_poller ( ) ?) ?,
693
662
skipped_first_event : false ,
694
663
} )
695
664
}
696
665
}
697
666
698
667
#[ cfg( feature = "async-tokio" ) ]
699
668
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
- }
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 ;
713
682
}
714
- Async :: NotReady => Async :: NotReady ,
715
- } )
683
+ }
716
684
}
717
685
}
718
686
@@ -729,18 +697,13 @@ impl PinValueStream {
729
697
730
698
#[ cfg( feature = "async-tokio" ) ]
731
699
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
- }
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 ( ) ?) ) )
745
708
}
746
709
}
0 commit comments