生锈期货 - 使用Box :: PIN或PIN_MUT外部期货:: SELECT
我已经阅读了Futures :: Select的使用情况,但是我遇到了障碍。这是我的混乱:在expression
in select!
中,我们应该使用类型实现unpin
and fusedfuture
>,但是这里pin
unpin
在使用pin
的使用情况下无效。
1.为什么我们应该在unpin
上包装pin
如果call fuse> fuse() select!select!
,如果没有效果2.为什么pin
可以在的表达式中使用!
这是代码:
async fn async_identity_fn(arg: usize) -> usize {
arg
}
let fut_1 = async_identity_fn(1).fuse();
let fut_2 = async_identity_fn(2).fuse();
let mut fut_1 = Box::pin(fut_1); // Pins the Future on the heap
pin_mut!(fut_2); // Pins the Future on the stack
let res = select! {
a_res = fut_1 => a_res,
b_res = fut_2 => b_res,
};
这是一些链接
用法代码> ;
/docs.rs/futures/latest/futures/macro.select.html“ rel =” nofollow noreferrer “ sstatic.net/p0o29.png“ rel =“ nofollow noreferrer”>关于使用选择条件的混乱!
I had read the usage of futures::select, but I encounter an obstacle. here is my confusion: At the part of expression
in select!
, we should use a type implementing Unpin
and FusedFuture
, but here Pin
on a Unpin
is no effect accoring to usage of Pin
.
1.why we should wrap Pin
on a Unpin
if call fuse()
outside a select!
, if it have no effect 2.why a Pin
could be used in expression of select!
and pass the checker if there needs a Unpin
.
here is code:
async fn async_identity_fn(arg: usize) -> usize {
arg
}
let fut_1 = async_identity_fn(1).fuse();
let fut_2 = async_identity_fn(2).fuse();
let mut fut_1 = Box::pin(fut_1); // Pins the Future on the heap
pin_mut!(fut_2); // Pins the Future on the stack
let res = select! {
a_res = fut_1 => a_res,
b_res = fut_2 => b_res,
};
here is some link
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是因为它不是
umpin
:因此,它对于
选择!
无效。但是,即使对于!umpin
值,如果我们将其固定,则固定值确实实现了umpin
(不是因为pin> pin
本身,而是因为>框
和引用始终是uncin
)。因为宏可以固定呼叫本身的结果。但是,当直接使用变量时,它不能很好地做到这一点。
Because it is not
Unpin
:So it is not valid for
select!
. However, even for!Unpin
value, if we pin it the pinned value does implementUnpin
(not because ofPin
itself but becauseBox
and references are alwaysUnpin
).Because the macro can pin the result of the call itself. However, when using variables directly it cannot do that soundly.