2个稳定版本
1.1.0 | 2022年4月19日 |
---|---|
1.0.0 | 2021年11月26日 |
#1388 in 异步
48KB
359 行
async-ops
这个crate提供了一种使用一些std::ops traits与Futures的方式。要使用std::ops trait与Future,首先需要使用async_ops::on将Future包装成Async。然后,只要Future的Output类型实现了支持的std::ops trait,那么Async实例也将实现相同的std::ops trait。
另一个选项是使用async_ops::assignable!将Future包装成Async,以启用在Future上使用std::ops traits的Assign变体。
示例
在编写async代码时,通常会对通过std::ops支持的运算进行操作。例如,添加数字可能看起来像这样
use futures_executor::block_on;
// Immediately returning a number is done for simplicity and production code
// wouldn't just immediately return a value.
let a = async { 40 };
let b = async { 2 };
let result = async { a.await + b.await };
assert_eq!(42, block_on(result));
实际上,上述代码实现并不最佳,因为a和b是顺序而不是并发地await。正确的解决方案是使用join!来并发await两个值,如下所示
use futures_executor::block_on;
use futures_util::join;
let a = async { 40 };
let b = async { 2 };
let result = async {
let (a, b) = join!(a, b);
a + b
};
assert_eq!(42, block_on(result));
或者,只需使用async_ops::on就可以在单行中完成上述示例
use futures_executor::block_on;
let a = async { 40 };
let b = async { 2 };
let result = async { (async_ops::on(a) + b).await };
assert_eq!(42, block_on(result));
请注意,async_ops::on示例也会并发await两个值。
支持 std::ops
特性
加法
Async
实现了 Add<Rhs> where Rhs: Future
特性,当包装的 Future::Output
类型实现了 Add<Rhs::Output>
。加法的结果类型是 Async<impl Future<Output = <Future::Output as Add<Rhs::Output>>::Output>>
。
use futures_executor::block_on;
let a = async { 40 };
let b = async { 2 };
let result = async { (async_ops::on(a) + b).await };
assert_eq!(42, block_on(result));
加法赋值
Async
实现了 AddAssign<Rhs> where Rhs: Future
特性,当包装的 Future
类型实现了 Assignable<<Async<Future> as Add<Rhs>>::Output>
,这反过来又要求 Future::Output
类型实现 Add<Rhs::Output>
。
use futures_executor::block_on;
let a = async { 40 };
let b = async { 2 };
let result = async {
async_ops::assignable!(a);
a += b;
a.await
};
assert_eq!(42, block_on(result));
位与
Async
实现了 BitAnd<Rhs> where Rhs: Future
特性,当包装的 Future::Output
类型实现了 BitAnd<Rhs::Output>
。位与的结果类型是 Async<impl Future<Output = <Future::Output as BitAnd<Rhs::Output>>::Output>>
。
use futures_executor::block_on;
let a = async { 110 };
let b = async { 59 };
let result = async { (async_ops::on(a) & b).await };
assert_eq!(42, block_on(result));
位与赋值
Async
实现了 BitAndAssign<Rhs> where Rhs: Future
,当包装的 Future
类型实现了 Assignable<<Async<Future> as BitAnd<Rhs>>::Output>
,这反过来又要求 Future::Output
类型实现 BitAnd<Rhs::Output>
。
use futures_executor::block_on;
let a = async { 110 };
let b = async { 59 };
let result = async {
async_ops::assignable!(a);
a &= b;
a.await
};
assert_eq!(42, block_on(result));
BitOr
Async
实现了 BitOr<Rhs> where Rhs: Future
,当包装的 Future::Output
类型实现了 BitOr<Rhs::Output>
。按位或的结果类型为 Async<impl Future<Output = <Future::Output as BitOr<Rhs::Output>>::Output>>
。
use futures_executor::block_on;
let a = async { 40 };
let b = async { 10 };
let result = async { (async_ops::on(a) | b).await };
assert_eq!(42, block_on(result));
BitOrAssign
Async
实现了 BitOrAssign<Rhs> where Rhs: Future
,当包装的 Future
类型实现了 Assignable<<Async<Future> as BitOr<Rhs>>::Output>
,这反过来又要求 Future::Output
类型实现 BitOr<Rhs::Output>
。
use futures_executor::block_on;
let a = async { 40 };
let b = async { 10 };
let result = async {
async_ops::assignable!(a);
a |= b;
a.await
};
assert_eq!(42, block_on(result));
BitXor
Async
实现了 BitXor<Rhs> where Rhs: Future
,当包装的 Future::Output
类型实现了 BitXor<Rhs::Output>
。按位异或的结果类型为 Async<impl Future<Output = <Future::Output as BitXor<Rhs::Output>>::Output>>
。
use futures_executor::block_on;
let a = async { 38 };
let b = async { 12 };
let result = async { (async_ops::on(a) ^ b).await };
assert_eq!(42, block_on(result));
BitXorAssign
Async
实现了BitXorAssign<Rhs> where Rhs: Future
,当封装的Future
类型实现了Assignable<<Async<Future> as BitXor<Rhs>>::Output>
时,这反过来又要求Future::Output
类型实现BitXor<Rhs::Output>
。
use futures_executor::block_on;
let a = async { 38 };
let b = async { 12 };
let result = async {
async_ops::assignable!(a);
a ^= b;
a.await
};
assert_eq!(42, block_on(result));
除法
Async
实现了Div<Rhs> where Rhs: Future
,当封装的Future::Output
类型实现了Div<Rhs::Output>
。除法的结果类型是Async<impl Future<Output = <Future::Output as Div<Rhs::Output>>::Output>>
。
use futures_executor::block_on;
let a = async { 84 };
let b = async { 2 };
let result = async { (async_ops::on(a) / b).await };
assert_eq!(42, block_on(result));
除法赋值
Async
实现了DivAssign<Rhs> where Rhs: Future
,当封装的Future
类型实现了Assignable<<Async<Future> as Div<Rhs>>::Output>
时,这反过来又要求Future::Output
类型实现Div<Rhs::Output>
。
use futures_executor::block_on;
let a = async { 84 };
let b = async { 2 };
let result = async {
async_ops::assignable!(a);
a /= b;
a.await
};
assert_eq!(42, block_on(result));
乘法
Async
实现了 Mul<Rhs> where Rhs: Future
,当包装的 Future::Output
类型实现了 Mul<Rhs::Output>
。乘法的结果类型是 Async<impl Future<Output = <Future::Output as Mul<Rhs::Output>>::Output>>
。
use futures_executor::block_on;
let a = async { 21 };
let b = async { 2 };
let result = async { (async_ops::on(a) * b).await };
assert_eq!(42, block_on(result));
MulAssign
Async
实现了 MulAssign<Rhs> where Rhs: Future
,当包装的 Future
类型实现了 Assignable<<Async<Future> as Mul<Rhs>>::Output>
,这反过来又要求 Future::Output
类型实现 Mul<Rhs::Output>
。
use futures_executor::block_on;
let a = async { 21 };
let b = async { 2 };
let result = async {
async_ops::assignable!(a);
a *= b;
a.await
};
assert_eq!(42, block_on(result));
Neg
Async
实现了 Neg
,当包装的 Future::Output
类型实现了 Neg
。取反的结果类型是 Async<impl Future<Output = <Future::Output as Neg>::Output>>
。
use futures_executor::block_on;
let a = async { -42 };
let result = async { (-async_ops::on(a)).await };
assert_eq!(42, block_on(result));
Not
Async
实现了 Not
,当包装的 Future::Output
类型实现了 Not
。逻辑取反的结果类型是 Async<impl Future<Output = <Future::Output as Not>::Output>>
。
use futures_executor::block_on;
let a = async { 213_u8 };
let result = async { (!async_ops::on(a)).await };
assert_eq!(42, block_on(result));
Rem
Async
实现了Rem<Rhs::Output> where Rhs: Future
,当被包裹的Future::Output
类型实现了Rem<Rhs::Output>
。余数操作的结果类型为Async<impl Future<Output = <Future::Output as Rem<Rhs::Output>>::Output>>
。
use futures_executor::block_on;
let a = async { 42 };
let b = async { 5 };
let result = async { (async_ops::on(a) % b).await };
assert_eq!(2, block_on(result));
RemAssign
Async
实现了RemAssign<Rhs> where Rhs: Future
,当被包裹的Future
类型实现了Assignable<<Async<Future> as Rem<Rhs>>::Output>
,这反过来又要求Future::Output
类型实现Rem<Rhs::Output>
。
use futures_executor::block_on;
let a = async { 42 };
let b = async { 5 };
let result = async {
async_ops::assignable!(a);
a %= b;
a.await
};
assert_eq!(2, block_on(result));
Shl
Async
实现了Shl<Rhs> where Rhs: Future
,当被包裹的Future::Output
类型实现了Shl<Rhs::Output>
。左移操作的结果类型为Async<impl Future<Output = <Future::Output as Shl<Rhs::Output>>::Output>>
。
use futures_executor::block_on;
let a = async { 21 };
let b = async { 1 };
let result = async { (async_ops::on(a) << b).await };
assert_eq!(42, block_on(result));
ShlAssign
Async
实现了ShlAssign<Rhs> where Rhs: Future
,当被包裹的Future
类型实现了Assignable<<Async<Future> as Shl<Rhs>>::Output>
,这反过来又要求Future::Output
类型实现Shl<Rhs::Output>
。
use futures_executor::block_on;
let a = async { 21 };
let b = async { 1 };
let result = async {
async_ops::assignable!(a);
a <<= b;
a.await
};
assert_eq!(42, block_on(result));
Shr
Async
实现了 Shr<Rhs> where Rhs: Future
,当封装的 Future::Output
类型实现了 Shr<Rhs::Output>
。右移操作的结果类型是 Async<impl Future<Output = <Future::Output as Shr<Rhs::Output>>::Output>>
。
use futures_executor::block_on;
let a = async { 168 };
let b = async { 2 };
let result = async { (async_ops::on(a) >> b).await };
assert_eq!(42, block_on(result));
ShrAssign
Async
实现了 ShrAssign<Rhs> where Rhs: Future
,当封装的 Future
类型实现了 Assignable<<Async<Future> as Shr<Rhs>>::Output>
,这又要求 Future::Output
类型实现 Shr<Rhs::Output>
。
use futures_executor::block_on;
let a = async { 168 };
let b = async { 2 };
let result = async {
async_ops::assignable!(a);
a >>= b;
a.await
};
assert_eq!(42, block_on(result));
Sub
Async
实现了 Sub<Rhs> where Rhs: Future
,当封装的 Future::Output
类型实现了 Sub<Rhs::Output>
。减法操作的结果类型是 Async<impl Future<Output = <Future::Output as Sub<Rhs::Output>>::Output>>
。
use futures_executor::block_on;
let a = async { 44 };
let b = async { 2 };
let result = async { (async_ops::on(a) - b).await };
assert_eq!(42, block_on(result));
SubAssign
Async
实现了当被封装的Future
类型实现了SubAssign<Rhs> where Rhs: Future
时,Assignable<<Async<Future> as Sub<Rhs>>::Output>
,这反过来又要求Future::Output
类型实现Sub<Rhs::Output>
。
use futures_executor::block_on;
let a = async { 44 };
let b = async { 2 };
let result = async {
async_ops::assignable!(a);
a -= b;
a.await
};
assert_eq!(42, block_on(result));
许可证
根据以下任一许可证授权:
- Apache License 2.0 (LICENSE-APACHE 或 https://apache.ac.cn/licenses/LICENSE-2.0)
- MIT 许可证 (LICENSE-MIT 或 http://opensource.org/licenses/MIT)
任选其一。
贡献
除非您明确说明,否则您有意提交给作品作为包含在内的任何贡献,如Apache License Version 2.0中定义,应按上述方式双重许可,不附加任何额外条款或条件。
依赖关系
~540–720KB
~14K SLoC