生锈期货 - 使用Box :: PIN或PIN_MUT外部期货:: SELECT

发布于 2025-02-09 09:32:54 字数 1329 浏览 1 评论 0原文

我已经阅读了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,
};

这是一些链接

用法代码> ;

关于使用pin> pin> pin> pin

select> select!

/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

usage of Pin ;

confusion about usage of Pin

usage of select!

confusion about condition of using select!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

誰ツ都不明白 2025-02-16 09:32:55

为什么我们应该在上包装 pin> pin> uncin 如果call fuse> fuse() select> select!,如果它没有效果

是因为它不是umpin

error[E0277]: `from_generator::GenFuture<[static generator@src/lib.rs:3:49: 5:2]>` cannot be unpinned
  --> src/lib.rs:12:15
   |
12 |       let res = select! {
   |  _______________^
13 | |         a_res = fut_1 => a_res,
14 | |         b_res = fut_2 => b_res,
15 | |     };
   | |_____^ within `futures::future::future::fuse::_::__Origin<'_, impl futures::Future<Output = usize>>`, the trait `Unpin` is not implemented for `from_generator::GenFuture<[static generator@src/lib.rs:3:49: 5:2]>`
   |
   = note: consider using `Box::pin`
   = note: required because it appears within the type `impl futures::Future<Output = usize>`
   = note: required because it appears within the type `impl futures::Future<Output = usize>`
   = note: required because it appears within the type `Option<impl futures::Future<Output = usize>>`
   = note: required because it appears within the type `futures::future::future::fuse::_::__Origin<'_, impl futures::Future<Output = usize>>`
   = note: required because of the requirements on the impl of `Unpin` for `futures::future::Fuse<impl futures::Future<Output = usize>>`
note: required by a bound in `futures_util::async_await::assert_unpin`
  --> /playground/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-util-0.3.21/src/async_await/mod.rs:50:24
   |
50 | pub fn assert_unpin<T: Unpin>(_: &T) {}
   |                        ^^^^^ required by this bound in `futures_util::async_await::assert_unpin`
   = note: this error originates in the macro `$crate::select_internal` (in Nightly builds, run with -Z macro-backtrace for more info)

因此,它对于选择!无效。但是,即使对于!umpin值,如果我们将其固定,则固定值确实实现了umpin(不是因为pin> pin本身,而是因为>框和引用始终是uncin)。

为什么pin可以在的表达中使用!

因为宏可以固定呼叫本身的结果。但是,当直接使用变量时,它不能很好地做到这一点。

why we should wrap Pin on a Unpin if call fuse() outside a select!, if it have no effect

Because it is not Unpin:

error[E0277]: `from_generator::GenFuture<[static generator@src/lib.rs:3:49: 5:2]>` cannot be unpinned
  --> src/lib.rs:12:15
   |
12 |       let res = select! {
   |  _______________^
13 | |         a_res = fut_1 => a_res,
14 | |         b_res = fut_2 => b_res,
15 | |     };
   | |_____^ within `futures::future::future::fuse::_::__Origin<'_, impl futures::Future<Output = usize>>`, the trait `Unpin` is not implemented for `from_generator::GenFuture<[static generator@src/lib.rs:3:49: 5:2]>`
   |
   = note: consider using `Box::pin`
   = note: required because it appears within the type `impl futures::Future<Output = usize>`
   = note: required because it appears within the type `impl futures::Future<Output = usize>`
   = note: required because it appears within the type `Option<impl futures::Future<Output = usize>>`
   = note: required because it appears within the type `futures::future::future::fuse::_::__Origin<'_, impl futures::Future<Output = usize>>`
   = note: required because of the requirements on the impl of `Unpin` for `futures::future::Fuse<impl futures::Future<Output = usize>>`
note: required by a bound in `futures_util::async_await::assert_unpin`
  --> /playground/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-util-0.3.21/src/async_await/mod.rs:50:24
   |
50 | pub fn assert_unpin<T: Unpin>(_: &T) {}
   |                        ^^^^^ required by this bound in `futures_util::async_await::assert_unpin`
   = note: this error originates in the macro `$crate::select_internal` (in Nightly builds, run with -Z macro-backtrace for more info)

So it is not valid for select!. However, even for !Unpin value, if we pin it the pinned value does implement Unpin (not because of Pin itself but because Box and references are always Unpin).

why a Pin could be used in expression of select! and pass the checker if there needs a Unpin.

Because the macro can pin the result of the call itself. However, when using variables directly it cannot do that soundly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文