#tokio #ctrlc #keyboard #interrupt

tokio-ctrlc-error

Easy handling of keyboard interrupts with tokio

1 unstable release

✓ Uses Rust 2018 edition

0.1.0 Aug 6, 2019

#262 in Asynchronous

Download history 93/week @ 2019-10-30 38/week @ 2019-11-06 77/week @ 2019-11-13 31/week @ 2019-11-20 50/week @ 2019-11-27 30/week @ 2019-12-04 36/week @ 2019-12-11 36/week @ 2019-12-18 29/week @ 2019-12-25 131/week @ 2020-01-01 38/week @ 2020-01-08 36/week @ 2020-01-15 38/week @ 2020-01-22 34/week @ 2020-01-29 35/week @ 2020-02-05

271 downloads per month
Used in gwasm-api

AGPL-3.0

6KB
69 lines

tokio-ctrlc-error

Build Status Docs crates-io-badge

Easy ctrl+c handling with failure and futures.

In many cases, a ctrl+c event from the user is hardly different from a fatal application error. This crate, inspired by Python's InterruptedException makes it easy to treat ctrl+c in precisely such a way.

Docs

API Documentation (Releases)

Examples

    use futures::prelude::*;
    use tokio_ctrlc_error::AsyncCtrlc;

    fn lengthy_task() -> impl Future<Item = (), Error = failure::Error> {
        futures::future::ok(())
    }

    let task = lengthy_task().ctrlc_as_error();
    let mut rt = tokio::runtime::Runtime::new().unwrap();
    let res = rt.block_on(task);
    println!("{:?}", res);

Usage notes

ctrlc_as_error has the same semantics as select and will return either the result of the future or an KeyboardInterrupt error, whichever occurs first. In particular, the interrupt is intercepted only for those futures in the chain that precede the call. For example:

    use std::time::Duration;
    use futures::prelude::*;
    use tokio_ctrlc_error::AsyncCtrlc;

    fn sleep() -> impl Future<Item = (), Error = failure::Error> {
        // The sleep is very short, so that the tests don't take too much time
        tokio_timer::sleep(Duration::from_millis(1)).from_err()
    }

    let task = sleep()
        .ctrlc_as_error()
        .and_then(|_| sleep());
    let mut rt = tokio::runtime::Runtime::new().unwrap();
    let res = rt.block_on(task);

Here, the interrupt will be handled only during the first sleep. During the second sleep, the default handling of the signal will take place.

Dependencies

~2MB
~37K SLoC