2个稳定版本
1.1.0 | 2018年12月16日 |
---|---|
1.0.0 | 2018年12月15日 |
#898 in 异步
38KB
531 行代码(不包括注释)
Deferred
Rust包,用于帮助执行代码逻辑的延迟执行。
想法
此包可用于特定操作,在这些操作中,您需要在不确定的时间执行逻辑的不同部分,但不能使用futures或其他异步操作。
使用
在 Cargo.toml
中记录
[dependencies]
deferred = "1.1"
您的包模块
#[macro_use]
extern crate deferred;
use deferred::*;
fn foo(v: i32) -> Deferred<i32> {
deferred!(v, [
|c| state!(c.state() + 1),
|c| foo2(c.state()).into(),
|c| state!(c.state() + 2)
])
}
fn foo2(v: i32) -> Deferred<i32> {
deferred!(v, [
|c| state!(c.state() * 2),
|c| state!(c.state() * 3)
])
}
{
let d = foo(1);
assert!(d.can_resume());
assert_eq!(d.state(), Some(&1));
let d = d.resume().unwrap();
assert!(d.can_resume());
assert_eq!(d.state(), Some(&2));
let d = d.resume().unwrap();
assert!(d.can_resume());
assert_eq!(d.state(), Some(&4));
let d = d.resume().unwrap();
assert!(d.can_resume());
assert_eq!(d.state(), Some(&12));
let d = d.resume().unwrap();
assert!(!d.can_resume());
assert_eq!(d.state(), Some(&14));
}
// IS EQUIVALENT TO:
{
let d = foo(1);
assert_eq!(d.consume(), 14);
}