async_stream流类型

发布于 2025-02-07 12:21:54 字数 834 浏览 1 评论 0 原文

考虑以下最小流示例:

use futures::StreamExt;
use async_stream::stream;

#[tokio::main]
async fn main() {
    let ticks = stream! {
        yield Ok(0);
        yield Err("");
    };
    futures::pin_mut!(ticks);
    while let Some(x) = ticks.next().await {
        println!("{:?}", x);
    }
}

ticks 的适当类型是什么?

这个简单的问题使我无法创建一个函数。正确的类型应与功能签名一起使用。

use futures::StreamExt;
use async_stream::stream;

fn query() -> ???? {
    return stream! {
        yield Ok(0);
        yield Err("");
    };
}

#[tokio::main]
async fn main() {
    let ticks = query();
    futures::pin_mut!(ticks);
    while let Some(x) = ticks.next().await {
        println!("{:?}", x);
    }
}

您如何找到类型?

Consider the following minimal stream example:

use futures::StreamExt;
use async_stream::stream;

#[tokio::main]
async fn main() {
    let ticks = stream! {
        yield Ok(0);
        yield Err("");
    };
    futures::pin_mut!(ticks);
    while let Some(x) = ticks.next().await {
        println!("{:?}", x);
    }
}

What is the proper type of ticks?

This surprisingly simple problem has stopped me from creating a function. The correct type should work with a function signature.

use futures::StreamExt;
use async_stream::stream;

fn query() -> ???? {
    return stream! {
        yield Ok(0);
        yield Err("");
    };
}

#[tokio::main]
async fn main() {
    let ticks = query();
    futures::pin_mut!(ticks);
    while let Some(x) = ticks.next().await {
        println!("{:?}", x);
    }
}

How do you find out types?

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

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

发布评论

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

评论(1

紙鸢 2025-02-14 12:21:54

实际类型是 ,但是您不应该指定两个原因:出于两个原因:

  1. you y you> you> em> em> em> em> em> em> em> em>它,因为您无法命名 [some_compiler_generated_future_type]
  2. 即使您避开了此问题(例如,使用 Impl特征), asyncstream async_stream 的s文档中都没有提及,因为它已被标记#[doc(hidden)] - 这是图书馆的作者认为它可能会改变的实现细节,并期望您不依赖它。

图书馆文档没有提及如何做到这一点,这是非常不幸的,但是鉴于我们想返回“某些流”,我们可以使用 Impl< item = farvedtype> 来表达它。您不能在本地变量声明中执行此操作(却是),但是您可以在功能返回类型位置:

fn query() -> impl futures::Stream<Item = Result<i32, &'static str>>

The actual type is async_stream::AsyncStream<Result<i32, &'static str>, [some_compiler_generated_future_type]>, but you should not specify it, for two reasons:

  1. You cannot specify it, because you cannot name [some_compiler_generated_future_type].
  2. Even if you sidestep this problem (for example by using impl Trait), AsyncStream is mentioned nowhere in async_stream's documentation, because it is marked #[doc(hidden)] - and this is a strong signal the authors of the library consider it an implementation detail that may change, and expect you to not rely on it.

The library docs do no mention how to do that, which is quite unfortunate, but given that we want to return "some stream", we can express it using impl Stream<Item = YieldType>. You cannot do that in local variable declarations (yet), but you can at function return type position:

fn query() -> impl futures::Stream<Item = Result<i32, &'static str>>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文