compiler-errors - 奇怪地重复出现的通用特征模式 : overflow evaluating the requirement

标签 compiler-errors rust traits type-constraints

我正在尝试用一堆字段实现一个通用结构,其中每个字段类型都应该知道整个结构的确切类型。这是一种策略模式。

pub struct Example<S: Strategy<Example<S, D>>, D> {
    pub s: S,
    pub a: S::Associated,
    pub data: D,
}
pub trait Strategy<T> {
    type Associated;
    fn run(&self, &T);
}
pub trait HasData {
    type Data;
    fn data(&self) -> &Self::Data;
}

impl<S: Strategy<Self>, D> Example<S, D> {
//               ^^^^
// the complex code in this impl is the actual meat of the library:
    pub fn do_it(&self) {
        self.s.run(self); // using the Strategy trait
    }
}
impl<S: Strategy<Self>, D> HasData for Example<S, D> {
    type Data = D;
    fn data(&self) -> &D {
        &self.data
    }
}

然后我打算从上面的“库”中实例化泛型:

pub struct ExampleStrat;
pub struct ExampleData;

impl<E: HasData<Data = ExampleData>> Strategy<E> for ExampleStrat {
    type Associated = ();
    fn run(&self, e: &E) {
        let _ = e.data();
        // uses ExampleData here
    }
}
let example = Example {
    s: ExampleStrat,
    a: (),
    data: ExampleData,
};
example.do_it();

在我的实际代码中,我有很多不同的“策略”和多个数据字段,因此 Example type 有一个令人印象深刻的泛型列表,如果库用户不需要明确说明它们(或者至少不经常)而可以只使用 HasData,我会很高兴。特征(及其关联类型,而不是泛型类型参数)。

如果 struct Example<S, D> 中没有类型绑定(bind),这实际上(令人惊讶地)很好,比我最初预期的要好得多(在 fighting with Self in the struct bounds 之后)。不过还是推荐给duplicate the impl trait bounds on the struct 当结构只应该与受限类型一起使用时,在我的情况下,我实际上需要它们才能使用 Associated输入 a字段。

现在编译器报错了

error[E0275]: overflow evaluating the requirement `main::ExampleStrat: Strategy<Example<main::ExampleStrat, main::ExampleData>>`
  --> src/main.rs:42:9
   |
42 |         a: (),
   |         ^^^^^
   |
   = note: required because of the requirements on the impl of `HasData` for `Example<main::ExampleStrat, main::ExampleData>`
   = note: required because of the requirements on the impl of `Strategy<Example<main::ExampleStrat, main::ExampleData>>` for `main::ExampleStrat`

我该如何解决这个问题?我是在尝试做一些不可能的事情,我是不是做错了,或者这应该是可能的,但我正在成为 a compiler bug 的牺牲品。 ?我的完整设计有缺陷吗?

最佳答案

首先,如果您避免在结构和特征的定义上设置特征界限,一切都会变得更加清晰。当事情变得复杂时,约束至少从同一个方向解决。

pub struct Example<S, D, A> {
    pub s: S,
    pub a: A,
    pub data: D,
}

pub trait Strategy<T> {
    type Associated;
    fn run(&self, &T);
}

pub trait HasData {
    type Data;
    fn data(&self) -> &Self::Data;
}

impl<S, D, A> Example<S, D, A>
where
    S: Strategy<Self, Associated = A>,
{
    pub fn do_it(&self) {
        self.s.run(self);
    }
}

impl<S, D, A> HasData for Example<S, D, A>
where
    S: Strategy<Self, Associated = A>,
{
    type Data = D;
    fn data(&self) -> &D {
        &self.data
    }
}

ExampleStratStrategy 实现如下所示:

impl<E: HasData<Data = ExampleData>> Strategy<E> for ExampleStrat {
    type Associated = ();
     // ...
}

这意味着您正在为所有可能的限定类型 E 定义它。类型检查器现在只能查看 trait bounds,它们又是通用的,并且只能根据其他 traits 来表示,这些 traits 将彼此用作边界,因此类型检查器进入了一个循环。通过给它一个你知道的具体类型,在循环中放置一个 block 。

pub struct ExampleStrat;
pub struct ExampleData;

impl Strategy<Example<ExampleStrat, ExampleData, ()>> for ExampleStrat {
    type Associated = ();
    fn run(&self, e: &Example<ExampleStrat, ExampleData, ()>) {
        let _ = e.data();
        // uses ExampleData here
    }
}

fn main() {
    let example = Example {
        s: ExampleStrat,
        a: (),
        data: ExampleData,
    };
    example.do_it();
}

关于compiler-errors - 奇怪地重复出现的通用特征模式 : overflow evaluating the requirement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50657585/

相关文章:

c - 我正在创建一个简单的文本函数,该函数应该在屏幕上显示文本,但在编译代码时会中断。有谁知道是什么打破了它?

rust - 如何将字符串列表缩写为最短、明确的形式

collections - 如何通过结构相等检查结构集合中的结构?

rust - cargo 日志消息存储在哪里?

rust - 具有特征的通用 API

PHPUnit 确保特征满足接口(interface)

c++ - "autoreconf": include/Makefile. 未找到

c - C程序中的' 'struct'之前的预期表达式 ' and '参数太少以至于无法起作用'

java - 为什么如果我的文件名和公共(public)类名不同,我会收到编译错误?

traits - Kotlin:无法从 trait 访问父类(super class)