Example on how to cancel a stream from within a FnMut closure
- Dominant language
- Rust
- Stars
- 168
- Forks
- 9
- PR merge metrics
- No merged PRs in 30d
Description
Hi, i'd like to preface this with that I am a novice rust user.
From all the examples and test code for this crate, I only see examples that purposely drop the trigger in outer scope, before the tokio runtime is ran. However, all my use cases revolves around shutting down one or more streams based on an external event, all whom are detected within some stream itself.
Example:
```rust
/// MWE
extern crate signal_hook;
extern crate stream_cancel;
extern crate tokio;
use signal_hook::iterator::Signals;
use stream_cancel::{StreamExt, Tripwire};
use tokio::prelude::*;
use tokio::runtime::current_thread;
fn main() -> Result<(), Box> {
println!("MWE");
// Create a (currently single threaded) runtime to execute event loop.
let mut runtime = current_thread::Runtime::new()?;
let (trigger, tripwire) = Tripwire::new();
// Setup signal handling
let signal_future = Signals::new(&[signal_hook::SIGINT])?
.into_async()?
.take_until(tripwire)
.for_each(|sig| {
match sig {
signal_hook::SIGINT => {
println!("got SIGINT, exiting");
trigger.cancel() // <-- This is an error
}
_ => unreachable!(),
}
Ok(())
}).map_err(|_e| println!("error print"));
runtime.spawn(signal_future);
// Kick off the singel threaded runtime
runtime.run()?;
Ok(())
}
```
Which results in the following error
```
error[E0507]: cannot move out of captured outer variable in an `FnMut` closure
--> src/main.rs:28:21
|
18 | let (trigger, tripwire) = Tripwire::new();
| ------- captured outer variable
...
28 | trigger.cancel() // <-- This is an error
| ^^^^^^^ cannot move out of captured outer variable in an `FnMut` closure
```
Which is totally reasonable, however, I'm at a loss on how to implement this functionality when all the examples rely on explicitly dropping the the object, which as far as I understand, cannot occur within a closure.
Can anyone assist and mayhaps add a documentation example for this use-case?
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.