简体   繁体   English

这个更高级别的特征界限是什么意思?

[英]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.

相关问题 如何从排名较高的特征绑定特征返回关联类型? - How do I return an associated type from a higher-ranked trait bound trait? 具有更高级别特征绑定参数的异步 function 参数不起作用,一种类型比另一种类型更通用 - Async function argument with higher-ranked trait bound parameter is not working, one type is more general than the other 在 Rust 中获取“无法从在此上下文中绑定的更高级别特征中提取关联类型”错误 - Getting "cannot extract an associated type from a higher-ranked trait bound in this context" error in Rust 为什么`std::mem::drop` 与更高级别特征边界中的闭包|_|() 不完全相同? - Why isn't `std::mem::drop` exactly the same as the closure |_|() in higher-ranked trait bounds? 等号在特征绑定中意味着什么? - What does equal sign mean in a trait bound? 更高排名的特质限制和盒装封闭寿命问题 - Higher Ranked Trait Bound and boxed closures lifetime issue “特质绑定std :: fmt :: Display不满意”是什么意思? - What does “the trait bound std::fmt::Display is not satisfied” mean? 特性绑定添加到自身中意味着什么? - What does a trait bound mean when it is added to itself? 更高等级的特征边界和函数参数 - Higher ranked trait bounds and function parameters 特征名称后面的特征是什么意思? - What does the trait after the trait name mean?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM