[英]What does this higher-ranked trait bound mean?
In Salsa, there is a higher-ranked trait bound on a trait.在 Salsa 中,有一个更高级别的特质绑定在一个特质上。 I've seen HRTBs on function definitions but not on a trait.
我在 function 定义上看到了 HRTB,但在特征上没有看到。 What does it mean?
这是什么意思?
pub trait Query: Debug + Default + Sized + for<'d> QueryDb<'d> {
...
fn query_storage<'a>(
group_storage: &'a <Self as QueryDb<'_>>::GroupStorage,
) -> &'a Arc<Self::Storage>;
}
https://github.com/salsa-rs/salsa/blob/fc6806a/src/lib.rs#L370 https://github.com/salsa-rs/salsa/blob/fc6806a/src/lib.rs#L370
As in, how should I read this?如,我应该如何阅读这个? Is it saying that, for any
Query
, there is a corresponding QueryDB
that has some lifetime?是不是说,对于任何
Query
,都有一个相应的QueryDB
有一定的生命周期?
How is this different from这与
pub trait Query<'d>: Debug + Default + Sized + QueryDb<'d>
aside from that impls cannot specify 'd
?除此之外 impls 不能指定
'd
?
It has to do with the type in the argument for query_storage
:它与
query_storage
参数中的类型有关:
Self as QueryDb<'_>
From the nomicon:从书本上:
for<'a>
can be read as "for all choices of 'a", and basically produces an infinite list of trait bounds that F must satisfy.for<'a>
可以读作“对于'a 的所有选择”,并且基本上会产生一个 F 必须满足的特征边界的无限列表。
https://doc.rust-lang.org/nomicon/hrtb.html https://doc.rust-lang.org/nomicon/hrtb.html
how should I read this?
我应该怎么读这个?
It means that any implementation of Query
also implements QueryDb<'d>
for all possible values of 'd
(ie all lifetimes) at once .这意味着
Query
的任何实现也同时为 'd 'd
所有可能值(即所有生命周期)实现QueryDb<'d>
。 Therefore, in a generic context, the trait bound T: Query
implies T: for<'d> QueryDb<'d>
.因此,在一般上下文中,特征绑定
T: Query
意味着T: for<'d> QueryDb<'d>
。
How is this different from
这与
pub trait Query<'d>: Debug + Default + Sized + QueryDb<'d>
aside from that impls cannot specify
'd
?除此之外 impls 不能指定
'd
?
By repeating the lifetime parameter on Query
, it means that all trait bounds T: Query
will need to be changed to T: for<'d> Query<'d>
in order to be equivalent to the version where the HRTB is in Query
itself.通过在
Query
上重复生命周期参数,这意味着所有 trait bounds T: Query
都需要更改为T: for<'d> Query<'d>
以便等同于 HRTB 在Query
本身中的版本.
This is basically a workaround for the lack of generic associated types .这基本上是缺少通用关联类型的一种解决方法。 With generic associated types,
QueryDb
would instead look like this:使用泛型关联类型,
QueryDb
看起来像这样:
pub trait QueryDb: Sized {
/// Dyn version of the associated trait for this query group.
type DynDb<'d>: ?Sized + Database + HasQueryGroup<Self::Group> + 'd;
/// Associate query group struct.
type Group: plumbing::QueryGroup<GroupStorage = Self::GroupStorage>;
/// Generated struct that contains storage for all queries in a group.
type GroupStorage;
}
Before the pull request that introduced this lifetime parameter, QueryDb
wasn't a separate trait;在引入此生命周期参数的拉取请求之前,
QueryDb
并不是一个单独的特征; its members were part of Query
.它的成员是
Query
的一部分。 The generic associated type would allow us to merge QueryDb
back into Query
.通用关联类型将允许我们将
QueryDb
合并回Query
。
After reading the comments on that pull request, I get the impression that this change didn't yield the expected results.在阅读了关于该拉取请求的评论后,我的印象是这种变化没有产生预期的结果。 The goal was to allow a bound different from the implied
'static
on associated type DynDb
, but since every Query
implements QueryDb<'d>
for all possible 'd
, that means every Query
implements QueryDb<'static>
.目标是允许与关联类型
DynDb
上隐含'static
不同的界限,但由于每个Query
为所有可能'd
实现QueryDb<'d>
,这意味着每个Query
都实现QueryDb<'static>
。 Therefore, in all implementations of QueryDb
, the DynDb
cannot possibly borrow anything with a lifetime shorter than 'static
, otherwise the implementation of Query
wouldn't be allowed (the bound for<'d> QueryDb<'d>
wouldn't be satisfied).因此,在
DynDb
QueryDb
借用任何生命周期短于'static
的东西,否则将不允许Query
的实现( for<'d> QueryDb<'d>
的界限)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.