[英]How can I bound an associated type to work with the `?` operator?
Given the following Rust:给定以下 Rust:
struct E;
trait Fallible {
type Error: Into<E>;
fn e(&self) -> Result<(), Self::Error>;
}
fn test(f: &impl Fallible) -> Result<(), E> {
Ok(f.e()?)
}
I am trying to express that the Fallible::Error
type can be converted to an E
and therefore should be usable with the ?
我试图表达
Fallible::Error
类型可以转换为E
,因此应该可以与?
operator.操作员。 But, for some reason the
?
但是,出于某种原因
?
is based on the From
trait, which I'm not sure that it is possible to bound.基于
From
特征,我不确定是否可以绑定。
This currently fails with:这目前失败:
error[E0277]: `?` couldn't convert the error to `E`
--> src/lib.rs:9:13
|
9 | Ok(f.e()?)
| ^ the trait `std::convert::From<<impl Fallible as Fallible>::Error>` is not implemented for `E`
|
= note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
= note: required by `std::convert::From::from`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.