如何指定关联类型的特征绑定是结果?
How can I specify a trait bound on an associated type that is a Result?
我正在尝试编写一个可以接受 Stream
和 returns Result
s 的函数。问题在于为 Result
.
的成功值指定边界
这是一个说明问题的示例(是的,没有异步 运行 时间就不会 运行,但这并不是真正的问题所在):
use std::{pin::Pin, task::{Context, Poll}};
use futures::{Stream, StreamExt};
struct Data;
impl Stream for Data {
type Item = Result<String, std::io::Error>;
#[inline]
fn poll_next(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Option<Self::Item>> {
Poll::Ready(Some(Ok("Hello".to_owned())))
}
}
async fn handle_stream<S>(mut stream: S) where S: Stream + Unpin, <S as Stream>::Item: AsRef<str> {
while let Some(item) = stream.next().await {
// TODO
}
}
fn main() {
let mut data = Data{};
async move {
handle_stream(data).await;
};
}
下面的编译器错误是有道理的,因为 Result
没有实现 AsRef
:
error[E0277]: the trait bound `Result<String, std::io::Error>: AsRef<str>` is not satisfied
--> src/main.rs:24:5
|
24 | handle_stream(data).await;
| ^^^^^^^^^^^^^ the trait `AsRef<str>` is not implemented for `Result<String, std::io::Error>`
|
note: required by a bound in `handle_stream`
--> src/main.rs:15:88
|
15 | async fn handle_stream<S>(mut stream: S) where S: Stream + Unpin, <S as Stream>::Item: AsRef<str> {
| ^^^^^^^^^^ required by this bound in `handle_stream`
For more information about this error, try `rustc --explain E0277`.
但我试图指定 S
的关联 Item
实际上应该是 Result
失败,因为 Result
不是特征:
error[E0404]: expected trait, found enum `Result`
--> src/main.rs:15:88
|
15 | async fn handle_stream<S>(mut stream: S) where S: Stream + Unpin, <S as Stream>::Item: Result<AsRef<str>> {
| ^^^^^^^^^^^^^^^^^^ not a trait
For more information about this error, try `rustc --explain E0404`.
如何指定我想要 Stream
具有 AsRef<str>
值的 return Result
?我意识到我可能还需要为 Err
类型指定一些内容;假设这是一个类似的语法。
等同关联类型的语法是Trait<Assoc = Type>
。
所以:
async fn handle_stream<S, I, E>(mut stream: S)
where
S: Stream<Item = Result<I, E>> + Unpin,
I: AsRef<str>,
{
// ...
}
我正在尝试编写一个可以接受 Stream
和 returns Result
s 的函数。问题在于为 Result
.
这是一个说明问题的示例(是的,没有异步 运行 时间就不会 运行,但这并不是真正的问题所在):
use std::{pin::Pin, task::{Context, Poll}};
use futures::{Stream, StreamExt};
struct Data;
impl Stream for Data {
type Item = Result<String, std::io::Error>;
#[inline]
fn poll_next(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Option<Self::Item>> {
Poll::Ready(Some(Ok("Hello".to_owned())))
}
}
async fn handle_stream<S>(mut stream: S) where S: Stream + Unpin, <S as Stream>::Item: AsRef<str> {
while let Some(item) = stream.next().await {
// TODO
}
}
fn main() {
let mut data = Data{};
async move {
handle_stream(data).await;
};
}
下面的编译器错误是有道理的,因为 Result
没有实现 AsRef
:
error[E0277]: the trait bound `Result<String, std::io::Error>: AsRef<str>` is not satisfied
--> src/main.rs:24:5
|
24 | handle_stream(data).await;
| ^^^^^^^^^^^^^ the trait `AsRef<str>` is not implemented for `Result<String, std::io::Error>`
|
note: required by a bound in `handle_stream`
--> src/main.rs:15:88
|
15 | async fn handle_stream<S>(mut stream: S) where S: Stream + Unpin, <S as Stream>::Item: AsRef<str> {
| ^^^^^^^^^^ required by this bound in `handle_stream`
For more information about this error, try `rustc --explain E0277`.
但我试图指定 S
的关联 Item
实际上应该是 Result
失败,因为 Result
不是特征:
error[E0404]: expected trait, found enum `Result`
--> src/main.rs:15:88
|
15 | async fn handle_stream<S>(mut stream: S) where S: Stream + Unpin, <S as Stream>::Item: Result<AsRef<str>> {
| ^^^^^^^^^^^^^^^^^^ not a trait
For more information about this error, try `rustc --explain E0404`.
如何指定我想要 Stream
具有 AsRef<str>
值的 return Result
?我意识到我可能还需要为 Err
类型指定一些内容;假设这是一个类似的语法。
等同关联类型的语法是Trait<Assoc = Type>
。
所以:
async fn handle_stream<S, I, E>(mut stream: S)
where
S: Stream<Item = Result<I, E>> + Unpin,
I: AsRef<str>,
{
// ...
}