简体   繁体   English

如何指定绑定在作为结果的关联类型上的特征?

[英]How can I specify a trait bound on an associated type that is a Result?

I'm trying to write a function that can take a Stream that returns Result s.我正在尝试编写一个可以采用返回ResultStream的函数。 The trouble is in specifying boundaries for the success values of the Result .问题在于为Result的成功值指定边界。

This is an example that illustrates the problem (yes, it won't run without an async runtime, but that's not really the issue at hand):这是一个说明问题的示例(是的,它不会在没有异步运行时的情况下运行,但这并不是真正的问题):

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;
    };
}

The compiler error below makes sense, since Result does not implement AsRef :下面的编译器错误是有道理的,因为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`.

But my attempts to specify that the associated Item for S should actually be a Result fail, since Result is not a trait:但是我试图指定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`.

How can I specify that I want Stream s that return Result s that have AsRef<str> values?如何指定我想要返回具有AsRef<str>值的ResultStream I realize I will likely need to specify something for the Err type as well;我意识到我可能还需要为Err类型指定一些东西; assuming that's a similar syntax.假设这是一个类似的语法。

The syntax for equating associated types is Trait<Assoc = Type> .等同关联类型的语法是Trait<Assoc = Type>

So:所以:

async fn handle_stream<S, I, E>(mut stream: S)
where
    S: Stream<Item = Result<I, E>> + Unpin,
    I: AsRef<str>,
{
    // ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从排名较高的特征绑定特征返回关联类型? - How do I return an associated type from a higher-ranked trait bound trait? 如何将特征与使用特征的关联类型作为参数的超特征绑定? - How do I bound a trait with a supertrait that uses the trait's associated type as a parameter? 如何在特征本身上写一个特征绑定来引用关联类型? - How to write a trait bound for a reference to an associated type on the trait itself? 如何在另一个泛型类型的特征绑定的类型参数上表达特征? - How can I express a trait bound on a type parameter for another generic type's trait bound? 要求在继承特征的关联类型上绑定特征 - Requiring a trait bound on the associated type of an inherited trait 如何指定关联类型<item=...>当调用一个特征的关联 function 时?</item=...> - How to specify associated type <Item=...> when calling associated function of a trait? 我怎样才能实现`从<some trait's associated type> ?` - How can I implement `From<some trait's associated type>?` 如何在特征绑定中指定`std :: ops :: Mul`的预期结果? - How do I specify the expected result of a `std::ops::Mul` in a trait bound? 使用关联类型时不满足特征边界 - Trait bound is not satisfied when using associated type 如何绑定关联类型以使用 `?` 运算符? - How can I bound an associated type to work with the `?` operator?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM