1个不稳定版本
使用旧的Rust 2015
| 0.1.0 | 2018年12月7日 |
|---|
#1128 in 异步
11KB
160 行
鳄鱼 🐊
鳄鱼是一个小巧的crate,用于从future获取输出值
#![feature(async_await)]
#![feature(futures_api)]
#[macro_use] extern crate alligator;
async fn hello_world() -> &'static str {
"Hello World"
}
fn main() {
println!("{}", later!{ hello_world() });
}
lib.rs:
鳄鱼 🐊
鳄鱼是一个小巧的crate,用于从future获取输出值
Later,鳄鱼中唯一的结构,是一个围绕实现 Future 的对象包装器。使用 Later 的目的是仅在需要输出值时才轮询包含的future。
鳄鱼的目标是提供一种简单的方法来获取大多数 Future 实现的值。使用 Later 包装的future的实现要求见 Future Requirements 部分。
问题
Later 有一个问题,它使用 Pin::new_unchecked 与 poll。这样做是为了让 Later 与返回 async fn 的值一起工作,该值没有实现 UnPin。
Future Requirements
要使用实现 Future 的对象与 Later,poll 方法需要按以下方式实现。
- 必须使用
poll的 localWaker 参数。 - 必须只在下一次调用 poll 将返回 Poll::Ready 时,使用参数的唤醒调用(或任何从参数派生的 Waker)。
示例
// `l!` and `later!` macros are just shortcuts for Later::new
let do_later = l!{ get_fut() };
// Do work that doesn't require or
// use the Output of the future returned
// by get_fut
// Prints the Output value of the future
println!("{}", do_later);
注意
遗憾的是,鳄鱼不是 #[no_std] 兼容的。 Later 使用 std 线程和同步机制等待 future 轮询完成。