gpt4 book ai didi

rust - 为什么在将使用 Diesel 的特征重写为特征方法的函数时得到 "overflow evaluating the requirement"?

转载 作者:行者123 更新时间:2023-11-29 08:08:24 27 4
gpt4 key购买 nike

我正在尝试使用 Diesel 添加分页。如果我使用函数,编译器能够检查泛型类型的边界,但如果我尝试做与特征实现相同的事情,编译器就不能。

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

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

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

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);
}
}

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

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 上的条件,但不清楚与具有相同条件的简单函数有什么区别。

我正在使用 Rust 1.35.0 和 Diesel 1.4。

最佳答案

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

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 guide 感兴趣,其中讨论了如何最好地添加 paginate 方法。

关于rust - 为什么在将使用 Diesel 的特征重写为特征方法的函数时得到 "overflow evaluating the requirement"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56341062/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com