Skip to content

Commit 174b8c3

Browse files
committed
Bump async dependencies
Bump tokio to version 1.x, mio to 0.7.x and futures to 0.3. Also set edition to 2018.
1 parent 12b60cf commit 174b8c3

File tree

2 files changed

+52
-90
lines changed

2 files changed

+52
-90
lines changed

Cargo.toml

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ homepage = "https://github.com/rust-embedded/rust-sysfs-gpio"
1111
documentation = "https://docs.rs/sysfs_gpio/"
1212
description = "Provides access to GPIOs using the Linux sysfs interface."
1313
readme = "README.md"
14+
edition = "2018"
1415

1516
[features]
1617
mio-evented = ["mio"]
1718
async-tokio = ["futures", "tokio", "mio-evented"]
1819

1920
[dependencies]
20-
futures = { version = "0.1", optional = true }
21+
futures = { version = "0.3", optional = true }
2122
nix = "0.22"
22-
mio = { version = "0.6", optional = true }
23-
tokio = { version = "0.1", optional = true }
23+
mio = { version = "0.7", optional = true, features = ["os-ext"]}
24+
tokio = { version = "1", optional = true, features = ["net"] }

src/lib.rs

+48-87
Original file line numberDiff line numberDiff line change
@@ -53,28 +53,27 @@ extern crate nix;
5353
#[cfg(feature = "async-tokio")]
5454
extern crate tokio;
5555

56-
use std::fs;
57-
use std::fs::File;
5856
use std::io;
5957
use std::io::prelude::*;
6058
#[cfg(any(target_os = "linux", target_os = "android", feature = "async-tokio"))]
6159
use std::io::SeekFrom;
6260
#[cfg(not(target_os = "wasi"))]
6361
use std::os::unix::prelude::*;
6462
use std::path::Path;
63+
use std::{fs, fs::File, task::Poll};
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"))]
7372
use nix::sys::epoll::*;
7473
#[cfg(not(target_os = "wasi"))]
7574
use nix::unistd::close;
7675
#[cfg(feature = "async-tokio")]
77-
use tokio::reactor::{Handle, PollEvented};
76+
use tokio::io::unix::AsyncFd;
7877

7978
pub use error::Error;
8079

@@ -472,17 +471,6 @@ impl Pin {
472471
AsyncPinPoller::new(self.pin_num)
473472
}
474473

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-
486474
/// Get a Stream of pin interrupts for this pin
487475
///
488476
/// The PinStream object can be used with the `tokio` crate. You should probably call
@@ -494,22 +482,6 @@ impl Pin {
494482
PinStream::init(*self)
495483
}
496484

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-
513485
/// Get a Stream of pin values for this pin
514486
///
515487
/// The PinStream object can be used with the `tokio` crate. You should probably call
@@ -536,9 +508,9 @@ fn extract_pin_fom_path_test() {
536508
let tok3 = Pin::extract_pin_from_path(&"../../devices/soc0/gpiochip3/gpio/gpio124");
537509
assert_eq!(124, tok3.unwrap());
538510
let err1 = Pin::extract_pin_from_path(&"/sys/CLASS/gpio/gpio");
539-
assert_eq!(true, err1.is_err());
511+
assert!(err1.is_err());
540512
let err2 = Pin::extract_pin_from_path(&"/sys/class/gpio/gpioSDS");
541-
assert_eq!(true, err2.is_err());
513+
assert!(err2.is_err());
542514
}
543515
#[cfg(not(target_os = "wasi"))]
544516
#[derive(Debug)]
@@ -643,76 +615,70 @@ impl AsyncPinPoller {
643615
}
644616

645617
#[cfg(feature = "mio-evented")]
646-
impl Evented for AsyncPinPoller {
618+
impl Source for AsyncPinPoller {
647619
fn register(
648-
&self,
649-
poll: &mio::Poll,
620+
&mut self,
621+
poll: &mio::Registry,
650622
token: mio::Token,
651-
interest: mio::Ready,
652-
opts: mio::PollOpt,
623+
interest: mio::Interest,
653624
) -> io::Result<()> {
654-
EventedFd(&self.devfile.as_raw_fd()).register(poll, token, interest, opts)
625+
SourceFd(&self.as_raw_fd()).register(poll, token, interest)
655626
}
656627

657628
fn reregister(
658-
&self,
659-
poll: &mio::Poll,
629+
&mut self,
630+
poll: &mio::Registry,
660631
token: mio::Token,
661-
interest: mio::Ready,
662-
opts: mio::PollOpt,
632+
interest: mio::Interest,
663633
) -> io::Result<()> {
664-
EventedFd(&self.devfile.as_raw_fd()).reregister(poll, token, interest, opts)
634+
SourceFd(&self.as_raw_fd()).reregister(poll, token, interest)
665635
}
666636

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)
669639
}
670640
}
671641

672642
#[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+
}
676647
}
677648

678649
#[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,
686653
}
687654

688655
#[cfg(feature = "async-tokio")]
689656
impl PinStream {
690657
pub fn init(pin: Pin) -> Result<Self> {
691658
Ok(PinStream {
692-
evented: PollEvented::new(pin.get_async_poller()?, &Handle::default())?,
659+
evented: AsyncFd::new(pin.get_async_poller()?)?,
693660
skipped_first_event: false,
694661
})
695662
}
696663
}
697664

698665
#[cfg(feature = "async-tokio")]
699666
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;
713680
}
714-
Async::NotReady => Async::NotReady,
715-
})
681+
}
716682
}
717683
}
718684

@@ -729,18 +695,13 @@ impl PinValueStream {
729695

730696
#[cfg(feature = "async-tokio")]
731697
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()?)))
745706
}
746707
}

0 commit comments

Comments
 (0)