为什么我得到“评估需求溢出"?当使用 Diesel 的 trait 将函数重写为 trait 方法时? [英] Why do I get "overflow evaluating the requirement" when rewriting a function using Diesel's traits into a trait method?

查看:47
本文介绍了为什么我得到“评估需求溢出"?当使用 Diesel 的 trait 将函数重写为 trait 方法时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Diesel 添加分页.如果我使用函数,编译器能够检查泛型类型的边界,但如果我尝试执行与特征的实现相同的操作,则不能.

I am trying to add pagination using Diesel. The compiler is able to check bounds on a generic type if I use a function but isn't if I try to do the same as an implementation of a trait.

这是一个简单的工作示例:

This is a simple working example:

use diesel::query_dsl::methods::{LimitDsl, OffsetDsl};

pub fn for_page<T>(query: T)
where
    T: OffsetDsl,
    T::Output: LimitDsl,
{
    query.offset(10).limit(10);
}

OffsetDslLimitDsl 是 Diesel 的特征提供方法 offsetlimit.

当我尝试将此方法提取为特征并像这样实现它时

When I try to extract this method as a trait and implement it like this

use diesel::query_dsl::methods::{LimitDsl, OffsetDsl};

trait Paginator {
    fn for_page(self);
}

impl<T> Paginator for T
where
    T: OffsetDsl,
    <T as OffsetDsl>::Output: LimitDsl,
{
    fn for_page(self) {
        self.offset(10).limit(10);
    }
}

我收到一条不太清楚的错误消息.

I get a not very clear error message.

error[E0275]: overflow evaluating the requirement `<Self as diesel::query_dsl::offset_dsl::OffsetDsl>::Output`
 --> src/main.rs:3:1
  |
3 | / trait Paginator {
4 | |     fn for_page(self);
5 | | }
  | |_^
  |
  = note: required because of the requirements on the impl of `Paginator` for `Self`
note: required by `Paginator`
 --> src/main.rs:3:1
  |
3 | trait Paginator {
  | ^^^^^^^^^^^^^^^

error[E0275]: overflow evaluating the requirement `<Self as diesel::query_dsl::offset_dsl::OffsetDsl>::Output`
 --> src/main.rs:4:5
  |
4 |     fn for_page(self);
  |     ^^^^^^^^^^^^^^^^^^
  |
  = note: required because of the requirements on the impl of `Paginator` for `Self`
note: required by `Paginator`
 --> src/main.rs:3:1
  |
3 | trait Paginator {
  | ^^^^^^^^^^^^^^^

我理解这意味着编译器无法检查 T::Output 上的条件,但不清楚与条件相同的简单函数有什么区别.

I understand that this means that the compiler cannot check the condition on T::Output, but it's not clear what is the difference with a simple function with the same condition.

我使用的是 Rust 1.35.0 和 Diesel 1.4.

I'm using Rust 1.35.0 and Diesel 1.4.

推荐答案

我无法回答为什么它们不同.我可以说重复 trait 定义的边界会编译:

I cannot answer why they differ. I can say that repeating the bounds on the trait definition compiles:

use diesel::query_dsl::methods::{LimitDsl, OffsetDsl};

trait Paginator
where
    Self: OffsetDsl,
    Self::Output: LimitDsl,
{
    fn for_page(self);
}

impl<T> Paginator for T
where
    T: OffsetDsl,
    T::Output: LimitDsl,
{
    fn for_page(self) {
        self.offset(10).limit(10);
    }
}

您可能还对 extending Diesel 指南 感兴趣,其中讨论了如何最好地添加分页方法.

You may also be interested in the extending Diesel guide, which discusses how best to add a paginate method.

这篇关于为什么我得到“评估需求溢出"?当使用 Diesel 的 trait 将函数重写为 trait 方法时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