【发布时间】:2019-09-15 09:21:45
【问题描述】:
在 Rust 中,我试图延迟类型以测试解耦的高级逻辑。理想情况下,我想将最小关系规则表示为关联类型的类型约束。在这个简化的示例中,错误类型之间唯一关键的关系是它们的值可以从低级转换为高级。
虽然这些关系似乎应该终止,但编译器会出现“溢出评估需求”的错误。我无法确定我的类型函数是否有缺陷,或者我是否遇到了 Rust 中已知或未知的限制。示例:
pub trait CapabilityA {
type Error;
fn perform_a(&self) -> Result<String, Self::Error>;
}
pub trait CapabilityB {
type Error;
fn perform_b(&self, a: &str) -> Result<(), Self::Error>;
}
pub trait Application {
type Error;
fn go(&self) -> Result<(), Self::Error>;
}
impl<T> Application for T
where
T: CapabilityA + CapabilityB,
<T as Application>::Error: From<<T as CapabilityA>::Error> + From<<T as CapabilityB>::Error>,
{
fn go(&self) -> Result<(), Self::Error> {
let a = self.perform_a()?;
let b = self.perform_b(&a)?;
Ok(b)
}
}
编译器响应:
error[E0275]: overflow evaluating the requirement `<Self as Application>::Error`
--> src/lib.rs:11:1
|
11 | / pub trait Application {
12 | | type Error;
13 | | fn go(&self) -> Result<(), Self::Error>;
14 | | }
| |_^
|
= note: required because of the requirements on the impl of `Application` for `Self`
【问题讨论】:
标签: rust