async_stream流类型
考虑以下最小流示例:
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);
}
}
您如何找到类型?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际类型是 ,但是您不应该指定两个原因:出于两个原因:
[some_compiler_generated_future_type]
。Impl特征
),asyncstream
在async_stream
的s文档中都没有提及,因为它已被标记#[doc(hidden)]
- 这是图书馆的作者认为它可能会改变的实现细节,并期望您不依赖它。图书馆文档没有提及如何做到这一点,这是非常不幸的,但是鉴于我们想返回“某些流”,我们可以使用
Impl< item = farvedtype>
来表达它。您不能在本地变量声明中执行此操作(却是),但是您可以在功能返回类型位置: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:[some_compiler_generated_future_type]
.impl Trait
),AsyncStream
is mentioned nowhere inasync_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: