-
Notifications
You must be signed in to change notification settings - Fork 44
Futures 0.3 #60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Futures 0.3 #60
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fcafe62
Futures 0.3
benjumanji d6f3d85
Strip dead code
benjumanji 9f33272
Guard pin behind tokio feature
benjumanji c412bdc
switch to pin-project-lite
benjumanji ed8ba13
Bump MSRV to 1.39.0 for newer tokio stuff
posborne 29c45ca
Fix and cleanup tokio example using async/await
posborne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
language: rust | ||
rust: | ||
- 1.31.1 | ||
- 1.39.0 | ||
- stable | ||
- beta | ||
- nightly | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,44 @@ | ||
#[cfg(feature = "use_tokio")] | ||
extern crate futures; | ||
#[cfg(feature = "use_tokio")] | ||
extern crate sysfs_gpio; | ||
#[cfg(feature = "use_tokio")] | ||
extern crate tokio; | ||
// Copyright (c) 2020. The sysfs-gpio Authors. | ||
|
||
#[cfg(feature = "use_tokio")] | ||
use futures::future::join_all; | ||
use futures::StreamExt; | ||
use std::env; | ||
|
||
#[cfg(feature = "use_tokio")] | ||
use futures::{lazy, Future, Stream}; | ||
|
||
#[cfg(feature = "use_tokio")] | ||
use sysfs_gpio::{Direction, Edge, Pin}; | ||
|
||
#[cfg(feature = "use_tokio")] | ||
fn stream(pin_nums: Vec<u64>) -> sysfs_gpio::Result<()> { | ||
async fn monitor_pin(pin: Pin) -> Result<(), sysfs_gpio::Error> { | ||
pin.export()?; | ||
pin.set_direction(Direction::In)?; | ||
pin.set_edge(Edge::BothEdges)?; | ||
let mut gpio_events = pin.get_value_stream()?; | ||
while let Some(evt) = gpio_events.next().await { | ||
let val = evt.unwrap(); | ||
println!("Pin {} changed value to {}", pin.get_pin_num(), val); | ||
} | ||
Ok(()) | ||
} | ||
|
||
async fn stream(pin_nums: Vec<u64>) { | ||
// NOTE: this currently runs forever and as such if | ||
// the app is stopped (Ctrl-C), no cleanup will happen | ||
// and the GPIO will be left exported. Not much | ||
// can be done about this as Rust signal handling isn't | ||
// really present at the moment. Revisit later. | ||
let pins: Vec<_> = pin_nums.iter().map(|&p| (p, Pin::new(p))).collect(); | ||
let task = lazy(move || { | ||
for &(i, ref pin) in pins.iter() { | ||
pin.export().unwrap(); | ||
pin.set_direction(Direction::In).unwrap(); | ||
pin.set_edge(Edge::BothEdges).unwrap(); | ||
tokio::spawn( | ||
pin.get_value_stream() | ||
.unwrap() | ||
.for_each(move |val| { | ||
println!("Pin {} changed value to {}", i, val); | ||
Ok(()) | ||
}) | ||
.map_err(|_| ()), | ||
); | ||
} | ||
Ok(()) | ||
}); | ||
tokio::run(task); | ||
|
||
Ok(()) | ||
join_all(pin_nums.into_iter().map(|p| { | ||
let pin = Pin::new(p); | ||
tokio::task::spawn(monitor_pin(pin)) | ||
})) | ||
.await; | ||
} | ||
|
||
#[cfg(feature = "use_tokio")] | ||
fn main() { | ||
#[tokio::main] | ||
async fn main() { | ||
let pins: Vec<u64> = env::args() | ||
.skip(1) | ||
.map(|a| a.parse().expect("Pins must be specified as integers")) | ||
.collect(); | ||
if pins.is_empty() { | ||
println!("Usage: ./tokio <pin> [pin ...]"); | ||
} else { | ||
stream(pins).unwrap(); | ||
stream(pins).await; | ||
} | ||
} | ||
|
||
#[cfg(not(feature = "use_tokio"))] | ||
fn main() { | ||
println!("This example requires the `tokio` feature to be enabled."); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.