#interrupt #async #points #check #future #interrupted #interruptible

tokio-interruptible-future

Easily interrupt async code in given check points. It's useful to interrupt threads/fibers.

27 releases (8 major breaking)

8.2.0 Jul 27, 2022
8.0.0 Feb 20, 2022
7.0.0 Feb 20, 2022
6.0.0 Feb 20, 2022
0.11.2 Feb 12, 2022

#3 in #interrupted

Download history 62/week @ 2024-02-23 21/week @ 2024-03-01

83 downloads per month
Used in 2 crates

Apache-2.0

7KB
108 lines

tokio-interruptible-future

Easily interrupt async code in given check points. It's useful to interrupt threads/fibers.

DISCLAIMER: Not enough tested.

TODO: Add documentation.

Recommended to use together with tokio-notify-drop.

#[derive(Debug, PartialEq, Eq)]
enum MyError {
    Interrupted(InterruptError),
}
impl From<InterruptError> for MyError {
    fn from(value: InterruptError) -> Self {
        Self::Interrupted(value)
    }
}
struct Test {
    interrupt_notifier: Notify,
}
impl Interruptible for Test {
    fn interrupt_notifier(&self) -> &Notify {
        &self.interrupt_notifier
    }
}
impl Test {
    pub fn new() -> Self {
        Self {
            interrupt_notifier: Notify::new()
        }
    }
    pub async fn f(&self) -> Result<(), MyError> {
        self.interruptible/*::<(), MyError>*/(async {
            loop {
                self.interrupt(); // In real code called from another fiber or another thread.
                self.check_for_interrupt::<MyError>().await?;
            }
        }).await
    }
    pub async fn g(&self) -> Result<u8, MyError> {
        self.interruptible::<u8, MyError>(async {
            Ok(123)
        }).await
    }
}

Dependencies

~3.5–5MB
~86K SLoC