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
535 每月下载量
在 2 crates 中使用
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