就上下文而言,我正在使用Vulkano库制作游戏,因此我省略了所有Vulkano导入。我试图使用render()
函数来渲染我的世界,但是我对错误感到困惑,因为我没有引用任何从world.rs
到mesh.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/