Skip to content

Commit 5911fb7

Browse files
Migration to 'tokio' crate from crate 'tokio-core'
1 parent 6c937b9 commit 5911fb7

File tree

6 files changed

+112
-69
lines changed

6 files changed

+112
-69
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@
1111
# Generated by Cargo
1212
Cargo.lock
1313
/target/
14+
15+
# Intellij IDEA
16+
.idea

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [master] - Unreleased
44

5+
### Changed
6+
7+
- Migrated to 'tokio' crate.
8+
59
## [0.5.3] - 2018-04-19
610

711
### Fixed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ readme = "README.md"
1111

1212
[features]
1313
mio-evented = ["mio"]
14-
tokio = ["futures", "tokio-core", "mio-evented"]
14+
use_tokio = ["futures", "tokio", "mio-evented"]
1515

1616
[dependencies]
1717
futures = { version = "0.1", optional = true }
1818
nix = "0.10.0"
1919
mio = { version = "0.6", optional = true }
20-
tokio-core = { version = "0.1", optional = true }
20+
tokio = { version = "0.1", optional = true }

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ The following features are planned for the library:
8383
- [ ] Support for configuring whether a pin is active low/high
8484
- [x] Support for configuring interrupts on GPIO
8585
- [x] Support for polling on GPIO with configured interrupt
86-
- [x] Support for asynchronous polling using `mio` or `tokio-core` (requires
87-
enabling the `mio-evented` or `tokio` crate features, respectively)
86+
- [x] Support for asynchronous polling using `mio` or `tokio` (requires
87+
enabling the `mio-evented` or `use_tokio` crate features, respectively)
8888

8989
Cross Compiling
9090
---------------

examples/tokio.rs

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,47 @@
1-
#[cfg(feature = "tokio")]
1+
#[cfg(feature = "use_tokio")]
22
extern crate futures;
3-
#[cfg(feature = "tokio")]
3+
#[cfg(feature = "use_tokio")]
44
extern crate sysfs_gpio;
5-
#[cfg(feature = "tokio")]
6-
extern crate tokio_core;
5+
#[cfg(feature = "use_tokio")]
6+
extern crate tokio;
77

8-
#[cfg(feature = "tokio")]
9-
use futures::{Future, Stream};
10-
#[cfg(feature = "tokio")]
11-
use sysfs_gpio::{Direction, Edge, Pin};
12-
#[cfg(feature = "tokio")]
8+
#[cfg(feature = "use_tokio")]
139
use std::env;
14-
#[cfg(feature = "tokio")]
15-
use tokio_core::reactor::Core;
1610

17-
#[cfg(feature = "tokio")]
11+
#[cfg(feature = "use_tokio")]
12+
use futures::{Future, lazy, Stream};
13+
14+
#[cfg(feature = "use_tokio")]
15+
use sysfs_gpio::{Direction, Edge, Pin};
16+
17+
#[cfg(feature = "use_tokio")]
1818
fn stream(pin_nums: Vec<u64>) -> sysfs_gpio::Result<()> {
1919
// NOTE: this currently runs forever and as such if
2020
// the app is stopped (Ctrl-C), no cleanup will happen
2121
// and the GPIO will be left exported. Not much
2222
// can be done about this as Rust signal handling isn't
2323
// really present at the moment. Revisit later.
2424
let pins: Vec<_> = pin_nums.iter().map(|&p| (p, Pin::new(p))).collect();
25-
let mut l = Core::new()?;
26-
let handle = l.handle();
27-
for &(i, ref pin) in pins.iter() {
28-
pin.export()?;
29-
pin.set_direction(Direction::In)?;
30-
pin.set_edge(Edge::BothEdges)?;
31-
handle.spawn(pin.get_value_stream(&handle)?
32-
.for_each(move |val| {
33-
println!("Pin {} changed value to {}", i, val);
34-
Ok(())
35-
})
36-
.map_err(|_| ()));
37-
}
38-
// Wait forever for events
39-
loop {
40-
l.turn(None)
41-
}
25+
let task = lazy(move || {
26+
for &(i, ref pin) in pins.iter() {
27+
pin.export().unwrap();
28+
pin.set_direction(Direction::In).unwrap();
29+
pin.set_edge(Edge::BothEdges).unwrap();
30+
tokio::spawn(pin.get_value_stream().unwrap()
31+
.for_each(move |val| {
32+
println!("Pin {} changed value to {}", i, val);
33+
Ok(())
34+
})
35+
.map_err(|_| ()));
36+
}
37+
Ok(())
38+
});
39+
tokio::run(task);
40+
41+
Ok(())
4242
}
4343

