5 个版本
0.2.0 | 2019年11月8日 |
---|---|
0.2.0-alpha.4 | 2019年11月4日 |
0.1.0-alpha.3 | 2019年10月31日 |
0.0.1 |
|
#78 in #await
49KB
1K SLoC
rustracts
Rust 库,用于在异步/await rust 中创建可取消的保险合同
rustracts = "0.2.0"
功能
可用
- FuturesContract:如果合同未被撤销,将在到期时产生一个值
- OnKillContract:如果上下文被验证无效,将产生一个值
- OptionContract:如果次要上下文已实现并且合同在之前未被撤销,将在到期时产生值
示例
use std::time::Duration;
use crate::context::cmp::GtContext;
use crate::{ContractExt, Status, FuturesContract};
fn simple_contract() {
let context: usize = 3;
let c = FuturesContract::new(Duration::from_secs(1), context, |con| -> usize { con + 5 });
if let Status::Completed(value) = futures::executor::block_on(c) {
assert_eq!(value, 8)
} else {
assert!(false)
}
}
fn voided_contract() {
let context = GtContext(3, 2); // Context is true if self.0 > self.1
let c = FuturesContract::new(Duration::from_secs(4), context, |con| -> usize {
con.0 + 5
});
let handle = std::thread::spawn({
let mcontext = c.get_context();
move || {
(*mcontext.lock().unwrap()).0 = 1; // Modify context before contract ends
}
});
if let Status::Completed(val) = futures::executor::block_on(c) {
assert_ne!(val, 1);
} else {
assert!(true); // Contract should be voided because updated value is 1 which is < 2
}
handle.join().unwrap();
}
fn updated_contract() {
let context = GtContext(3, 2); // Context is valid if self.0 > self.1
let c = FuturesContract::new(Duration::from_secs(1), context, |con| -> usize {
con.0 + 5
});
let handle = std::thread::spawn({
let mcontext = c.get_context();
move || {
(*mcontext.lock().unwrap()).0 += 2;
}
});
if let Status::Completed(value) = futures::executor::block_on(c) {
assert_eq!(value, 10);
} else {
assert!(false);
}
handle.join().unwrap();
}
依赖关系
~1MB
~15K SLoC