在尝试推迟关联类型时“溢出评估需求”

问题描述 投票:0回答:2

在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
2个回答
3
投票

一个更简单的例子reproducing the same error是:

pub trait Foo {}

pub trait Application {
    type Error;
}

impl<T> Application for T where <T as Application>::Error: Foo {}

你对Application的定义是递归的。要知道Ts实现Application,你需要评估<T as Application>,这需要编译器知道Ts实现Application,等等。

Application的实现中,你必须选择具体的Error,例如here with String

impl<T> Application for T
where
    T: CapabilityA + CapabilityB,
    String: From<<T as CapabilityA>::Error>,
    String: From<<T as CapabilityB>::Error>,
{
    type Error = String;

    fn go(&self) -> Result<(), Self::Error> {
        let a = self.perform_a()?;
        let b = self.perform_b(&a)?;
        Ok(b)
    }
}

0
投票

基于@ mcarton的回答及其讨论,编译和表达我的意图的替代方案是:

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 HasApplicationError: {
    type Error;
}

pub trait Application : HasApplicationError {
    fn go(&self) -> Result<(), Self::Error>;
}

impl<T> Application for T
where
    T: CapabilityA + CapabilityB + HasApplicationError,
    <T as HasApplicationError>::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)
    }
}

这是一个比我希望的更多的代码,但似乎很好。

© www.soinside.com 2019 - 2024. All rights reserved.