#interrupt #async #future #points #check #interruptible #threads-fibers

tokio-interruptible-future

在指定的检查点轻松中断异步代码。它对中断线程/纤程非常有用。

27 个版本 (8 个主要版本)

8.2.0 2022年7月27日
8.0.0 2022年2月20日
7.0.0 2022年2月20日
6.0.0 2022年2月20日
0.11.2 2022年2月12日

#31 in #interrupt

Download history • Rust 包仓库 41/week @ 2024-04-13 • Rust 包仓库 19/week @ 2024-06-29 • Rust 包仓库 311/week @ 2024-07-06 • Rust 包仓库 224/week @ 2024-07-27 • Rust 包仓库

535 每月下载量
2 crates 中使用

Apache-2.0

7KB
108 代码行数,不包括注释

tokio-interruptible-future

在指定的检查点轻松中断异步代码。它对中断线程/纤程非常有用。

免责声明:测试不足。

待办事项:添加文档。

建议与 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
    }
}

依赖项

~3–5MB
~84K SLoC