4

我试图了解 Bevy 的IntoForEachSystemtrait的实现以及它与底层 HecsQueryFetchtraits交互的方式。Hecs 具有查询类型(您在调用中请求的内容query::<T>)和项目类型(查询返回的内容)。这个想法是IntoForEachSystem为查询类型与查询的项目类型匹配的闭包实现的,并且fn f(&i32)因为&i32查询返回一个&i32项目而起作用。

我想我在这个片段中提取了设计的相关部分,但我无法对其进行类型检查:

// Hecs Query trait
trait Query {
    type Fetch: for<'a> Fetch<'a>;
}

// Hecs Query trait implementation for read-only references
impl<'a, T> Query for &'a T
where
    T: 'static,
{
    type Fetch = FetchRead<T>;
}

// Hecs Fetch trait
trait Fetch<'a>: Sized {
    type Item;
}

// Hecs Fetch trait implementation for read-only references
struct FetchRead<T>(std::marker::PhantomData<T>);

impl<'a, T> Fetch<'a> for FetchRead<T>
where
    T: 'static,
{
    type Item = &'a T;
}

// Bevy IntoForEachSystem trait, simplified
trait IntoForEachSystem<R> {
    fn system(self);
}

// Bevy IntoForEachSystem trait implementation for functions of one argument
impl<F, R> IntoForEachSystem<R> for F
where
    F: Fn(R),
    F: Fn(<<R as Query>::Fetch as Fetch>::Item),
    R: Query,
{
    fn system(self) {
        println!("hello");
    }
}

fn hmm(_x: &i32) {
    todo!()
}

fn main() {
    IntoForEachSystem::system(hmm)
}

错误:

error[E0631]: type mismatch in function arguments
   |
31 |     fn system(self);
   |     ---------------- required by `IntoForEachSystem::system`
...
46 | fn hmm(_x: &i32) {
   | ---------------- found signature of `for<'r> fn(&'r i32) -> _`
...
51 |     IntoForEachSystem::system(hmm)
   |                               ^^^ expected signature of `for<'r> fn(<FetchRead<i32> as Fetch<'r>>::Item) -> _`
   |
   = note: required because of the requirements on the impl of `IntoForEachSystem<&i32>` for `for<'r> fn(&'r i32) {hmm}`

我认为编译器将推断的生命周期'r视为fn hmm<'r>(&'r i32)不同于'a. type Fetch: for<'a> Fetch<'a>我没有看到 Bevy 用来实现相同目标的技巧。

4

1 回答 1

1

你真的超级亲密!

fn main() {
    hmm.system();
}

这是......非常令人沮丧,因为就我而言IntoForEachSystem::system(hmm)应该相当于。hmm.system()也许这是 Rust 编译器中的一个错误?

于 2020-09-16T19:51:58.843 回答