44-
#[cfg(feature = "tokio")]
44+
#[cfg(feature = "use_tokio")]
4545
fn main() {
4646
let pins: Vec<u64> = env::args()
4747
.skip(1)
@@ -54,7 +54,7 @@ fn main() {
5454
}
5555
}
5656

57-
#[cfg(not(feature = "tokio"))]
57+
#[cfg(not(feature = "use_tokio"))]
5858
fn main() {
5959
println!("This example requires the `tokio` feature to be enabled.");
6060
}

src/lib.rs

Lines changed: 70 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -41,38 +41,36 @@
4141
//! }
4242
//! ```
4343
44-
#[cfg(feature = "tokio")]
44+
#[cfg(feature = "use_tokio")]
4545
extern crate futures;
4646
#[cfg(feature = "mio-evented")]
4747
extern crate mio;
4848
extern crate nix;
49-
#[cfg(feature = "tokio")]
50-
extern crate tokio_core;
49+
#[cfg(feature = "use_tokio")]
50+
extern crate tokio;
5151

52-
#[cfg(feature = "tokio")]
53-
use futures::{Async, Poll, Stream};
52+
use std::fs;
53+
use std::fs::File;
54+
use std::io::{self, SeekFrom};
55+
use std::io::prelude::*;
56+
use std::os::unix::prelude::*;
57+
use std::path::Path;
5458

59+
#[cfg(feature = "use_tokio")]
60+
use futures::{Async, Poll, Stream};
5561
#[cfg(feature = "mio-evented")]
5662
use mio::Evented;
5763
#[cfg(feature = "mio-evented")]
5864
use mio::unix::EventedFd;
59-
6065
#[cfg(any(target_os = "linux", target_os = "android"))]
6166
use nix::sys::epoll::*;
6267
use nix::unistd::close;
68+
#[cfg(feature = "use_tokio")]
69+
use tokio::reactor::{Handle, PollEvented};
6370

64-
use std::io::prelude::*;
65-
use std::os::unix::prelude::*;
66-
use std::io::{self, SeekFrom};
67-
use std::fs;
68-
use std::fs::File;
69-
use std::path::Path;
70-
71-
#[cfg(feature = "tokio")]
72-
use tokio_core::reactor::{Handle, PollEvented};
71+
pub use error::Error;
7372

7473
mod error;
75-
pub use error::Error;
7674

7775
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7876
pub struct Pin {
@@ -446,29 +444,57 @@ impl Pin {
446444

447445
/// Get a Stream of pin interrupts for this pin
448446
///
449-
/// The PinStream object can be used with the `tokio-core` crate. You should probably call
447+
/// The PinStream object can be used with the `tokio` crate. You should probably call
450448
/// `set_edge()` before using this.
451449
///
452-
/// This method is only available when the `tokio` crate feature is enabled.
453-
#[cfg(feature = "tokio")]
454-
pub fn get_stream(&self, handle: &Handle) -> Result<PinStream> {
455-
PinStream::init(self.clone(), handle)
450+
/// This method is only available when the `use_tokio` crate feature is enabled.
451+
#[cfg(feature = "use_tokio")]
452+
pub fn get_stream_with_handle(&self, handle: &Handle) -> Result<PinStream> {
453+
PinStream::init_with_handle(self.clone(), handle)
454+
}
455+
456+
457+
/// Get a Stream of pin interrupts for this pin
458+
///
459+
/// The PinStream object can be used with the `tokio` crate. You should probably call
460+
/// `set_edge()` before using this.
461+
///
462+
/// This method is only available when the `use_tokio` crate feature is enabled.
463+
#[cfg(feature = "use_tokio")]
464+
pub fn get_stream(&self) -> Result<PinStream> {
465+
PinStream::init(self.clone())
456466
}
457467

458468
/// Get a Stream of pin values for this pin
459469
///
460-
/// The PinStream object can be used with the `tokio-core` crate. You should probably call
470+
/// The PinStream object can be used with the `tokio` crate. You should probably call
461471
/// `set_edge(Edge::BothEdges)` before using this.
462472
///
463473
/// Note that the values produced are the value of the pin as soon as we get to handling the
464474
/// interrupt in userspace. Each time this stream produces a value, a change has occurred, but
465475
/// it could end up producing the same value multiple times if the value has changed back
466476
/// between when the interrupt occurred and when the value was read.
467477
///
468-
/// This method is only available when the `tokio` crate feature is enabled.
469-
#[cfg(feature = "tokio")]
470-
pub fn get_value_stream(&self, handle: &Handle) -> Result<PinValueStream> {
471-
Ok(PinValueStream(PinStream::init(self.clone(), handle)?))
478+
/// This method is only available when the `use_tokio` crate feature is enabled.
479+
#[cfg(feature = "use_tokio")]
480+
pub fn get_value_stream_with_handle(&self, handle: &Handle) -> Result<PinValueStream> {
481+
Ok(PinValueStream(PinStream::init_with_handle(self.clone(), handle)?))
482+
}
483+
484+
/// Get a Stream of pin values for this pin
485+
///
486+
/// The PinStream object can be used with the `tokio` crate. You should probably call
487+
/// `set_edge(Edge::BothEdges)` before using this.
488+
///
489+
/// Note that the values produced are the value of the pin as soon as we get to handling the
490+
/// interrupt in userspace. Each time this stream produces a value, a change has occurred, but
491+
/// it could end up producing the same value multiple times if the value has changed back
492+
/// between when the interrupt occurred and when the value was read.
493+
///
494+
/// This method is only available when the `use_tokio` crate feature is enabled.
495+
#[cfg(feature = "use_tokio")]
496+
pub fn get_value_stream(&self) -> Result<PinValueStream> {
497+
Ok(PinValueStream(PinStream::init(self.clone())?))
472498
}
473499
}
474500

@@ -538,7 +564,7 @@ impl PinPoller {
538564
/// making this call. This call makes use of epoll under the
539565
/// covers. To poll on multiple GPIOs or other event sources,
540566
/// poll asynchronously using the integration with either `mio`
541-
/// or `tokio_core`.
567+
/// or `tokio`.
542568
///
543569
/// This function will return Some(value) of the pin if a change is
544570
/// detected or None if a timeout occurs. Note that the value provided
@@ -613,23 +639,33 @@ impl Evented for AsyncPinPoller {
613639
}
614640
}
615641

616-
#[cfg(feature = "tokio")]
642+
#[cfg(feature = "use_tokio")]
617643
pub struct PinStream {
618644
evented: PollEvented<AsyncPinPoller>,
619645
skipped_first_event: bool,
620646
}
621647

622-
#[cfg(feature = "tokio")]
648+
#[cfg(feature = "use_tokio")]
623649
impl PinStream {
624-
pub fn init(pin: Pin, handle: &Handle) -> Result<Self> {
650+
pub fn init_with_handle(pin: Pin, handle: &Handle) -> Result<Self> {
625651
Ok(PinStream {
626652
evented: PollEvented::new(pin.get_async_poller()?, &handle)?,
627653
skipped_first_event: false,
628654
})
629655
}
630656
}
631657

632-
#[cfg(feature = "tokio")]
658+
#[cfg(feature = "use_tokio")]
659+
impl PinStream {
660+
pub fn init(pin: Pin) -> Result<Self> {
661+
Ok(PinStream {
662+
evented: PollEvented::new(pin.get_async_poller()?, &Handle::default())?,
663+
skipped_first_event: false,
664+
})
665+
}
666+
}
667+
668+
#[cfg(feature = "use_tokio")]
633669
impl Stream for PinStream {
634670
type Item = ();
635671
type Error = Error;
@@ -650,18 +686,18 @@ impl Stream for PinStream {
650686
}
651687
}
652688

653-
#[cfg(feature = "tokio")]
689+
#[cfg(feature = "use_tokio")]
654690
pub struct PinValueStream(PinStream);
655691

656-
#[cfg(feature = "tokio")]
692+
#[cfg(feature = "use_tokio")]
657693
impl PinValueStream {
658694
#[inline]
659695
fn get_value(&mut self) -> Result<u8> {
660696
get_value_from_file(&mut self.0.evented.get_mut().devfile)
661697
}
662698
}
663699

664-
#[cfg(feature = "tokio")]
700+
#[cfg(feature = "use_tokio")]
665701
impl Stream for PinValueStream {
666702
type Item = u8;
667703
type Error = Error;

0 commit comments

Comments
 (0)