为什么编译器会产生 "cycle detected when processing ..." 错误?
Why does the compiler result an error of "cycle detected when processing ..."?
就上下文而言,我正在使用 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
- ...
mesh.rs
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
中的其他导入,但其中 none 重新导入了 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 复杂的 trait 系统非常幼稚。我必须做的唯一改变是在关联类型 Vertex
上添加一个 'static
生命周期,这解决了这个问题,这个问题确实令人困惑,因为错误显示为循环引用。
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
}
就上下文而言,我正在使用 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
- ...
mesh.rs
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
中的其他导入,但其中 none 重新导入了 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 复杂的 trait 系统非常幼稚。我必须做的唯一改变是在关联类型 Vertex
上添加一个 'static
生命周期,这解决了这个问题,这个问题确实令人困惑,因为错误显示为循环引用。
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
}