rust - 为什么编译器会导致 “cycle detected when processing …”错误?

标签 rust traits vulkan

就上下文而言,我正在使用Vulkano库制作游戏,因此我省略了所有Vulkano导入。我试图使用render()函数来渲染我的世界,但是我对错误感到困惑,因为我没有引用任何从world.rsmesh.rs的实现。我刚开始使用Rust已有几个月了,所以我可能仍然对Traits和其他东西感到困惑。

项目源目录

src
  - main.rs  // imports all the module file in the directory
  - mesh.rs  // does not import `world.rs` module
  - world.rs  // imports the `mesh.rs` module
  - ...

网格

pub trait Mesh {
    type Vertex;

    fn vert_data(&self, texture: &TextureAtlas, position: [f32; 3]) -> Vec<Self::Vertex>;  // returns the vertex data
    fn ind_data(&self, index: u32) -> Vec<u32>;  // returns the index data
}

pub struct Cube {
    top: [u16; 2],
    bottom: [u16; 2],
    left: [u16; 2],
    right: [u16; 2],
    front: [u16; 2],
    back: [u16; 2],
}

impl Cube {
    /* ... */
}

impl Mesh for Cube {
    /* ... */
}



我已经检查了world.rs中的其他导入,但是没有一个重新导入world.rs

世界

use crate::mesh::Cube;
use crate::mesh::Mesh;

pub struct World<V, M> {
    name: String,
    chunk: Vec<Chunk<V, M>>,
}

impl<V, M> World<V, M> {
    pub fn render(device: Arc<Device>, txtr: &TextureAtlas) -> (Arc<CpuAccessibleBuffer<[dyn Mesh<Vertex=CubeVtx>]>>, Arc<CpuAccessibleBuffer<[_]>>) {
    }

    /* ... */
}


错误:
error[E0391]: cycle detected when processing `world::<impl at src\world.rs:24:1: 88:2>::render`
  --> src\world.rs:34:5
   |
34 |     pub fn render(device: Arc<Device>, txtr: &TextureAtlas) -> (Arc<CpuAccessibleBuffer<[dyn Mesh<Vertex=CubeVtx>]>>, Arc<CpuAccessibleBuffer<[_]>>) {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
note: ...which requires processing `world::<impl at src\world.rs:24:1: 88:2>::render`...
  --> src\world.rs:34:5
   |
34 |     pub fn render(device: Arc<Device>, txtr: &TextureAtlas) -> (Arc<CpuAccessibleBuffer<[dyn Mesh<Vertex=CubeVtx>]>>, Arc<CpuAccessibleBuffer<[_]>>) {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...which requires processing `mesh::Cube`...
  --> src\mesh.rs:31:1
   |
31 | pub struct Cube {
   | ^^^^^^^^^^^^^^^
   = note: ...which requires computing the variances for items in this crate...
   = note: ...which again requires processing `world::<impl at src\world.rs:24:1: 88:2>::render`, completing the cycle
note: cycle used when collecting item types in module `world`
  --> src\main.rs:50:1
   |
50 | mod world;
   | ^^^^^^^^^^


我怀疑这可能与Mesh特质有关。

我试图删除target目录,并将Mesh的实现分离到单独的文件中,仅将特征保留在原始文件中。

最佳答案

因此,我发现自己对Rust的复杂特征系统非常幼稚。我唯一要做的更改是在关联类型'static上添加了Vertex生存期,从而修复了该问题,这确实使错误显示为循环引用这一事实感到困惑。

pub trait Mesh {
    type Vertex: 'static;

    fn vert_data(&self, texture: &TextureAtlas, position: [f32; 3]) -> Vec<Self::Vertex>;  // returns the vertex data
    fn ind_data(&self, index: u32) -> Vec<u32>;  // returns the index data
}

关于rust - 为什么编译器会导致 “cycle detected when processing …”错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61178408/

相关文章:

rust - 如何在 nom 中构建负向先行解析器?

rust - 如何通过具有语法扩展的特征标识符获取原始 AST?

rust - 如何使用 Arc<Mutex<...>> 转换特征对象?

c++ - Visual Studio "The debug worker process exited unexpectedly"每次运行都在同一个断点上

c++ - 在 Vulkan 中启用垂直同步

glsl - 错误: 'subgroup op' : requires SPIR-V 1. 3

rust - 安全遍历有向无环图

rust - 使用 Xargo 从特定源编译 `core`

rust - 从枚举中访问元组

generics - rust:巨大的通用构造函数,嵌套特征